Ask Your Question

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

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

AndreFeerreira gravatar image

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

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;;

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

    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


thanks for the edit !

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

1 answer

Sort by ยป oldest newest most voted

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

berak gravatar image

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

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 =;
if (! ok)

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
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 999 times

Last updated: Apr 26 '18