OpenCV错误:在imshow中断言失败(size.width> 0&& size.height> 0)
我正在使用pi相机在Raspberry Pi上运行以下代码,我具有适用于所有功能的Broadcom驱动程序,但出现错误.也许与视频提要的尺寸有关,但是我不知道如何在Linux上进行设置.
I am running the following code on Raspberry Pi with pi camera, I have the broadcom drivers for it and all, but I am getting an error. Perhaps something to do with the dimensions of the video feed, but I do not know how to set it on Linux.
代码:
import cv2
import numpy as np
cap = cv2.VideoCapture()
while True:
ret, img = cap.read()
cv2.imshow('img', img)
if cv2.waitKey(0) & 0xFF == ord('q):
break
错误:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow,
file /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp, line 325
Traceback (most recent call last):
File "check_picam_with_opencv.py", line 10, in <module>
cv2.imshow('img', img)
cv2.error: /home/pi/opencv-3.3.0/modules/highgui/src/window.cpp:325: error:
(-215) size.width>0 && size.height>0 in function imshow
为VideoCapture
提供ID.
cap = cv2.VideoCapture(0)
还要检查ret
的值,看看它是TRUE
还是FALSE
Also check the value of ret
, see if it's TRUE
or FALSE
print (ret)
要捕获视频,您需要创建一个VideoCapture对象.它的参数可以是设备索引或视频文件的名称.设备索引只是指定哪个摄像机的编号.
To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera.
cap = cv2.VideoCapture(0)
要检查
cap
是否已初始化,可以使用cap.isOpened()
函数,该函数将返回True
表示成功初始化,而返回False
表示失败.
To check whether the
cap
has been initialized or not, you can usecap.isOpened()
function, which returnsTrue
for successful initialization andFalse
for failure.
if cap.isOpened() == False:
print ("VideoCapture failed")
cap.read()
返回布尔值(True/False).如果正确读取帧,它将为True.因此,您可以通过检查此返回值来检查视频的结尾.
cap.read()
returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.
ret, frame = cap.read()
if ret == False:
print("Frame is empty")
进一步阅读此处.