繁体   English   中英

opencv python VideoWriter的问题

[英]Issues with opencv python VideoWriter

我一直试图让openCV python VideoWriter对象无法正常工作。 我要做的是读取视频,抓取帧,进行一些处理并将其写回视频文件。 我得到的错误是:

Traceback (most recent call last):
File "detect.py", line 35, in <module>
    out_video.write(processed)
cv2.error: /tmp/opencv-z9Pa/opencv-2.4.9/modules/imgproc/src/color.cpp:4419: 
error: (-215) src.depth() == dst.depth() in function cvCvtColor

我写的代码如下:

video = VideoCapture(args["video"])
num_frames = video.get(CV_CAP_PROP_FRAME_COUNT)
width = video.get(CV_CAP_PROP_FRAME_WIDTH)
height = video.get(CV_CAP_PROP_FRAME_HEIGHT)

fps = video.get(CV_CAP_PROP_FPS)

out_video = VideoWriter()
fourcc = CV_FOURCC('m', 'p', '4', 'v')
out_video.open(args["out"], fourcc, int(fps), (int(width), int(height)),True)

while (video.isOpened()):
    ret, frame = video.read()
    # This simply takes the frame and does some image processing on it
    segments = compute_superpixels(frame, num_pixels=100)
    processed = mark_boundaries(frame, segments)
    out_video.write(processed)

有谁知道我在这里做错了什么?

[编辑]

我尝试了一些可能会带来一些亮点(或不亮)的东西。 所以,如果我想写原始帧即替换

out_video.write(processed)

out_video.write(frame)

我收回了原始视频。 但是,框架和处理对象具有相同的大小和类型! 所以,现在我完全不知道发生了什么。 处理和框架形状和类型的输出是:

frame: (576, 720, 3)
processed: (576, 720, 3)
frame: <type 'numpy.ndarray'>
processed: <type 'numpy.ndarray'>

我弄清楚出了什么问题。 这条线

processed = mark_boundaries(frame, segments)

实际上是将图像归一化在0和1之间,所以它不是8位深度,这是一个问题。 该修复程序正在执行以下操作:

processed = (processed * 255.0).astype('u1')

然后将其传递给VideoWriter.write_frame()。

当您尝试将图像转换为颜色空间时,函数cvtColor引发的错误(-215)会出现但不可能。

例如,如果您尝试将已经是灰色的图像或二进制转换为灰色,则converte颜色将引发此错误

暂无
暂无

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

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