图像的载入,显示和输出
这篇博客我回谈一谈,如何通过采用OpenCV的库函数将图像载入,显示以及输出。首先,打开C++的IDE编程环境新建一个工程,(必须把OpenCV的环境设置好在IDE下)
///
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, const char** argv )
{
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);
//read the image data in the file "MyPic.JPG" and store it in 'img'
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE);
//create a window with the name "MyWindow"
imshow("MyWindow", img);
//display the image which is stored in the 'img' in the "MyWindow" window
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
//
在运行程序之前,在C++源文件文件夹里放一个叫 (MyPic.JPG) 文件名的图片,否则就需要设置函数imread()的第一个参数为图片的绝对路径。
代码解释
- #include "stdafx.h"
因为是在 Visual Studio下开发,所以需要加上。
- #include "opencv2/highgui/highgui.hpp"
imread(),
namedWindow(),
imshow() and
waitKey() 的定义需要包含以上头文件。
上面定义的
Mat类的对象,
在头文件 "opencv2/core/core.hpp"已经声明。 It is declared in "opencv2/core/core.hpp" header file. 但是代码中为什么没有?因为在