阅读webCamera时遇到Ubuntu OpenCV错误

时间:2016-12-14 15:18:35

标签: python-2.7 opencv ubuntu webcam

我可以在Windows中运行相同的程序。我可以在Ubuntu 16.04,64位中使用lsusb看到我的相机。相机是OSVR红外相机。

我的节目是

import numpy as np
import cv2
camera = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = camera.read()

    cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('camera', frame)

    #    k = cv2.waitKey(30)

# When everything done, release the capture
camera.release()
cv2.destroyAllWindows()

结果是:

cwu@ubuntu:~/project/osvrCamera$ python test.py 
select timeout
select timeout
OpenCV Error: Assertion failed (!buf.empty() && buf.isContinuous()) in imdecode_, file /tmp/binarydeb/ros-kinetic-opencv3-3.1.0/modules/imgcodecs/src/loadsave.cpp, line 490
Traceback (most recent call last):

File "test.py", line 8, in <module>
ret, frame = camera.read() 
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.1.0/modules/imgcodecs/src/loadsave.cpp:490: error: (-215) !buf.empty() && buf.isContinuous() in function imdecode_

1 个答案:

答案 0 :(得分:0)

首先检查框架是否为empty()(当您尝试转换/显示时,凸轮/框架可能未完全初始化:

import numpy as np
import cv2
camera = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = camera.read()
    if not frame.empty():
       cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

       cv2.imshow('camera', frame)

    #    k = cv2.waitKey(30)

# When everything done, release the capture
camera.release()
cv2.destroyAllWindows()