如何解决"cv2.错误:(-215)size.width> 0&& size.height>在功能"imshow"中为"0"; Python错误? [英] How to solve "cv2. error: (-215) size.width>0 && size.height>0 in function imshow" Error in Python?

查看:303
本文介绍了如何解决"cv2.错误:(-215)size.width> 0&& size.height>在功能"imshow"中为"0"; Python错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于通过网络摄像头捕获视频的代码:

This is the code I am using for capturing video through my webcam:

import cv2
cap = cv2.VideoCapture(0)

while True:
    ret, frame= cap.read()
    # frame= cv2.resize(frame, None,fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
    cv2.imshow('Input', frame)
    c = cv2.waitKey(1)
    if c == 27:
        break
cap.release()
cv2.destroyAllWindows()

错误是:

OpenCV错误:断言失败(size.width> 0& size.height> 0),在imshow中,文件为/io/opencv/modules/highgui/src/window. cpp,线325 追溯(最近一次通话): 文件"video_capturing2.py",第10行,在 cv2.imshow('输入',帧) cv2.error:/io/opencv/modules/highgui/src/window.cpp:325:错误:(-215)size.width> 0& size.height> 0在功能上 怎么

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /io/opencv/modules/highgui/src/window. cpp, line 325 Traceback (most recent call last):   File "video_capturing2.py", line 10, in    cv2.imshow('Input', frame) cv2.error: /io/opencv/modules/highgui/src/window.cpp:325: error: (-215) size.width>0 && size.height>0 in function ims how

我该如何解决?每当我运行上面的代码时,ret标志就永远不会为True.我已经尝试过sudo pip3 install opencv-contrib-python,但仍然无济于事.

How do I solve it? whenever I am running the code above, the ret flag is never True. I have tried sudo pip3 install opencv-contrib-python but still it is not helping.

推荐答案

imshow失败.就您而言,这很可能是由于您忽略了cap.read()中的空白帧并调用了imshow的事实,即使cap.read返回了空白图像.

imshow fails when you pass an empty image, as pointed out by @101. In your case, this is most likely due to the fact that you ignore empty frames from cap.read() and call imshow even if cap.read returned an empty image.

要解决此问题,请用while cap.isOpened()替换while True循环,或者在读取帧之前检查cap.isOpened()是否成功.

To fix this, replace your while True loop with a while cap.isOpened() or check whether cap.isOpened() has been successful before reading frames.

这篇关于如何解决"cv2.错误:(-215)size.width> 0&& size.height>在功能"imshow"中为"0"; Python错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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