Introduction to OpenCV2(Using OpenCV with gcc and CMake)
直接放程序,并不是很难:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );//第二个参数确定读取图片后的类型,“1”代表了三通道的彩色图片。
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
建立一个CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
而实际上,大概由于我下载的opencv版本比较新的缘故,且官网的资料了没有及时的更新,编译的时候会报错(需要C++11的支持),需要需要进一步的更改CMakeLists.txt文件,如下所示:
然后使用cmake命令编译:
编译成功后可以直接运行:
运行结果如下图所示: