user2772836
user2772836

Reputation: 79

opencv error: assertion failed (size.width>0 && size.height>0) in unknown function line 261

This is my code:

#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;

int main(){
    //create matrix to store image
    Mat image;

    //initialize capture    
    VideoCapture cap(0);
    cap.open(0);

    //create window to show image   
    namedWindow("window",1);

    while(1){   
        //copy webcam stream to image
        cap>>image;

        //print image to screen     
        imshow("window",image); //Error line

        //delay 33ms
        waitKey(33);
    }
}

The error I get:

opencv error: assertion failed (size.width>0 && size.height>0) in unknown function
file...\opencv\modules\highgui\src\window.cpp line 261

In window.cpp, line 261 is:

CV_Assert(size.width>0 && size.height>0);

I solved my problem. firstly, you can add this code

VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480); 

then add this code in while loop:

cap.read(image);

Upvotes: 0

Views: 17766

Answers (2)

Andrzej Besz
Andrzej Besz

Reputation: 11

cap.open(0);
Sleep(1000);  // Wait for response of camera, don't forget to #include <windows.h>

chears

Upvotes: 1

ChronoTrigger
ChronoTrigger

Reputation: 8617

(size.width>0 && size.height>0) means that there is an empty Mat over there. In this case, it must be that image is empty because the camera couldn't be opened. Check VideoCapture::open return value.

Upvotes: 3

Related Questions