Significance of argc and argv in int main( int argc, char** argv ) in OpenCV

venus picture venus · Sep 4, 2013 · Viewed 12.3k times · Source

In the following program for loading and displaying image in openCV

#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(! image.data )                              // 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;
}

I am not able to understand how the programmer is specifying the input image. This is because argv[1] is just an array element and I think has no relation to the image to be specified, and it hasn't been defined anywhere in the program. Can anybody clear my doubts?

One more thing: What is being checked in the "if" statement that checks if(argc !=2)?

Answer

P0W picture P0W · Sep 4, 2013
main( int argc, char** argv )
           |            |
           |            |
           |            +----pointer to supplied arguments
           +--no. of arguments given at command line (including executable name)

Example :

display_image image1.jpg

Here,

argc will be 2
argv[0] points to display_image
argv[1] points to image1

if(argc !=2 )
   ^^ Checks whether no. of supplied argument is not exactly two