我一直试图让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().