Ubuntu OpenCV CMake

1.Building packages

  1. update packages list
1
sudo apt update
  1. Install building packages
1
sudo apt -y install build-essential

2.Install OpenCV from Ubuntu Repositories

link

  1. Install OpenCV
1
sudo apt -y install libopencv-dev
  1. Check the OpenCV version
1
dpkg -l libopencv-dev

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
// 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(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

test

7.VSCode Debug

  • Install Extensions

install_Extensions

  • restart and set cmake

set_cmake

  • add breakpoint and click Debug button

breakpoint

  • Debugging

run_debug

  • set pic path for main args

please tell me…