Ask Your Question
0

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

asked 2018-04-25 16:53:25 -0600

AndreFeerreira gravatar image

updated 2019-02-12 15:04:22 -0600

LBerger gravatar image

Hi all,

Just installed OpenCV yesterday and I'm trying to play around with it, to eventually work on some fun projects. At the moment I'm struggling to visualize my laptop webcam, and I can't figure out why. This is the code I'm running:

int main() {
    VideoCapture stream1(0);

    if (!stream1.isOpened()) { //check if video device has been initialised
        cout << "cannot open camera";
    }

    //unconditional loop
    while (true) {
        Mat cameraFrame;
        stream1.read(cameraFrame);

        imshow("cam", cameraFrame);
        exit(0);
        if (waitKey(30) >= 0)
            break;
    }

    return 0;
 }

After building the project and running it, this is the error that comes up:

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

Thanks in advance for your help ! :)

edit retag flag offensive close merge delete

Comments

thanks for the edit !

berak gravatar imageberak ( 2018-04-26 03:26:25 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-26 03:30:17 -0600

berak gravatar image

updated 2018-04-26 03:32:39 -0600

your image wasn't valid, and imshow() does not like it at all.

you should check the return value of VideoCapture::read(), like :

bool ok = stream1.read(cameraFrame);
if (! ok)
    continue;

some older webcam models need a "warmup", and might return empty Mat's initially.

also it should rather be:

if (!stream1.isOpened()) { //check if video device has been initialised
    cout << "cannot open camera" << endl;
    return -1;
}

(flush the stream, and return on error. maybe you only missed the msg, because it borked, before it could get printed)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-25 16:53:25 -0600

Seen: 3,765 times

Last updated: Apr 26 '18