OpenCV错误:在imshow中断言失败(size.width> 0&& size.height> 0) [英] OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow

查看:298
本文介绍了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 use cap.isOpened() function, which returns True for successful initialization and False 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")

进一步阅读此处.

这篇关于OpenCV错误:在imshow中断言失败(size.width&gt; 0&amp;&amp; size.height&gt; 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