一、在opencv的安装文件夹中创建sample文件夹并进入sample文件夹
二、将自己的图片命名为apple放在sample文件夹中
三、创建编写源文件readimage.cpp
sudo gedit readimage.cpp
在文件中写入
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
string imageName("apple.jpg"); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
image = imread(imageName.c_str(), IMREAD_COLOR); // Read the file
if( image.empty() )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );
imshow( "Display window", image );
waitKey(0);
return 0;
}
四、创建编写CMakeLists.txt
sudo gedit CMakeLists.txt
在文件中写入
cmake_minimum_required(VERSION 2.8)
project( readimage )
find_package( OpenCV REQUIRED )
add_executable( readimage readimage.cpp )
target_link_libraries( readimage ${OpenCV_LIBS} )
五、cmake一下
cmake .
六、编译
make
七、运行
./readimage
哦了~