Ask Your Question

Loading image

asked 2013-02-12 10:27:14 -0500

project_310 gravatar image

updated 2013-02-12 11:12:21 -0500


I dont understand where exactly do i write the filename or pathname of image in the most basic code , I have tried different versions of code and successsfulyl loaded images the documentation has the following code :

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
    if( argc != 2)
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

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

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.
    waitKey(0);                          // Wait for a keystroke in the window
    return 0;

PLS tell me where do i write the pathname/filename to load my image.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-02-12 10:41:46 -0500

updated 2013-02-12 10:42:58 -0500

Right now your are loading image given as first argument to your program (argv[1]) If you want to load a default image in the code, just replace argv[1] by "c:\\Where\\MyImage\\IS\\image.jpg" or "/home/myself/myimage/is/here.png" for example, in the imread function.

edit flag offensive delete link more


I keep trying that again and again and I still cant load the image using the Mat operator I tried the method via Iplimage one , that works good but after reading documentation it's an old method and the memory management is not good !

So pls help me with this MAT method as it still isnt working , it builds successfully , but shows Command prompt with following line ...

Usage: display_image ImageToLoadAndDisplay

Either give me a different code using Mat operator or help me out with this one code one. I am using all codes from documentation , still they dont work ..serious problems and frustrating !

project_310 gravatar imageproject_310 ( 2013-02-13 05:15:49 -0500 )edit

If you see "Usage: display_image ImageToLoadAndDisplay" that because you don't use argument to launch your program. The code with Mat should work, but you have to define the argument to the command line (see @unxmut answer below to specify it). Take care that under Visual Studio, you have to specify arguments for Debug and Release separately.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-02-13 06:51:28 -0500 )edit

answered 2013-02-12 10:53:20 -0500

unxnut gravatar image

If you are executing from within Visual Studio, there is an option to specify command line arguments in Project Configuration Settings. If you are trying to run it from a command line window, you will be better off specifying the absolute (or relative) path on the command line argument. Make sure you enclose the path in double quotes because of spaces in the path if you have any.

edit flag offensive delete link more


I cant find this setting , can u pls elaborate and explain where to find and activate the above settings?

Sorry for troubling you on this trivial problem , but it's really not working !

project_310 gravatar imageproject_310 ( 2013-02-13 07:45:49 -0500 )edit

It is in: Project->Properties->Debugging->Command Arguments. You can also specify working directory over there just to make sure that your image is in the appropriate directory.

unxnut gravatar imageunxnut ( 2013-02-13 09:01:08 -0500 )edit

Question Tools

1 follower


Asked: 2013-02-12 10:27:14 -0500

Seen: 818 times

Last updated: Aug 21 '13