简体   繁体   English

为什么 opencv Videocapture function 会读取像素值错误的视频帧?

[英]Why does opencv Videocapture function read video frames with wrong pixel values?

I am capturing videos from a camera and saving them using the opencv VideoWriter function.我正在从相机捕捉视频并使用 opencv VideoWriter function 保存它们。 I save the captured videos as uncompressed avi files.我将捕获的视频保存为未压缩的 avi 文件。 When I finish recording the video, I have another script that is supposed to read the video frame by frame, process the pixel values.当我完成视频录制后,我有另一个脚本应该逐帧读取视频,处理像素值。 However, when I try to read the frames of the saved video, the pixel values are off a bit.但是,当我尝试读取已保存视频的帧时,像素值有点偏离。

For example, comparing the first frames of the video being written, and the video being read (supposed to be 100% identical), I notice that the pixel values are off by a small number (RGB values off by a small number, usually less than 5).例如,比较正在写入的视频的第一帧和正在读取的视频(假设 100% 相同),我注意到像素值偏离了一个小数字(RGB 值偏离了一个小数字,通常更少大于 5)。

I have already made sure that I am using the exact same video codex when writing the video, and when reading the video (Check code below)我已经确保在编写视频和阅读视频时使用完全相同的视频编解码器(检查下面的代码)

def write_video():
    out = cv2.VideoWriter("encrypted.avi" ,cv2.VideoWriter_fourcc(*'XVID'),30, (640,480))
    foreach frame:
        out.write(frame)

def read_video():
    cap = cv2.VideoCapture("encrypted.avi")
    cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'XVID'))
    while(cap.isOpened()):
        ret, frame = cap.read()

For my application, the frames being written and read should match 100%.对于我的应用程序,正在写入和读取的帧应该 100% 匹配。 I have included an image highlighting the difference between the first frame in the video being written, and the video being read.我附上了一张图片,突出了正在写入的视频中的第一帧和正在读取的视频之间的区别。 Any help is much appreciated!任何帮助深表感谢!

在此处输入图像描述

These are the compression artifacts, since you are using lossy compression.这些是压缩伪影,因为您使用的是有损压缩。 If you would like your frames match down to the last bit, write them as a sequence of.PNG files -- these are losslessly compressed and preserve everything.如果您希望您的帧匹配到最后一位,请将它们写为一系列 .PNG 文件——这些文件经过无损压缩并保留所有内容。 Beware that.PNG will take much more of your HDD space than compressed video.请注意,.PNG 会比压缩视频占用更多的硬盘空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM