Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion failed (size.width>0 && size.height>0)

I'm using Visual Studio Express 2013 with OpenCV 2.4.7, following this tutorial.

I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:

  • the return value of VideoCapture::open is 1

  • extending the waitKey() delay to 50ms and later 500ms

  • setting the dimensions of the window

  • creating another project on 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

Error occurs at the line 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.

With the code

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

we can wait for the camera to start

like image 798
jytoronto Avatar asked Dec 29 '13 01:12

jytoronto


1 Answers

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.
...
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. So hopefully the next input images are not empty.

like image 158
Dennis Avatar answered Sep 22 '22 10:09

Dennis