准备:MinGW-w64(记得设置环境变量)、vscode(C/C++插件)、CMake、OpenCV源码

CMake与编译

打开CMake

  1. 指定源码目录(记为srcDir)
  2. 指定编译目录(记为buildDir)
  3. 点击Configure配置,注:”Specify the generator for this project”选”MinGW Makefiles”
  4. 配置中间的列表,根据资料,勾选BUILD_opencv_world,WITH_OPENGL和BUILD_EXAMPLES,不勾选WITH_IPP、WITH_MSMF和ENABLE_PRECOMPILED_HEADERS(如果有的话),CPU_DISPATCH选空。
  5. 检查输出是否有误,无误点击generate生成。
  6. 进入buildDir,打开cmd,用MinGW32-make进行编译,可指定-j N多线程编译
  7. 编译完成,MinGW32-make install安装
  8. 添加路径buildDir/bin到环境变量

注:编译后,buildDir\\install\\x64\\mingw\\bin下的dll被自动转移到buildDir/bin下,所以这里只引用了一个。opencv的头文件在buildDir\\install\\include下,这里不添加到环境变量中。

配置VScode

打开VScode,按ctrl+shift+p打开C/C++: Edit Configurations (JSON),配置c_cpp_properties.json。此json指定includePath是给VScode看的,让其实现代码高亮。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"buildDir\\install\\include",
"buildDir\\install\\include\\opencv2",
// "buildDir\\install\\x64\\mingw\\bin"
// "buildDir\\bin"
],
"defines": [
],
"compilerPath": "C:\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}

**配置launch.jspn**。注意下面的preLaunchTask要与后面tasks.json中的label一致,才会正确启动;program的地址要与后面tasks.json中的args --> -o中的地址一致,因为这一项指定了程序生成的地址,若地址不一样,会找不到。此json主要是指定了程序生成的地址和preLaunchTask,也是给VScode看的。

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
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [{
"name": "opencv dbg",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/dbg/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": false
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": false
}
],
"preLaunchTask": "opencv4.5.1"
}]
}

**配置tasks.json**。这里指定用的是C:\\mingw64\\bin\\g++.exe作为编译器,args中还指定了输出文件地址、头文件地址(-I)、动态库文件地址(-L)、以及动态库。此json指定的编译的必须条件(头文件、库等),是给编译器看的。

注意:若在CMake中勾选了BUILD_opencv_world,那么其实只会生成一个动态库,不需要像这里一样引用那么多个库。并且库的表达方式比较特殊,在buildDir\\install\\x64\\mingw\\bin中,库的文件名称是libopencv_core451.dll之类的以lib开头的.dll文件,但是在指定时,需要指定为-lopencv_core451,即去掉lib & .dll,同时以-l开头(无空格)。

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
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "opencv4.5.1",
"type": "shell",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g", // 生成和调试有关的信息
"${file}",
"-o", "${workspaceFolder}/dbg/${fileBasenameNoExtension}.exe",
"-I",
"buildDir\\install\\include",
"-I",
"buildDir\\install\\include\\opencv2",
"-L",
"buildDir\\bin", // 这个必须有 似乎编译器不会自动从环境变量中查找
"-lopencv_calib3d451",
"-lopencv_core451",
"-lopencv_dnn451",
"-lopencv_features2d451",
"-lopencv_flann451",
"-lopencv_highgui451",
"-lopencv_imgcodecs451",
"-lopencv_imgproc451",
"-lopencv_ml451",
"-lopencv_objdetect451",
"-lopencv_photo451",
"-lopencv_stitching451",
"-lopencv_video451",
"-lopencv_videoio451",
],
"options": {
"cwd": "C:\\mingw64\\bin"
}
}
]
}

如果没有为buildDir\\bin设环境变量,那么执行时还需要把库文件复制一份到程序同级目录下,即dbg目录下(与-o指定目录同级)。所以放到其他电脑上运行时记得复制bin下的动态链接库。

测试

创建一个cpp文件,测试一下运行即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(){
Mat img = Mat::zeros(Size(100, 100), CV_8UC3);
cvtColor(img, img, COLOR_BGR2HSV_FULL);
int bH, bS, bV;
for (int i = 0; i < img.cols; i++){
bH = (float)i/img.cols*255;
bS = 255;
bV = 255;
for (int j = 0; j < img.rows; j++){
bS = (float)(img.rows - j)/img.rows*255;
img(Range(j, j+1), Range(i, i+1)) = Scalar(bH, bS, bV);
}
}
cvtColor(img, img, COLOR_HSV2BGR_FULL);
imwrite("5.jpg", img);
}