Reputation: 87
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)?
Upvotes: 3
Views: 12860
Reputation: 73
If you are using Visual Studio
argv[1] corresponds to [Project Properties]->[Configuration Properties]->[Debug]
It can set the imiage name and address
Upvotes: 0
Reputation: 212949
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".
Upvotes: 1
Reputation: 47784
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
Upvotes: 6
Reputation: 1143
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.
Upvotes: 1