argc和argv在OpenCV中的int main(int argc,char**argv)中的意义

ven*_*nus 3 c++ opencv argv argc

在以下程序中,用于在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;
}
Run Code Online (Sandbox Code Playgroud)

我无法理解程序员如何指定输入图像.这是因为argv [1]只是一个数组元素,我认为与要指定的图像无关,并且它尚未在程序中的任何位置定义.任何人都可以清楚我的怀疑吗?

还有一件事:检查是否(argc!= 2)的"if"语句中检查了什么?

P0W*_*P0W 6

main( int argc, char** argv )
           |            |
           |            |
           |            +----pointer to supplied arguments
           +--no. of arguments given at command line (including executable name)
Run Code Online (Sandbox Code Playgroud)

示例:

display_image image1.jpg
Run Code Online (Sandbox Code Playgroud)

这里,

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
Run Code Online (Sandbox Code Playgroud)