[记] OpenCV4 源码编译安装 | 记录
OpenCV4 源码编译安装 | 记录
参考资料
环境
- wsl2-ubuntu20.04
- opencv4.5.5
步骤
-
安装GCC \ CMake \ make
sudo apt install -y g++ sudo apt install -y cmake sudo apt install -y make
-
下载源码
github地址:https://github.com/opencv
opencv官网:https://opencv.org/releases/
注:opencv_contrib应与opencv版本号相同
-
编译安装
注:可将opencv_contrib包放置在opencv文件夹内。
创建build文件夹:
cd opencv<> mkdir build && cd build
配置cmake:
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_C_COMPILER=/usr/bin/gcc \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.5/modules/ \ -D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so \ -D CUDA_ARCH_BIN=7.0 \ -D CUDA_ARCH_PTX="" \ -D WITH_CUDA=ON \ -D WITH_TBB=ON \ -D WITH_FFMPEG=ON \ -D BUILD_PYTHON_SUPPORT=ON \ -D BUILD_NEW_PYTHON_SUPPORT=ON \ -D BUILD_OPENCV_PYTHON3=ON \ -D WITH_V4L=ON \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D BUILD_EXAMPLES=ON \ -D WITH_QT=ON \ -D WITH_GSTREAMER=ON \ -D WITH_OPENGL=ON \ -D ENABLE_FAST_MATH=1 \ -D CUDA_FAST_MATH=1 \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_PC_FILE_NAME=opencv.pc \ -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.0 \ -D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \ -D WITH_CUBLAS=ON \ -D WITH_NVCUVID=ON \ -D BUILD_opencv_cudacodec=ON \ -D OPENCV_DNN_CUDA=ON \ -D WITH_CUDNN=ON \ -D OPENCV_ENABLE_NONFREE=ON\ -D WITH_GSTREAMER=ON \ -D PYTHON3_INCLUDE_DIR=/usr/include/python3.8 \ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \ -D BUILD_EXAMPLES=ON ..
以上为配置参数示例,其中
OPENCV_EXTRA_MODULES_PATH
为opencv_contrib
文件夹路径。注:对于
opencv4.5.5
版本对于python
的支持,建议增加-D PYTHON3_INCLUDE_DIR=/usr/include/python3.8
,否则可能出现无法import cv2
状况,具体原因未深究。配置完成结果实例如下:
-- General configuration for OpenCV 4.5.5 ===================================== -- Version control: unknown -- -- Extra modules: -- Location (extra): /home/fish/opencv/opencv-4.5.5/opencv_contrib-4.5.5/modules -- Version control (extra): unknown -- -- Platform: -- Timestamp: 2022-05-01T11:37:44Z -- Host: Linux 5.10.102.1-microsoft-standard-WSL2 x86_64 -- CMake: 3.16.3 -- 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 (18 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 (33 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 -- AVX512_SKX (8 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.4.0) -- C++ flags (Release): -fsigned-char -ffast-math -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 -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 -ffast-math -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 -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/gcc -- C flags (Release): -fsigned-char -ffast-math -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 -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 -ffast-math -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 -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: NO -- Precompiled headers: NO -- Extra dependencies: m pthread cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda-11.0/lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/local/cuda/lib64 -- 3rdparty dependencies: -- -- OpenCV modules: -- To be built: aruco barcode 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 line_descriptor mcc 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 wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto -- Disabled: world -- Disabled by dependency: - -- Unavailable: alphamat cvv hdf java julia matlab ovis python2 sfm viz -- Applications: tests perf_tests examples apps -- Documentation: NO -- Non-free algorithms: YES -- -- GUI: GTK3 -- QT: NO -- GTK+: YES (ver 3.24.20) -- GThread : YES (ver 2.64.6) -- GtkGlExt: NO -- OpenGL support: NO -- VTK support: NO -- -- Media I/O: -- ZLib: /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11) -- JPEG: /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80) -- WEBP: build (ver encoder: 0x020f) -- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37) -- TIFF: /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0) -- JPEG 2000: build (ver 2.4.0) -- OpenEXR: build (ver 2.3.0) -- HDR: YES -- SUNRASTER: YES -- PXM: YES -- PFM: YES -- -- Video I/O: -- DC1394: NO -- FFMPEG: YES -- avcodec: YES (58.54.100) -- avformat: YES (58.29.100) -- avutil: YES (56.31.100) -- swscale: YES (5.5.100) -- avresample: NO -- GStreamer: NO -- 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/fish/opencv/opencv-4.5.5/build/3rdparty/ippicv/ippicv_lnx/icv -- Intel IPP IW: sources (2020.0.0) -- at: /home/fish/opencv/opencv-4.5.5/build/3rdparty/ippicv/ippicv_lnx/iw -- VA: NO -- Lapack: NO -- Eigen: NO -- Custom HAL: NO -- Protobuf: build (3.19.1) -- -- NVIDIA CUDA: YES (ver 11.0, CUFFT CUBLAS FAST_MATH) -- NVIDIA GPU arch: 70 -- NVIDIA PTX archs: -- -- cuDNN: YES (ver 8.0.5) -- -- OpenCL: YES (no extra features) -- Include path: /home/fish/opencv/opencv-4.5.5/3rdparty/include/opencl/1.2 -- Link libraries: Dynamic load -- -- Python 3: -- Interpreter: /usr/bin/python3 (ver 3.8.10) -- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10) -- numpy: /home/fish/.local/lib/python3.8/site-packages/numpy/core/include (ver 1.22.3) -- install path: /usr/lib/python3/dist-packages/cv2/python-3.8 -- -- Python (for build): /usr/bin/python2.7 -- -- Java: -- ant: NO -- JNI: NO -- Java wrappers: NO -- Java tests: NO -- -- Install to: /usr/local -- -----------------------------------------------------------------
编译:
make
安装:
sudo make install
默认安装位置:
/usr/local/bin
- executable files/usr/local/lib
- libraries (.so)/usr/local/cmake/opencv4
- cmake package/usr/local/include/opencv4
- headers/usr/local/share/opencv4
- other files (e.g. trained cascades in XML format)
-
验证
该命令显示opencv版本号。
c++:
pkg-config opencv --modversion
python:
python3 import cv2 print(cv2.__version__)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了