使用Matlab中的代码在Python中逐帧读取视频

2024-06-12 02:06:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试翻译下面的代码,它假设一帧一帧地读取视频,然后计算帧之间的差异,我的翻译似乎不太好,因为我得到的字段值不同

afTime = zeros(1,NumFrames-1);
for iFrameIter=1:NumFrames-1
    rgbCurrImage = read(hVideo,iFrameIter);
    rgbSubSampled = double(rgbCurrImage(1:2:end,1:2:end,:))/255;
    graySubSampled = 0.299*rgbSubSampled(:,:,1) + 0.587*rgbSubSampled(:,:,2) + 0.114*rgbSubSampled(:,:,3);

我认为Python有一个较短的方法来实现这一点(不是为每个通道手动),但在Matlab和Python中无法达到相同的值。一直在使用cv2读取视频

这是我的python代码:

afSimilarity = np.zeros(number_of_frames - 1)
PrevResult = np.zeros(int(iQuantizedNumBlocks))

    for iFrameIter in range(number_of_frames - 1):
        print(iFrameIter)
        ret, rgbCurrImage = cap.read()
        rgbSubSampled = np.double(rgbCurrImage[::2, ::2, :]) / 255
        graySubSampled = 0.299 * rgbSubSampled[:, :, 2] + 0.587 * rgbSubSampled[:, :, 1] + 0.114 * rgbSubSampled[:, :, 0]

例如,对于第一帧,我在Python中获得的graySubSampled值的结果: enter image description here

0.232776471 0.244988235 0.23174902 0.267937255

0.237278431 0.242094118 0.252003922 0.25592549

0.253478431 0.232976471 0.248215686 0.253545098

0.242094118 0.237278431 0.248215686 0.2613828235

python中rgbCurrImage的值为: rgbCurrImage[0]: 675565

685666

715868

736070

IrgbCurrImage1: 675565

685666

715868

736070

rgbCurrImage2: 59 58 66

59 58 66

62 59 57

64 61 69

rgbCurrImage[3]: 60 59 67

60 59 67

61 58 66

63 60 68

我很确定这是bgr,而不是python中的rgb,但我后来处理了它

在Matlab中: enter image description here

0.239 0.25168824 0.241168627 0.277356863

0.242094118 0.246909804 0.258160784 0.262082353

0.2574 0.237345098 0.253031373 0.256058824

0.245121569 0.242607843 0.251243137 0.262113725

matlab中rgbCurrImage的值为: rgbcurrimage(1,:,:): 665768

675870

696073

716279

rgbcurrimage(2,:,:): 66 58 66

675968

696071

716276

rgbcurrimage(3,:,:): 675962

675963

686065

70 62 69

rgbcurrimage(4,:,:): 686158

686158

675958

696160


Tags: 代码forread视频npzerosenddouble