[英]OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
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. 我正在使用pi相机在Raspberry Pi上运行以下代码,我具有适用于所有功能的Broadcom驱动程序,但出现错误。 Perhaps something to do with the dimensions of the video feed, but I do not know how to set it on Linux. 也许与视频提要的尺寸有关,但是我不知道如何在Linux上进行设置。
Code: 码:
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
Error: 错误:
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
Provide an id to VideoCapture
. 为VideoCapture
提供一个ID。
cap = cv2.VideoCapture(0)
Also check the value of ret
, see if it's TRUE
or FALSE
还要检查ret
的值,看看它是TRUE
还是FALSE
print (ret)
Edit: 编辑:
To capture a video, you need to create a VideoCapture object. 要捕获视频,您需要创建一个VideoCapture对象。 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)
To check whether the
cap
has been initialized or not, you can usecap.isOpened()
function, which returnsTrue
for successful initialization andFalse
for failure. 要检查cap
是否已初始化,可以使用cap.isOpened()
函数,该函数返回True
表示初始化成功,返回False
表示失败。
if cap.isOpened() == False:
print ("VideoCapture failed")
cap.read()
returns a bool (True/False).cap.read()
返回布尔值(True / False)。 If frame is read correctly, it will be True. 如果正确读取帧,它将为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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.