OpenCV加载和显示图像

1. 代码及注释

#include 
#include 
#include 
#include "opencv2/imgcodecs/legacy/constants_c.h"	// 报错:未定义标识符CV_LOAD_IMAGE_COLOR,所以需要加上该行
#include 	// 报错:未定义标识符CV_WINDOW_AUTOSIZE,所以需要加上该行

using namespace cv;
using namespace std;

// argc:argument count,表示传入main函数的参数个数
// argv:argument vector,表示传入main函数的参数序列,char** argv等价于char* argv[],argv[0]为exe文件的完整路径
// 传参数给main函数的方法1:调试-属性-调试-命令参数-编辑,参数之间以空格分隔
// 传参数给main函数的方法2:win+r,cmd,输入参数:exe文件的完整路径 参数1 参数2,参数之间以空格分隔
int main(int argc, char** argv)
{
	for (int i = 0; i < argc; ++i)	// for循环输出main函数的所有参数
		cout << argv[i] << endl;
	if (argc != 2)	// main函数需要2个参数,argv[0]为exe文件的完整路径,argv[1]为图像文件的路径
	{
		cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
		return -1;
	}

	Mat image;
	image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file,CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format
	image = imread(argv[1], CV_LOAD_IMAGE_UNCHANGED);	// CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
	image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);	// CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one,灰度图像

	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;
}

2. 参考内容

Load and Display an Image
C++ main函数中参数argc和argv含义及用法

你可能感兴趣的:(opencv)