使用gcc和cmake编译工具编辑opencv例子

1、第一步,建DisplayImage.cpp:

#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("test.jpg");
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
2、第二步,建一个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} )
3、进入到要编译的cpp目录下面,运行:

cd <DisplayImage_directory>
cmake .
make

4、生成一个可执行文件DisplayImage,

$ ./DisplayImage ./

5、显示一张test图片


你可能感兴趣的:(使用gcc和cmake编译工具编辑opencv例子)