Ubuntu OpenCV CMake
1.Building packages
- update packages list
- Install building packages
1
| sudo apt -y install build-essential
|
2.Install OpenCV from Ubuntu Repositories
link
- Install OpenCV
1
| sudo apt -y install libopencv-dev
|
- Check the OpenCV version
3.Install CMake
1 2
| sudo apt -y install cmake cmake --version
|
4.Create CPP Projetct
link
1 2 3 4 5 6 7 8 9 10
| DisplayImage ├── build │ └── ├── CMakeLists.txt ├── cmake-modules │ └── OpenCVConfig.cmake ├── img │ └── lenna.jpeg └── src └── DisplayImage.cpp
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#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], IMREAD_COLOR);
if (!image.data) { printf("No image data \n"); return -1; } namedWindow("Display Image", WINDOW_AUTOSIZE); imshow("Display Image", image);
waitKey(0);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # CMakeLists.txt
# CMake 最低版本号要求 cmake_minimum_required(VERSION 3.6)
# 项目信息 project(DisplayImage)
# 将cmake-modules文件夹中的文件加入 CMAKE_MODULE_PATH list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake-modules")
# 查找并从外部项目加载设置 # REQUIRED如果找不到软件包,该选项将停止处理并显示一条错误消息 find_package(OpenCV REQUIRED)
# 将给定目录添加到编译器用来搜索包含文件的目录中。相对路径被解释为相对于当前源目录 include_directories(${OpenCV_INCLUDE_DIRS})
# 利用源码文件生成目标可执行程序 add_executable(DisplayImage src/DisplayImage.cpp)
# 添加链接库 target_link_libraries(DisplayImage ${OpenCV_LIBS})
|
5.Find OpenCV Config
link
1
| sudo find / -name "OpenCVConfig.cmake"
|
copy to cmake-modules folder
6.Build and Test
1 2 3 4
| cd build cmake .. make ./DisplayImage ../img/lenna.jpeg
|
maybe need chmod +x DisplayImage
7.VSCode Debug
- add breakpoint and click Debug button
- set pic path for main args
please tell me…