Ubuntu 17.04上的OpenCV VideoCapture()无法正常工作

时间:2017-06-14 09:04:16

标签: python opencv ubuntu webcam video-capture

我试图在Ubuntu 17.04上使用OpenCV的cv2 python绑定,但我似乎无法使VideoCapture正常工作。我正在使用代码进行测试:

import cv2
import numpy as np
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)
plt.ion()
while True:
    ret,image = cap.read()
    print(image)
    print(cap)
    im = np.array(image, dtype=float)
    plt.imshow(im)
    plt.pause(0.0005)
    plt.show()

,结果是

None
<VideoCapture 0x7f272f87adb0>
Traceback (most recent call last):
  File "cam.py", line 15, in <module>
    plt.imshow(im)
  File "/home/daniele/.local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 3157, in imshow
    **kwargs)
  File "/home/daniele/.local/lib/python3.5/site-packages/matplotlib/__init__.py", line 1898, in inner
    return func(ax, *args, **kwargs)
  File "/home/daniele/.local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5124, in imshow
    im.set_data(X)
  File "/home/daniele/.local/lib/python3.5/site-packages/matplotlib/image.py", line 600, in set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data

并且没有打开来自网络摄像头视频的窗口。

如果我不使用im = np.array(image, dtype=float),则错误相同,但TypeError: Image data can not convert to float 我已经检查过我的网络摄像头在我的系统中是0,我尝试用“奶酪”#39;它的工作原理。 我已经通过pip3 install opencv-python

安装了Opencv

1 个答案:

答案 0 :(得分:1)

OpenCV已经有一些内置的工具,用于使用cv2.imshow()在窗口中显示图像,使用您不需要的OpenCV API将图像转换为np.array()。查看以下代码是否有效:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret,image = cap.read()
    cv2.imshow("window", image)
    # Set the window delay time in milliseconds.
    cv2.waitkey(300)