问题描述
我正在尝试编写python3-opencv3代码以读取彩色视频并将其转换为灰度并保存回去.(尝试练习以学习python& opencv)
I am trying to a write a python3-opencv3 code to read the color video and convert it to grayscale and save it back.(Trying exercises to learn python & opencv)
在ubuntu中工作时,我发现cv2.VideoCapture isColor标志不起作用(仅适用于Windows)
As I am working in ubuntu, I found out cv2.VideoCapture isColor flag will not work (it works only 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在函数写入中
It is give me thiserror: (-215) img.cols == width && img.rows == height*3 in function write
我正在猜测,问题出在帧大小和灰度图像转换上,但我无法弄清楚?我尝试了不同的高度和高度组合.虽然无法执行程序.
I am guessing, the problem is with framesize and grayscale image conversion but I am not able to figure out ? I have tried different combination of height & widht but none are able execute the program correctly.
有人可以帮忙吗?
根据评论的要求:
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
.)
OpenCV error -215
means "assertion failed". The assertion itself is listed in the message (the phrase "Assertion failed" itself probably should, too; you can open a ticket about this in OpenCV's bug tracker). (Update: my pull request with this change was accepted on 06.03 and was released in opencv
3.4.2
.)
正如可以从断言中得出的那样,它希望帧是3色通道的帧.要制作灰度视频,您需要将isColor=False
传递给构造函数:
As one can deduce from the assertion, it expects the frame to be a 3-color-channel one. To make a grayscale video, you need to pass isColor=False
to VideoWriter
constructor:
out = cv2.VideoWriter('Video_output.avi', fourcc, FPS, (width,height), False)
这篇关于VideoWriter.write中的Python OpenCV错误(-215)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!