Unable to load an image - Open CV not working with Visual Studio2019

asked 2019-06-20 07:53:31 -0600

updated 2019-07-01 22:51:34 -0600

supra56 gravatar image

Hi guys,

I have just set up open cv4.1.0 with visual studio 2019 on windows 10. My simple code for loading the image is as under. Code:

int main()
    Mat imag;

       // LOAD image
       imag = imread("D:\\image2.png");   // Read the file "image.jpg".
              //This file "image.jpg" should be in the project folder.
              //Else provide full address : "D:/images/image.jpg"

       if(imag.empty() )  // Check for invalid inputf
              cout <<  "Could not open or find the image" << std::endl ;
              return -1;

       //DISPLAY image
       namedWindow("Window1", WINDOW_AUTOSIZE); // Create a window for display.
       imshow("Window1", imag); // Show our image inside it.

       //SAVE image
       imwrite("result1.jpg",imag);// it will store the image in name "result.jpg"

       waitKey(5000);                       // Wait for a keystroke in the window
       return 0;

When i build this code and run it, an empty image is momentarily displayed and I get this error in the terminal window

"OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file C:\buildslave64\win64_amdocl\master_PackSlave-win64-vc14-shared\opencv\modules\highgui\src\window.cpp, line 281"

I have placed my image in the active directory so i have no clue why it is not being accessed

edit retag flag offensive close merge delete


noobs always fail at relative image paths, it's not visual studio's fault, but yours.

start with an absolute path.

I have placed my image in the active directory

relevant is the dir, where your program starts (e.g. the debug or release folder)

berak gravatar imageberak ( 2019-06-20 08:00:33 -0600 )edit

what means absolute path?

xiejing gravatar imagexiejing ( 2019-06-20 08:11:29 -0600 )edit

^^ please look it up !

berak gravatar imageberak ( 2019-06-20 08:12:08 -0600 )edit

@berak i already tried to put the image in debug folder , but also failed

xiejing gravatar imagexiejing ( 2019-06-20 08:12:58 -0600 )edit

you can also set the working directory from the project's debug settings

berak gravatar imageberak ( 2019-06-20 08:13:14 -0600 )edit

again, try harder. there is no bug, there's just you and the file system.

berak gravatar imageberak ( 2019-06-20 08:14:18 -0600 )edit

@berak I am sorry but can you help me with teamviewer ?

xiejing gravatar imagexiejing ( 2019-06-20 08:16:22 -0600 )edit

no, sorry.

berak gravatar imageberak ( 2019-06-20 08:20:37 -0600 )edit

@berak Ok no problem . so i put the image in the debug folder and then using its absolute path in code, but fails . What should i do in working directory in Project setting ?

xiejing gravatar imagexiejing ( 2019-06-20 08:43:41 -0600 )edit

@berak I have done with all possible options what i can try with , but it fails always, absolute path in code to the image file , also set the debug folder as working directory . wrong bad. What is the problem ? can you help me once more ?

xiejing gravatar imagexiejing ( 2019-06-21 03:41:47 -0600 )edit