Ask Your Question

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, 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

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


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

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


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


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

Seen: 58,726 times

Last updated: Aug 04 '13