简体   繁体   English

OpenCV错误:在cv :: imshow(Python)中断言失败(size.width> 0 && size.height> 0)

[英]OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow (Python)

I went trough many similar topics like mine, but found no solution for my code. 我经历了许多类似我的主题,但是没有找到我的代码的解决方案。 First of all I simply want to run my webcam and see a picture. 首先,我只想运行我的网络摄像头并查看图片。

import numpy as np
import cv2

cap = cv2.VideoCapture(1)

while True:
  re,img=cap.read()
  cv2.imshow("video output", img)
  k = cv2.waitKey(10)&0xFF
  if k==27:
     break
cap.release()
cv2.destroyAllWindows()

I also tried it with: 我也尝试过:

if img is not None:

I still get this error: 我仍然收到此错误:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow

I have to say that I am not using my laptop webcam so its id is not 0, but it should be 1. I read about solutions like in c++: frame=cvQueryFrame(capture); if (frame.empty()) break; 我不得不说我没有使用我的笔记本电脑网络摄像头,因此它的ID不为0,但应该为1 frame=cvQueryFrame(capture); if (frame.empty()) break; frame=cvQueryFrame(capture); if (frame.empty()) break; But how to do it in Python? 但是,如何在Python中做到这一点? I do not think that this will fix my problem? 我认为这不会解决我的问题吗? Has anybody a solution? 有没有人解决?

Best regards 最好的祝福

According to the OpenCV docs : 根据OpenCV的文档

cap.read() returns a bool (True/False). cap.read()返回布尔值(True / False)。 If frame is read correctly, it will be True. 如果正确读取帧,它将为True。

... ...

Sometimes, cap may not have initialized the capture. 有时,cap可能尚未初始化捕获。 In that case, this code shows error. 在这种情况下,此代码显示错误。 You can check whether it is initialized or not by the method cap.isOpened(). 您可以通过cap.isOpened()方法检查它是否已初始化。 If it is True, OK. 如果为True,则确定。 Otherwise open it using cap.open(). 否则,请使用cap.open()将其打开。

Thus your code becomes: 因此,您的代码变为:

import numpy as np
import cv2

device = 1
cap = cv2.VideoCapture(device)
# if capture failed to open, try again
if not cap.isOpened():
    cap.open(device)

# only attempt to read if it is opened
if cap.isOpened:
    while True:
        re, img = cap.read()
        # Only display the image if it is not empty
        if re:
            cv2.imshow("video output", img)
        # if it is empty abort
        else:
            print "Error reading capture device"
            break
        k = cv2.waitKey(10) & 0xFF
        if k == 27:
            break
    cap.release()
    cv2.destroyAllWindows()
else:
    print "Failed to open capture device"

If you still have errors try changing device to -1, 0 or 2. Otherwise, it may be an issue unrelated to OpenCV such as driver issues. 如果仍然有错误,请尝试将设备更改为-1、0或2。否则,可能是与OpenCV无关的问题,例如驱动程序问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相机流 - OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'cv::imshow' - Camera Streaming - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' opencv 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'cv::imshow' - opencv error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' OpenCV(4.2.0) 错误: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' - OpenCV(4.2.0) error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow' 错误:(-215:断言失败)函数 cv::imshow 中的 size.width>0 && size.height>0 - error: (-215:Assertion failed) size.width>0 && size.height>0 in function cv::imshow OpenCV 错误:(-215:断言失败)size.width>0 && size.height>0 in function 'imshow' - OpenCV error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' OpenCV错误:在imshow中断言失败(size.width> 0 && size.height> 0) - OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow OpenCV 错误:断言失败(size.width>0 && size.height>0)python - OpenCV Error: Assertion failed (size.width>0 && size.height>0) python opencv python 错误:断言失败 (size.width>0 && size.height>0) - opencv python error: Assertion failed (size.width>0 && size.height>0) 错误:(-215:断言失败)函数“imshow”中的 size.width>0 && size.height>0 - error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' 相机校准opencv python imshow错误:(-215)size.width> 0 && size.height> 0 - Camera Calibration opencv python imshow error: (-215) size.width>0 && size.height>0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM