Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

c++ - Assertion failed (size.width>0 && size.height>0)

I'm using Visual Studio Express 2013 with OpenCV 2.4.7, following this tutorial.

I have spent hours searching the web for solutions, including all of the relevant SO questions. I have tried:

  • the return value of VideoCapture::open is 1

  • extending the waitKey() delay to 50ms and later 500ms

  • setting the dimensions of the window

  • creating another project on Visual C++

  • opening an existing image instead of reading from camera (same error)

but no luck, please help!

Here's my code:

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

int main() {
    Mat image;

    VideoCapture cap;
    int camOpen = cap.open(CV_CAP_ANY);

    namedWindow("window", CV_WINDOW_AUTOSIZE);

    while (true) {
        cap >> image;

        imshow("window", image);

    // delay 33ms
    waitKey(33);        
    }

}

As I compiled and ran it, I got the following error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file ........opencvmoduleshighguisrcwindow.cpp, line 261

Error occurs at the line imshow("window", image);. When I commented it out, there are no complaints.


UPDATES:

A plausible explanation of why this error occured was that my webcam takes time to start, which is why image.empty() is true initially, hence the abort() function was called to exit the program.

With the code

if (!image.empty()) {
    imshow("window", image);
}

we can wait for the camera to start

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tried your code and for me it works (it visualizes the current webcam input)!
I ran it on Visual Studio 2012 Ultimate with OpenCV 2.4.7.
...
The error occurs because the image is empty, so try this:

while (true) {
    cap >> image;

    if(!image.empty()){
        imshow("window", image);
    }

// delay 33ms
waitKey(33);        
}

Maybe the first image you receive from your webcam is empty. In this case imshow will not throw an error. So hopefully the next input images are not empty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...