Ask Your Question
1

OpenCV Error: Assertion Failed (size.width>0 && size.height>0) in unknown function

asked 2013-08-03 10:01:35 -0600

Gil gravatar image

I am new to OpenCV and I followed the first example in <<opencv 2="" computer="" vision="" application="" programming="" cookbook="">> with OpenCV 2.4.6.0, below is its code:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

int main() {

    //read an image
    cv::Mat image = cv::imread("img.jpg");

    //create image window named "My Image"
    cv::namedWindow("My Image");

    //show the image on window
    cv::imshow("My Image", image);

    //wait key for 5000ms
    cv::waitKey(5000);

    return 1;
}

The code could be compiled successfully and could be run by clicking the executable file in debug folder. But if I press the "F5" key to debug the project, it shows me this error in command windows when the code line runs to 'cv::imshow("My Image", image);' :

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown function, file C:\opencv\modules\highgui\src\window.cpp, line 261

Finally I found if I change

cv::Mat image = cv::imread("img.jpg");

to

cv::Mat image = cv::imread("E:\\OpenCV\\OpenCVProjects\\myFirstOpenCVConsoleApp\\Debug\\img.jpg");

, no error exists. It seems it's a matter with the path of the image file, but I don't want to use absolute path here, could any one tell me what the wrong is with the path? Where could place the img.jpg would be right? Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-08-04 00:52:59 -0600

vinayverma gravatar image

updated 2013-08-04 00:54:11 -0600

I believe you are using Visual Studio. You need to put the file in your project directory (i.e. $(ProjectDir)).

Note that this is different from solution directory. In simple words, it is the directory where your project code files are added by default (When you click Add->New Item).

Hope this helps.

edit flag offensive delete link more

Comments

neither placing the image in 'project' nor specifying the whole address didn't rectified that error !

Jagajeet Kuppala gravatar imageJagajeet Kuppala ( 2017-11-06 14:46:39 -0600 )edit

Question Tools

Stats

Asked: 2013-08-03 10:01:35 -0600

Seen: 59,353 times

Last updated: Aug 04 '13