简体   繁体   English

尝试使用 VideoCapture 和 imshow(),引发 Assertion failed (size.width>0 && size.height>0) in cv::imshow

[英]trying to use VideoCapture and imshow(), raises Assertion failed (size.width>0 && size.height>0) in cv::imshow

I'm using Visual Studio Express 2013 with OpenCV 2.4.7 , following this tutorial .我按照本教程使用 Visual Studio Express 2013 和 OpenCV 2.4.7

I have spent hours searching the web for solutions, including all of the relevant SO questions.我花了几个小时在网上搜索解决方案,包括所有相关的 SO 问题。 I have tried:我试过了:

  • the return value of VideoCapture::open is 1 VideoCapture::open的返回值为 1

  • extending the waitKey() delay to 50ms and later 500ms将 waitKey() 延迟延长至 50 毫秒,之后延长至 500 毫秒

  • setting the dimensions of the window设置窗口的尺寸

  • creating another project on Visual C++在 Visual C++ 上创建另一个项目

  • opening an existing image instead of reading from camera (same error)打开现有图像而不是从相机读取(同样的错误)

but no luck, please help!但没有运气,请帮忙!

Here's my code:这是我的代码:

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

int main() {
    Mat image;

    VideoCapture cap;
    int camOpen = cap.open(CV_CAP_ANY);

    namedWindow("window", CV_WINDOW_AUTOSIZE);

    while (true) {
        cap >> image;

        imshow("window", image);

    // delay 33ms
    waitKey(33);        
    }

}

As I compiled and ran it, I got the following error:当我编译并运行它时,出现以下错误:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file........\opencv\modules\highgui\src\window.cpp, line 261 OpenCV 错误:断言失败 (size.width>0 && size.height>0) 在 cv::imshow,文件.......\opencv\modules\highgui\src\window.cpp,第 261 行

Error occurs at the line imshow("window", image);错误发生在imshow("window", image); . . When I commented it out, there are no complaints.当我评论出来时,没有任何抱怨。


UPDATES:更新:

A plausible explanation of why this error occured was that my webcam takes time to start, which is why image.empty() is true initially, hence the abort() function was called to exit the program.出现此错误的一个合理解释是我的网络摄像头需要时间才能启动,这就是为什么 image.empty() 最初为真,因此调用 abort() 函数退出程序。

With the code用代码

if (!image.empty()) {
    imshow("window", image);
}

we can wait for the camera to start我们可以等待相机启动

I tried your code and for me it works (it visualizes the current webcam input)! 我尝试了你的代码,对我而言它是可行的(它可视化当前的网络摄像头输入)!
I ran it on Visual Studio 2012 Ultimate with OpenCV 2.4.7. 我使用OpenCV 2.4.7在Visual Studio 2012 Ultimate上运行它。
... ...
The error occurs because the image is empty, so try this: 发生错误是因为图像为空,请尝试以下操作:

while (true) {
    cap >> image;

    if(!image.empty()){
        imshow("window", image);
    }

// delay 33ms
waitKey(33);        
}

Maybe the first image you receive from your webcam is empty. 也许您从网络摄像头收到的第一张图片是空的。 In this case imshow will not throw an error. 在这种情况下,imshow不会抛出错误。 So hopefully the next input images are not empty. 所以希望下一个输入图像不是空的。

Do this: 做这个:

VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int camOpen = cap.open(CV_CAP_ANY);

Or you can try changing this: 或者你可以尝试改变这个:

while (true) {
        cap >> image;

        imshow("window", image);

    // delay 33ms
    waitKey(33);        
    }

to

try
{
     cap >> image;
     imshow("window", image);
     waitKey(33); 
}
catch (Exception& e)
{
    const char* err_msg = e.what();
    std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}
int i=0;

while(i<4)

{

VideoCapture cap(0); // force camera to open 4 tiMEs

i++;

}

