Introduction to OpenCV3(Load and Display an Image)
直接放代码:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
String imageName( "../build/lena.jpg" ); // by default,稍加修改
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
image = imread( imageName, IMREAD_COLOR ); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", 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;
}
core.hpp定义了library基本组成部分,比如数据结构Mat即来源这个头文件;
highgui.hpp包含了输入和输出;比如这个程序中的namedWindow(),imshow(),以及waitKey()即来源于这个头文件;
imgcodecs.hpp包含了图片的读取和写;比如这个程序中的imread()即来源于这个头文件;
opencv的名称空间为cv;
关于imread函数中第二个参数,我觉得官网解释很棒!
IMREAD_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
IMREAD_GRAYSCALE ( 0) loads the image as an intensity one
IMREAD_COLOR (>0) loads the image in the RGB format
namedWindow( );这个函数定义了窗口的名称,第二个参数表面窗口大小是否可调等,WINDOW_AUTOSIZE表示了图像将占据整个窗口,且不允许调整大小。
imshow();第一个参数表示了显示窗口的名字,第二参数是指显示的图像名称;
waitKey(0);表示了等待用户输入的时间,0表示了值得用户输入为止;
关于CMakeLists.txt文件的事情,这个可以使用上一篇的文件,但需要注意本文程序的名称(直接使用了上个程序名,哈哈哈,比较懒);
运行结果如下: