18
votes

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
Not to be a stickler or anything, but if you put namedWindow("xyz", CV_WINDOW_AUTOSIZE), it will be much easier for you (and later, others) to read and follow.. Coming to you issue, could put breakpoints after every line and tell me where exactly the issue is occurring..? - scap3y
edited question as requested, let me know if any other additional information would be helpful - jytoronto
had put "xyz" as an example. For this example, you should be putting "window" (t is the name of the window and hence should be same in namedWindow and imshow. - scap3y
Check your webcam settings , whether it works . If you use Skype or other webcam software, check whether it is working . - motiur

3 Answers

16
votes

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.

0
votes

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;
}
0
votes
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.