waitKey(5000); 
VideoCapture cap(0);
int camOpen = cap.open(CV_CAP_ANY);

namedWindow("window", CV_WINDOW_AUTOSIZE);

while (true) {
    cap >> image;

    imshow("window", image);
waitKey(33);        
}

Do this it will work for you for sure. 这样做肯定会对你有用。

Always check for errors.始终检查错误。

You can either enable exception throwing from the VideoCapture instance, or you will have to check manually.您可以从VideoCapture实例启用异常抛出,或者您必须手动检查。

If checking manually, there are two places you need to check:如果手动检查,有两个地方需要检查:

  1. assert(cap.isOpened()); after creating/opening the VideoCapture instance.在创建/打开VideoCapture实例之后。 If it could not be opened, there is no exception being raised by that.如果无法打开,则不会引发异常。 You have to check and handle this.你必须检查和处理这个。 An assertion suffices but you can handle this more gracefully if you want.一个断言就足够了,但如果你愿意,你可以更优雅地处理它。

  2. if (image.empty()) break; after each cap >> image;在每个cap >> image; , or if (.cap;read(image)) break; , 或者if (.cap;read(image)) break; instead of cap.read(image);而不是cap.read(image); , because the video stream may have ended or there's some issue with the camera. ,因为视频流可能已结束或相机出现问题。 Again, no automatic exception being thrown.同样,没有抛出自动异常。 You need to check and handle this.你需要检查和处理这个。 The read() may fail intermittently (ie next read() works again), but that's rare. read()可能会间歇性地失败(即下一个read()再次工作),但这种情况很少见。 Usually, once a read() fails, all following read() calls on that VideoCapture instance will also fail.通常,一旦read()失败,该 VideoCapture 实例上的所有后续read()调用也将失败。

There is VideoCapture::setExceptionMode() , which allows read() and open() calls to actually throw exceptions on their own instead of failing silently.VideoCapture::setExceptionMode() ,它允许read()open()调用实际抛出异常,而不是默默地失败。 In that case, you still have to catch the exception and inspect it ( e.what() ).在那种情况下,您仍然必须捕获异常并检查它( e.what() )。 If you don't catch and inspect the exception explicitly, the runtime may just print to stderr that some exception occurred, but no details on it.如果您没有明确地捕获和检查异常,运行时可能只是打印到 stderr ,表明发生了一些异常,但没有详细信息。

暂无
暂无

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

相关问题 Visual Studio 2015 OpenCV 断言在 cv::imshow windows.cpp 中失败(size.width&gt;0 &amp;&amp; size.height&gt;0) - Visual Studio 2015 OpenCV Assertion failed (size.width>0 && size.height>0) in cv::imshow windows.cpp 复制图像后在imshow中断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0) - Assertion failed (size.width>0 && size.height>0) in imshow after copying image 使用OpenCV断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0)Qt - Assertion failed (size.width>0 && size.height>0) Qt with OpenCV C++,OpenCV,在尝试显示图像时收到此错误“OpenCV(4.3.0)错误:断言失败(size.width&gt;0 &amp;&amp; size.height&gt;0)” - C++, OpenCV, getting this error “OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)” when trying to display image OpenCV 错误:断言失败 (size.width&gt;0 &amp;&amp; size.height&gt;0) 简单代码 - OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code opencv错误:未知函数行261中的断言失败(size.width&gt; 0 &amp;&amp; size.height&gt; 0) - opencv error: assertion failed (size.width>0 && size.height>0) in unknown function line 261 Imshow()大小限制 - Imshow() size restrictions OpenCV cv::Mat、imshow()、cv::VideoCapture 在运行时不工作 - OpenCV cv::Mat , imshow() , cv::VideoCapture is not working at runtime OpenCV cv :: imshow()GUI不显示 - OpenCV cv::imshow() GUI not showing cv :: imshow有时很慢 - cv::imshow sometimes is very slow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM