OpenCVCpp

Murtaza Hassan

4h上手C++版Opencv_哔哩哔哩_bilibili

数字图像处理(c++ opencv)入门学习 - 知乎 (zhihu.com)

Installing OpenCV on Windows for C++

Step 1 Download and Install OpenCV

Downloading and Installing OpenCV: Releases - OpenCV

2024年1月11日,最新版的OpenCV是4.9.0,我下载了的版本是OpenCV-4.8.0。下载解压到一个文件夹中。

1
2
3
4
5
6
7
8
9
(base) PS D:\Dev\opencv480> dir
目录: D:\Dev\opencv480
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/1/11 14:14 build
d----- 2024/1/11 14:14 sources
-a---- 2023/6/28 20:57 11560 LICENSE.txt
-a---- 2023/6/28 20:57 27913 LICENSE_FFMPEG.txt
-a---- 2023/6/28 20:57 868 README.md.txt

Step 2 Environment Variables

Add bin folder to Environment Variables path: D:\Dev\opencv480\build\x64\vc16\bin, then restart the PC.s

image-20240111171607051

Step3 New Visual Studio Project

  1. Best VS setting(Cherno)
  • Output Directory: $(SolutionDir)bin$(Platform)$(Configuration)\
  • Intermediate Directory: $(SolutionDir)bin\intermediate$(Platform)$(Configuration)\

D:\userCode\IntroOpenCV\bin\Win32\Debug\

D:\userCode\IntroOpenCV\bin\intermediate\Win32\Debug\

image-20240111172341038

  1. Set the platform target to x64

image-20240111172118484
on

Step 4 OpenCV configuration

Add Directories by going to Project-Properties-Configuration Properties

1
2
3
4
5
6
7
VC++ Directories
1. Add Build Directories: D:\Dev\opencv480\build\include
2. Add Library Directories: D:\Dev\opencv480\build\x64\vc16\lib
Linker Input
1. Add Linker input: opencv_world480d.lib
** d(opencv_world480d.lib) for debug
** without d(opencv_world480.lib) for release

image-20240111181709454

image-20240111181842738

Step 5 Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <string>

int main()
{
std::string path = "./Resources/test.png";
cv::Mat img = cv::imread(path);
cv::imshow("Image", img);
cv::waitKey(0);
return 0;
}

Using VSCode and OpenCV in Windows

Installing OpenCV 4.8.0 on Ubuntu 20.04 for C++

reference:

Source code downloading

  1. Download opencv-4.8.0.zip Sources in https://opencv.org/releases/ or https://github.com/opencv/opencv/releases/tag/4.8.0
  2. Download opencv_contrib-4.8.0.zip Sources in https://github.com/opencv/opencv_contrib/releases/tag/4.8.0
  3. unzip opencv-4.8.0.zip && unzip opencv_contrib-4.8.0.zip

build

  1. build opencv
1
2
3
4
5
6
7
8
9
10
11
12
# 先安装依赖环境
npu481@npu481-H310M:~/userdoc/mydownload$ sudo apt-get install build-essential
npu481@npu481-H310M:~/userdoc/mydownload$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

# 编译opencv
npu481@npu481-H310M:~/userdoc/mydownload$ mkdir -p build && cd build

npu481@npu481-H310M:~/userdoc/mydownload/build$ cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_ENABLE_NONFREE=True -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.8.0/modules ../opencv-4.8.0

npu481@npu481-H310M:~/userdoc/mydownload/build$ sudo make -j6 # 6表示运行命令的个数

