0

这是我的代码:

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

我得到的错误:

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

在 window.cpp 中,第 261 行是:

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

我解决了我的问题。首先,您可以添加此代码

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

然后在while循环中添加这段代码:

cap.read(image);
4

2 回答 2

3

(size.width>0 && size.height>0)意思是那边有空Mat。在这种情况下,它必须image是空的,因为无法打开相机。检查VideoCapture::open返回值。

于 2013-09-12T13:57:14.843 回答
1
cap.open(0);
Sleep(1000);  // Wait for response of camera, don't forget to #include <windows.h>

切斯

于 2013-10-31T09:21:42.363 回答