OpenCV学习笔记一 (2016.12.3)

1、imread函数

函数定义:Mat imread(const string& filename, int flags=1 );

第一个参数为图像的路径,第二个参数flags为载入标识,指定一个加载图像的颜色类型,缺省为1,此时载入三通道的彩色图像。

flags取值:

CV_LOAD_IMAGE_ANYDEPTH- 如果取这个标识的话,若载入的图像的深度为16位或者32位,就返回对应深度的图像,否则,就转换为8位图像再返回。     

CV_LOAD_IMAGE_COLOR- 如果取这个标识的话,总是转换图像到彩色一体。

CV_LOAD_IMAGE_GRAYSCALE- 如果取这个标识的话,始终将图像转换成灰度1

如果想要载入最真实的图像,选择CV_LOAD_IMAGE_ANYDEPTH 或者 CV_LOAD_IMAGE_ANYCOLOR。

OpenCV学习笔记一 (2016.12.3)_第1张图片


2、namedWindow函数

函数定义:void namedWindow(const string& winname,int flags=WINDOW_AUTOSIZE );  用于创建一个窗口。

    第一个参数,const string&型的name,即填被用作窗口的标识符的窗口名称。
    第二个参数,int 类型的flags ,窗口的标识,可以填如下的值:
    WINDOW_NORMAL设置了这个值,用户便可以改变窗口的大小(没有限制)
    WINDOW_AUTOSIZE如果设置了这个值,窗口大小会自动调整以适应所显示的图像,并且不能手动改变窗口大小。

3.imshow函数

函数定义:void imshow(const string& winname, InputArray mat); 在指定的窗口(winname)中显示一幅图像。

第一个参数,const string&类型的winname,填需要显示的窗口标识名称。

第二个参数,InputArray 类型的mat,填需要显示的图像。

4.waitKey函数

waitKey有两个作用:
    1. It waits for x milliseconds for a key press.If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.(等待X毫秒的关键字输入,如果在此期间输入了关键词,那么返回关键词的ASCII码,否则结束返回-1)
    2.It handles any windowing events, such as creating windows with cv::namedWindow(), or showing images with cv::imshow().(处理所有的窗口时间,例如使用namedWindow()函数创建一个窗口或者使用imshow()函数显示一副图像)


//版本一 VS2013 OpenCV2.4.13
//CMD命令调试:HelloOpenCV.exe  lena.jpg

#include
#include
#include

using namespace cv;
using namespace std;

int main(int argc,char** argv){

	//判断程序启动命令是否为二
	if (argc!=2){
		cout << "Usage:Read_ImageToLoadAndDisplay" << endl;
		return -1;
	}

	//定义Mat类型的图像变量image
	Mat image;
	//读取图像
	//函数定义:Mat imread(const string& filename, int flags=1 );
	//flags为载入标识,指定一个加载图像的颜色类型,缺省为1。
	image = imread(argv[1]);

	//检查图像输入是否有效
	if (!image.data){
		cout << "could not open or find the image" << endl;
		return -1;
	}

	//创建一个窗口用于显示图像
	//函数定义void namedWindow(const string& winname,int flags=WINDOW_AUTOSIZE );   
	namedWindow("Display window",WINDOW_AUTOSIZE);
	
	//图像显示
	/*void imshow(const char *name, const CvArr *img)  
	{
		cvNamedWindow(name, CV_WINDOW_AUTOSIZE);
		cvShowImage(name, img);
		cvWaitKey(0);
		cvDestroyWindow(name);
	}*/
	imshow("Display window",image);

	//waitKey有两个作用:
	//1. It waits for x milliseconds for a key press.If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.
	//2.It handles any windowing events, such as creating windows with cv::namedWindow(), or showing images with cv::imshow().
	waitKey(0);

	return 0;
}

//版本二
//VS调试

#include
#include
#include

using namespace cv;
using namespace std;

int main(int argc, char** argv){

	Mat image;
	image = imread("F:/OpenCV_Stydy_WorkSpace/lena.jpg");

	if (!image.data){
		cout << "could not open or find the image" << endl;
		return -1;
	}
  
	namedWindow("Display window", WINDOW_AUTOSIZE);
	imshow("Display window", image);
	waitKey(0);

	return 0;
}

本人初学OpenCV,C++编程亦很不熟练,代码尚有不完善的地方,敬请指教!

5、输出图像到文件imwrite函数

 函数定义:bool imwrite(const string& filename,InputArray img, const vector& params=vector() );  

第一个参数,const string&类型的filename,填写将图像写入文件的路径及图像的名称和后缀。

第二个参数,InputArray类型的img,一般填一个Mat类型的图像数据就行,即将要输出到文件中的图像数据。

第三个参数,const vector&类型的params,表示为特定格式保存的参数编码,它有默认值vector(),所以一般情况下不需要填写。

#include"opencv2/opencv.hpp"
#include

using namespace cv;
using namespace std;

int main(int argc, char** argv){

	Mat image;
	image = imread("F:/OpenCV_Stydy_WorkSpace/lena.jpg");

	if (!image.data){
		cout << "could not open or find the image" << endl;
		return -1;
	}
	
	//Canny提取边缘,并将结果存储与result
	Mat result;
	Canny(image,result,50,150);

	imshow("Lene_Canny",result);
	imwrite("lena_Canny.png",result);
	
	waitKey(0);
	return 0;
}

OpenCV学习笔记一 (2016.12.3)_第2张图片


你可能感兴趣的:(初窥OpenCV,opencv,C++)