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

筅森魡賤 提交于 2019-12-08 13:34:41

问题


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

回答1:


(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.




回答2:


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

chears



来源:https://stackoverflow.com/questions/18766025/opencv-error-assertion-failed-size-width0-size-height0-in-unknown-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!