OpenCV4入门系列教程3:编译源码

系列索引:系列索引地址

上一篇:OpenCV4入门系列教程2:模块介绍

编译

入门OpenCV开发的第一步是进行编译,如果你不需要太个性化的配置可以直接跳过本部分使用官方安装包或者Linux发行版仓库的软件包,如果你需要CUDA支持等等可以继续。

下载源码

自己编译源码的话需要下载两部分源码,一个是官方主仓库,另一个是contrib仓库。

主仓库

1
git clone https://github.com/opencv/opencv

如果国内下载太慢可以考虑去码云中下载。

1
git clone https://gitee.com/mirrors/opencv

contrib仓库中包含待测试的代码、没有版权的算法等等

1
git clone https://github.com/opencv/opencv_contrib

同样的,码云地址

1
git clone https://gitee.com/mirrors/opencv_contrib

主仓库大约100M左右,contrib仓库大约50M。

配置

使用cmake来配置生成编译需要的MakeFile文件。

根据个人需要,配置之前安装Intel mkl tbb等库。

然后打开cmake工具进行配置,选择附加模块,支持cuda等等。

我的配置结果如下

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
General configuration for OpenCV 4.3.0-pre =====================================
Version control: 4.2.0-568-g95a07ca11c

Extra modules:
Location (extra): /home/jackey/Documents/opencv_contrib/modules
Version control (extra): 4.2.0-62-g96b38d4a

Platform:
Timestamp: 2020-03-30T09:41:49Z
Host: Linux 5.5.13-1-MANJARO x86_64
CMake: 3.17.0
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: Release

CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (16 files): + SSSE3 SSE4_1
SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (30 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (6 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: /usr/bin/c++ (ver 9.3.0)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed
ccache: YES
Precompiled headers: NO
Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/opt/cuda/lib64 -L/lib64
3rdparty dependencies:

OpenCV modules:
To be built: alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform java line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz xfeatures2d ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: -
Unavailable: cnn_3dobj cvv hdf js matlab ovis python2 sfm
Applications: tests perf_tests examples apps
Documentation: doxygen javadoc
Non-free algorithms: NO

GUI:
GTK+: YES (ver 3.24.14)
GThread : YES (ver 2.64.1)
GtkGlExt: NO
VTK support: YES (ver 8.2.0)

Media I/O:
ZLib: /lib64/libz.so (ver 1.2.11)
JPEG: /lib64/libjpeg.so (ver 80)
WEBP: /lib64/libwebp.so (ver encoder: 0x020f)
PNG: /lib64/libpng.so (ver 1.6.37)
TIFF: /lib64/libtiff.so (ver 42 / 4.1.0)
JPEG 2000: OpenJPEG (ver 2.3.1)
OpenEXR: /usr/lib/libImath-2_4.so /usr/lib/libIlmImf-2_4.so /usr/lib/libIex-2_4.so /usr/lib/libHalf-2_4.so /usr/lib/libIlmThread-2_4.so (ver 2_4)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: YES (2.2.6)
FFMPEG: YES
avcodec: YES (58.54.100)
avformat: YES (58.29.100)
avutil: YES (56.31.100)
swscale: YES (5.5.100)
avresample: YES (4.0.0)
GStreamer: YES (1.16.2)
v4l/v4l2: YES (linux/videodev2.h)

Parallel framework: pthreads

Trace: YES (with Intel ITT)

Other third-party libraries:
Intel IPP: 2020.0.0 Gold [2020.0.0]
at: /home/jackey/Documents/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
Intel IPP IW: sources (2020.0.0)
at: /home/jackey/Documents/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
Lapack: YES (/opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so /opt/intel/mkl/lib/intel64/libmkl_sequential.so /opt/intel/mkl/lib/intel64/libmkl_core.so /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so /opt/intel/mkl/lib/intel64/libmkl_sequential.so /opt/intel/mkl/lib/intel64/libmkl_core.so /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.so /opt/intel/mkl/lib/intel64/libmkl_sequential.so /opt/intel/mkl/lib/intel64/libmkl_core.so -lpthread -lm -ldl)
Eigen: YES (ver 3.3.7)
Custom HAL: NO
Protobuf: build (3.5.1)

NVIDIA CUDA: YES (ver 10.2, CUFFT CUBLAS)
NVIDIA GPU arch: 30 35 37 50 52 60 61 70 75
NVIDIA PTX archs:

cuDNN: YES (ver 7.6.5)

Vulkan: YES
Include path: /home/jackey/Documents/opencv/3rdparty/include
Link libraries: Dynamic load

OpenCL: YES (no extra features)
Include path: /home/jackey/Documents/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 3:
Interpreter: /usr/bin/python3 (ver 3.8.2)
Libraries: /lib64/libpython3.8.so (ver 3.8.2)
numpy: /usr/lib/python3.8/site-packages/numpy/core/include (ver 1.18.2)
install path: lib/python3.8/site-packages/cv2/python-3.8

Python (for build): /usr/bin/python3

Java:
ant: /bin/ant (ver 1.10.7)
JNI: /usr/lib/jvm/default/include /usr/lib/jvm/default/include/linux /usr/lib/jvm/default/include
Java wrappers: YES
Java tests: YES

Install to: /home/jackey/Documents/opencv_dev
-----------------------------------------------------------------

Configuring done
Generating done

然后执行

1
make -j20

编译时间根据配置不同而不同,我的时间是17:49~18:53共计64分钟。

安装时间为3分钟。

OpenCV代码每天都在更新,就不提供结果文件下载了。

作为开发者,编译自己想要的库是必备的技能,如果是新手只需要使用官方打包好的就可以了。

下一篇:OpenCV4入门系列教程3.3:Win10+Qt 5.12.3+mingw7.3.0编译OpenCV 4.1.0