VideoCapture获取摄像头失败

2017-10-23  本文已影响0人  不湿的尿布湿

源码

import numpy as np

cap=cv2.VideoCapture(0)

while(1):
    ret , frame = cap.read()

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

    cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

error如下:

图片.png
Traceback (most recent call last):
  File "/home/yewii/python项目/opencv/try.py", line 9, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor

解决方案:

因为Ubuntu摄像头获取的视频格式是webm
下了一个XviD解码器,导入使用
参考关于opencv中不能读取视频avi的问题

Container FourCC Name Description
AVI 'DIB ' RGB(A) Uncompressed RGB, 24 or 32 bit
AVI 'I420' RAW I420 Uncompressed YUV, 4:2:0 chroma subsampled
AVI 'IYUV' RAW I420 identical to I420
上一篇下一篇

猜你喜欢

热点阅读