繁体   English   中英

VideoWriter.write中的Python OpenCV错误(-215)

[英]Python OpenCV Error(-215) in VideoWriter.write

我正在尝试编写python3-opencv3代码以读取彩色视频并将其转换为灰度并保存回去。 (尝试练习以学习python和opencv)

当我在ubuntu中工作时,我发现cv2.VideoCapture isColor标志将不起作用(仅适用于Windows)


import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
ret, frame = cap.read()
print('ret =', ret, 'W =', frame.shape[1], 'H =', frame.shape[0], 'channel =', frame.shape[2])
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
height, width = gray.shape[:2]
print(height,width)
cv2.imshow('frame',gray)
FPS= 20.0
FrameSize=(frame.shape[1], frame.shape[0]) 
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Video_output.avi', fourcc, FPS, (width,height))

while(ret):
    ret, frame = cap.read()
    if cv2.waitKey(1) & 0xFF == ord('q') or ret == False:
        break
    print('ret =', ret, 'W =', frame.shape[1], 'H =', frame.shape[0], 'channel =', frame.shape[2])
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Save the video
    out.write(gray) 
    cv2.imshow('frame',gray)
cap.release()
out.release()
cv2.destroyAllWindows()

这是给我的错误:(-215)img.cols == width && img.rows == height * 3在函数写入

我猜,问题出在帧大小和灰度图像转换上,但我无法弄清楚? 我曾尝试与高度的widht不同组合,但没有一个是能够正确执行程序。

有人可以帮忙吗?

根据评论中的要求:

Traceback (most recent call last):
  File "/home/akash/Coded/VideoCode/Test3", line 70, in <module>
    out.write(gray) 
cv2.error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/videoio/src/cap_mjpeg_encoder.cpp:834: error: (-215) img.cols == width && img.rows == height*3 in function write

OpenCV错误-215表示“断言失败”。 断言本身已在消息中列出(短语“断言失败”本身也可能也应如此;您可以在OpenCV的错误跟踪器中打开与此有关的故障单)。 (更新: 我对此更改的拉取请求已于06.03接受,并在opencv 3.4.2中发布。)

从断言可以推断出,它期望帧是3色通道。 要制作灰度视频, 您需要将isColor=False传递给VideoWriter构造函数

out = cv2.VideoWriter('Video_output.avi', fourcc, FPS, (width,height), False)

暂无
暂无

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

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