3

I can't understand, whats the function of the code in OpenCV for loading a image. Whats the function of if(argc !=2)? Can you tell me about it.

 if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

Full Code:

1 #include <opencv2/core/core.hpp>
2 #include <opencv2/highgui/highgui.hpp>
3 #include <iostream>
4
5 using namespace cv;
6 using namespace std;
7
8 int main( int argc, char** argv )
9 {
10 if( argc != 2)
11 {
12 cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
13 return -1;
14 }
15
16 Mat image;
17 image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
18
19 if(! image.data ) // Check for invalid input
20 {
21 cout << "Could not open or find the image" << std::endl ;
22 return -1;
23 }
24
25 namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
26 imshow( "Display window", image ); // Show our image inside it.
27
28 waitKey(0); // Wait for a keystroke in the window
29 return 0;
30 }
Deanie
  • 2,316
  • 2
  • 19
  • 35
Dejan
  • 105
  • 2
  • 3
  • 6

2 Answers2

24

This should be covered in any tutorial for C++ (or C, ObjC, or related languages), such as the GNU tutorial.

The main function of a C++ program has two parameters, by convention named argc and argv, which give it the command-line arguments used to launch the program.

argc is the count of arguments, and argv is an array of the strings.

The program itself is the first argument, argv[0], so argc is always at least 1.

So, argc is 2 when the program is run with one command-line argument. If it's run with no arguments, or more than one, the argc != 2 will be true, so the usage message " Usage: display_image ImageToLoadAndDisplay" will be printed, telling the user how to run it properly.


For example, if you do this:

$ display_image firstarg "second arg"

The values will be:

argc: 3
argv[0]: "display_image"
argv[1]: "firstarg"
argv[2]: "second arg"

It may be worth pointing out that the code is weird in many ways. The extra space at the beginning of the usage message is very weird. "Usage" is usually all in lowercase. You typically put the actual program name (argv[0]) in the string rather than a hardcoded canonical name. Usage messages usually go to cerr, not cout. And the convention is to return a positive number for errors that are the user's fault, typically 2 for invalid arguments, not -1. You can find better examples of argc/argv handling in the source code to almost any tool made to be used on the Unix command line (although most of them are more complicated, often using libraries like getopt to parse out options from file arguments, etc.).

abarnert
  • 354,177
  • 51
  • 601
  • 671
3
  • argc = Arguement count. This is used to determine the number of command line arguements a program is run with.
  • argv is called the arguement vector which holds the names of all command line arguements including the name of your program.
  • On any program you run, argc is always atleast 1; and this is because the program itself is included in the arguement count. So in your program, argc is required to be 2 which consists of: your_program and another_file. If argc is not 2, this means it is equal to 1 or it is greater than 2 and since this is not what the program wants, the code aborts further execution
smac89
  • 39,374
  • 15
  • 132
  • 179