首先建立源码文件showPicture.cpp:
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cout << "Hello OpenCV " << CV_VERSION << endl;
// 载入图像
Mat myMat = imread("天.jpg", 1);
// 创建一个窗口
namedWindow("Opencv Image", WINDOW_AUTOSIZE);
// 显示图像
imshow("Opencv Image", myMat);
// 等待按键延时 ms
waitKey(5000);
return 0;
}
然后建立编译文件CMakeLists.txt
# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)
# Define project name 定义工程名
project(example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI 自动查找库
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources 声明可执行目标文件及源文件
add_executable(example showPicture.cpp) # 目标文件,源文件0,源文件1,...
# Link your application with OpenCV libraries 将目标文件与库链接
target_link_libraries(example ${OpenCV_LIBS}) # 目标文件,库路径
$cmeke . 后出现问题:
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
原因:我装opencv的时候没有选择默认路径,所以要手动指定一下
OpenCVConfig.cmake
这个文件。
编译文件这样写:
# cmake needs this line 要求的最低版本
cmake_minimum_required(VERSION 2.8)
# Define project name 定义工程名
project(example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
set(OpenCV_DIR /usr/opencv-3.2.0/build) 手动设置OpenCVConfig.cmake的路径!
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI 自动查找库
find_package(OpenCV REQUIRED)
# Declare the executable target built from your sources 声明可执行目标文件及源文件
add_executable(example showPicture.cpp) # 目标文件,源文件0,源文件1,...
# Link your application with OpenCV libraries 将目标文件与库链接
target_link_libraries(example ${OpenCV_LIBS}) # 目标文件,库路径
接着
$cmake .
$make .
..
成功!