npu481@npu481-H310M:~/userdoc/mydownload/build$ sudo make install # 编译成功后,在build目录下执行安装命令,等待安装完成
1
2
3
4
5
6
- CMAKE_INSTALL_PREFIX 是opencv的安装地址 默认安装在 `/usr/local`
- CMAKE_BUILD_TYPE 是opencv安装的版本,Release和Debug两种可选,默认安装Release
- OPENCV_ENABLE_NONFREE 是否使用部分被申请了专利的算方法 这里选True的话就可以使用了
- OPENCV_GENERATE_PKGCONFIG 强烈建议开启这个 设置为ON。OPENCV_GENERATE_PKGCONFIG因为opencv4默认不生成.pc文件,所以加上这句用于生成opencv4.pc文件,支持pkg-config功能。opencv4版本及以上,这里用ON。OpenCV安装后的各种头文件和库文件的路径信息都在这个配置文件中。
- OPENCV_EXTRA_MODULES_PATH: opencv_contrib-4.8.0/modules
- `../opencv-4.8.0`: 制定OpenCV源文件目录
1
2
3
4
5
6
7
8
- `/usr/local`: 安装位置
- `/usr/local/bin`: 可执行文件
- `/usr/local/lib`: 库文件(.so)
- `/usr/local/lib/cmake/opencv4`: camke包
- `/usr/local/lib/pkgconfig`: opencv4.pc的位置
- `/usr/local/include/opencv4/opencv2`: 头文件
- `/usr/local/share/opencv4`: 其他文件
- `/usr/local/share/licenses/opencv4`: 其他文件
  1. environment config
  • 修改etc/bash.bashrc

我们之前安装的时候 OPENCV_GENERATE_PKGCONFIG=ON 所以在/usr/local/lib/pkgconfig下生成了opencv4.pc文件,里面记录了OpenCV头文件、库文件的路径。需要进行如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
npu481@npu481-H310M:~/userdoc/mydownload/build$ sudo find / -iname opencv4.pc
/usr/local/lib/pkgconfig/opencv4.pc


npu481@npu481-H310M:~/userdoc/mydownload/build$ sudo gedit /etc/bash.bashrc

#在bash.bashrc文件末尾添加以下内容 并保存
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

#更新
npu481@npu481-H310M:~/userdoc/mydownload/build$ source /etc/bash.bashrc
  • check
1
2
3
4
5
npu481@npu481-H310M:~/userdoc/mydownload/build$ pkg-config --modversion opencv4
4.8.0

npu481@npu481-H310M:~/userdoc/mydownload/build$ pkg-config --libs opencv4
-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
  • 配置OpenCV的动态库环境
1
2
3
4
5
6
7
npu481@npu481-H310M:~/userdoc/mydownload/build$ sudo gedit /etc/ld.so.conf.d/opencv.conf

# 添加lib路径 在 末尾 保存退出
/usr/local/lib

# 刷新动态库环境
npu481@npu481-H310M:~/userdoc/mydownload/build$ sudo ldconfig
  1. 测试OpenCV
1
2
3
4
5
npu481@npu481-H310M:~/userdoc/mydownload$ cd opencv-4.8.0/samples/cpp/example_cmake/
npu481@npu481-H310M:~/userdoc/mydownload/opencv-4.8.0/samples/cpp/example_cmake$ mkdir build && cd build
npu481@npu481-H310M:~/userdoc/mydownload/opencv-4.8.0/samples/cpp/example_cmake/build$ cmake ..
npu481@npu481-H310M:~/userdoc/mydownload/opencv-4.8.0/samples/cpp/example_cmake/build$ make
npu481@npu481-H310M:~/userdoc/mydownload/opencv-4.8.0/samples/cpp/example_cmake/build$ ./opencv_example

VSCode中配置OpenCV

reference: https://zhuanlan.zhihu.com/p/363035298

tasks.json

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
31
32
33
34
35
36
37
38
39
40
41
42
43
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "g++",
"args": [
"-g",
"-std=c++11", // 指定C++版本
"${file}",
"-o",
"${workspaceFolder}/main",
"`pkg-config", "--cflags", "--libs", "opencv4`",
"-I", "/usr/local/include",
"-I", "/usr/local/include/opencv4",
"-I", "/usr/local/include/opencv4/opencv2",
"-L", "/usr/local/lib",
"-l", "opencv_core",
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio"
],
"options": {
"cwd": "/usr/bin"
},
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0",
}

c_cpp_properties.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}

launch.json

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
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
// "cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

OpenCVCpp
https://cosmicdusty.cc/post/Knowledge/OpenCVCpp/
作者
Murphy
发布于
2024年1月11日
许可协议