gpt4 book ai didi

python - opencv python VideoWriter 的问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:00 27 4
gpt4 key购买 nike

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

关于python - opencv python VideoWriter 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26275003/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com