OpenCV - 无法使VideoCapture工作

时间:2016-07-29 18:07:01

标签: python opencv

我有以下Python代码加载视频文件并在窗口中显示:

cap = cv2.VideoCapture('file.mp4')
cap.open('file.mp4')
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow("My Video Window", frame)

cap.release()
cv2.destroyAllWindows()

然而,当我运行它时,我收到以下错误:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/opencv3-20160626-61731-v18vfg/opencv-3.1.0/modules/highgui/src/window.cpp, line 281
Traceback (most recent call last):
  File "file.py", line 66, in <module>
    cv2.imshow("My Video Window", frame)
cv2.error: /tmp/opencv3-20160626-61731-v18vfg/opencv-3.1.0/modules/highgui/src/window.cpp:281: error: (-215) size.width>0 && size.height>0 in function imshow

可能是什么问题? 谢谢!

如果我添加:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

我明白了:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor, file /tmp/opencv3-20160626-61731-v18vfg/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7456
Traceback (most recent call last):
  File "file.py", line 66, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /tmp/opencv3-20160626-61731-v18vfg/opencv-3.1.0/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor

0 个答案:

没有答案