3

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)?

4

4 回答 4

6
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
于 2013-09-04T16:57:23.007 回答
1

The program is meant to be called from the command line with one argument which specifies the filename:

$ display_image filename.jpg

In this case the argv[1] will be a pointer to the string "filename.jpg".

于 2013-09-04T16:57:31.507 回答
1

When a user runs the program from a command line interface, they can specify a path to the file after typing the program name: imdisplay image.jpg

argc contains the number of total arguments, including the name of the program. Thus if the user didn't type an image name, then argc is 1. argv is an array of char*s of size argc. so argv[0] is the name of the program as typed by the user, and argc[1] is the first argument.

if (argc != 2) will happen if the user is using the program incorrectly.

于 2013-09-04T16:58:14.813 回答
0

If you are using Visual Studio

argv[1] corresponds to [Project Properties]->[Configuration Properties]->[Debug]

It can set the imiage name and address

于 2018-07-09T08:00:33.520 回答