cv2.videocapture.read()不返回一个numpy数组 [英] cv2.videocapture.read() does not return a numpy array
本文介绍了cv2.videocapture.read()不返回一个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码试图从树莓派上的网络摄像头捕获一个帧,并将其另存为图像.我使用的是opencv 2,但是在运行代码时出现奇怪的错误.
I have this code trying to capture a frame from my webcam on raspberry pi, and saving it as an image. I use opencv 2, but I get strange errors when I run the code..
import time
import sys
from subprocess import call
import cv2
cam = cv2.VideoCapture()
while True:
cam.open(-1)
image = cam.read()
cv2.imwrite("current.jpeg",image)
time.sleep(10);
这是程序返回的内容:
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Traceback (most recent call last):
File "kvamskogen.py", line 18, in <module>
cv2.imwrite("current.jpeg",image)
TypeError: <unknown> is not a numpy array
这是怎么了?
推荐答案
从VideoCapture
读取(cam.read()
)将返回元组(return value, image)
.对于第一个项目,您检查是否读取成功,如果读取成功,则继续使用返回的image
.
Reading (cam.read()
) from a VideoCapture
returns a tuple (return value, image)
. With the first item you check wether the reading was successful, and if it was then you proceed to use the returned image
.
此文档记录在 http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video .html
这篇关于cv2.videocapture.read()不返回一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文