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

Viewed 53283

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

3 Answers
Related