参考:Ubuntu系统—配置OpenCV
一、下载和安装依赖包
1、首先更新 apt-get,在安装前最好先更新一下系统,不然有可能会安装失败。在终端输入:
sudo apt-get updatesudo apt-get upgrade
2、接着安装官方给的opencv依赖包,在终端输入:
sudo apt-get install build-essentialsudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-devsudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-devsudo apt-get -y install libgstreamer-plugins-base1.0-devsudo apt-get -y install libgstreamer1.0-devsudo apt-get install libvtk6-dev
OpenCV3.4.x版本+Opencv_contrib+Ubuntu16.04安装记录_YuYunTan的博客-CSDN博客
安装前的必备包
这些安装不算十分完全,我只安装自己够用就成的某些包。
安装一些必要的库,还有cmake,git,g++。
sudo apt-get install build-essential sudo apt-get install cmake git g++
安装依赖包
安装一些依赖包。
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-devsudo apt-get install checkinstall yasm libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libfaac-dev libmp3lame-dev libtheora-devsudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev libavresample-dev x264 v4l-utils
处理图像所需的包
sudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
处理视频所需包
sudo apt-get install libxvidcore-dev libx264-dev ffmpeg
opencv功能优化
sudo apt-get install libatlas-base-dev gfortran
部分依赖包
sudo apt-get install libopencv-devlibqt4-dev qt4-qmake libqglviewer-dev libsuitesparse-dev libcxsparse3.1.4 libcholmod3.0.6 sudo apt-get install python-dev python-numpy
可选依赖
sudo apt-get install libprotobuf-dev protobuf-compilersudo apt-get install libgoogle-glog-dev libgflags-devsudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen
3.下载cmake-gui工具和mingw-w64
sudo apt install cmake-qt-guisudo apt install mingw-w64
二、下载opencv4.2.0和opencv_contrib-4.2.0源码压缩包
opencv4.2.0 地址https://github.com/opencv/opencv/tree/4.2.0
opencv_contrib-4.2.0地址https://github.com/opencv/opencv_contrib
首先在终端中输入如下命令来安装依赖包:
sudo aptinstall cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-devsudo apt install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-devsudo aptinstall build-essential qt5-default ccache libv4l-dev libavresample-devlibgphoto2-dev libopenblas-base libopenblas-dev doxygenopenjdk-8-jdk pylint libvtk6-dev
三、配置opencv
1、将opencv4.2.0和opencv_contrib-4.2.0解压(提取),放在一个文件夹opencv-4.2.0下,如下图所示:
2、双击进入解压出来的opencv-4.2.0文件夹,右键打开终端(或者在别处打开终端,通过输入cd opencv-4.2.0进入当前目录下),然后依次输入(不要忘了第三行的最后的空格和两个点):
mkdir buildcd build
3.然后进行cmake编译,参数可自行调整:
在build
目录内执行以下命令(注意contrib
路径换成自己的):
下面的参数是带cuda和contrib扩展包的:
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.2.0/modules \ -D PYTHON3_EXECUTABLE=/usr/bin/python3 \ -D PYTHON_EXECUTABLE=/usr/bin/python \ -D WITH_TBB=ON \ -D WITH_V4L=ON \ -D WITH_QT=ON \ -D WITH_GTK=ON \ -D WITH_VTK=ON \ -D WITH_OPENGL=ON \ -D WITH_OPENMP=ON\ -D BUILD_EXAMPLES=ON \ -D WITH_CUDA=ON \ -D BUILD_TIFF=ON \ -D ENABLE_PRECOMPILED_HEADERS=OFF\ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -DOPENCV_ENABLE_NONFREE=ON \ -D CUDA_nppicom_LIBRARY=stdc++ \ -D CUDA_ARCH_BIN="8.6" ..
CUDA_ARCH_BIN
一般需要指定,且最好不要把所有版本都编译,如CUDA_ARCH_BIN="3.0 3.5 3.7 5.0 5.2 6.0 6.1 7.0 7.5 8.6"
最好根据上面的说明,查一下当前显卡的型号,以及对应的显卡算力,然后在这里指定一个即可,如3060显卡可以保持CUDA_ARCH_BIN="8.6"
。否则全部编译一遍速度会很慢。3060显卡不加这条命令:-D CUDA_ARCH_BIN=”8.6″ ,会报错如下:
nvcc fatal : Unsupported gpu architecture 'compute_30'CMake Error at cuda_compile_1_generated_gpu_mat.cu.o.RELEASE.cmake:222 (message):Error generating/home/cgm/opencv-4.2.0/opencv-4.2.0/build/modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_gpu_mat.cu.o
原因: 我的3060显卡不支持compute_30的GPU构架
GPU Compute Capability Geforce RTX 3060 8.6 算力 CUDA GPUs – Compute Capability | NVIDIA Developer
nvcc warning : The ‘compute_35’, ‘compute_37’, ‘compute_50’, ‘sm_35’, ‘sm_37’ and ‘sm_50’ architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
nvcc警告:“compute_35”、“compute_3 7”、“compute_50”、“sm_35”,“sm_37”和“sm_50”体系结构已弃用,可能会在将来的版本中删除(使用-Wno弃用的gpu目标来抑制警告)。
cuda 11已经废弃 compute_30了,所以需要把compute_30给去掉
CMAKE_INSTALL_PREFIX
指定了编译好的库的目录,也就是说编译完成的OpenCV
库文件会在该目录下OPENCV_GENERATE_PKGCONFIG
指定了生成pkgconfig
配置文件,这个文件在后续创建OpenCV工程的时会很有用。- 如果没有 -D OPENCV_GENERATE_PKGCONFIG=ON ,后面查看配置时会找不到:
$ pkg-config --cflags --libs opencv4Package opencv4 was not found in the pkg-config search path.Perhaps you should add the directory containing `opencv4.pc'to the PKG_CONFIG_PATH environment variableNo package 'opencv4' found
- 增加 这个命令-DOPENCV_ENABLE_NONFREE=ON \ 是为了不出现下面那这样的结果
terminate called after throwing an instance of 'cv::Exception'what():OpenCV(4.2.0) /home/cgm/opencv-4.2.0/opencv_contrib-4.2.0/modules/xfeatures2d/src/surf.cpp:1027: error: (-213:The function/feature is not implemented) This algorithm is patented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'
“此算法已获得专利,在此配置中被排除在外;”
“设置OPENCV_ENABLE_NONFREE CMake选项并重建库”);
3.编译成功
最后会列出其编译后的模块列表。
-- OpenCV modules:-- To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 quality reg rgbd saliency sfm 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 java js matlab ovis-- Applications:tests perf_tests examples apps-- Documentation: NO-- Non-free algorithms: NO-- -- GUI: -- QT:YES (ver 5.12.8)-- QT OpenGL support: YES (Qt5::OpenGL 5.12.8)-- GTK+:NO-- OpenGL support:YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)-- VTK support: YES (ver 7.1.1)-- -- 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:/usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)-- PNG: /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)-- TIFF:build (ver 42 - 4.0.10)-- JPEG 2000: build (ver 1.900.1)-- OpenEXR: build (ver 2.3.0)-- HDR: YES-- SUNRASTER: YES-- PXM: YES-- PFM: YES-- -- Video I/O:-- DC1394:YES (2.2.5)-- 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.3)-- v4l/v4l2:YES (linux/videodev2.h)-- -- Parallel framework:TBB (ver 2020.1 interface 11101)-- -- Trace: YES (with Intel ITT)-- -- Other third-party libraries:-- Intel IPP: 2019.0.0 Gold [2019.0.0]--at: /home/cgm/opencv-4.2.0/opencv-4.2.0/build/3rdparty/ippicv/ippicv_lnx/icv-- Intel IPP IW:sources (2019.0.0)-- at:/home/cgm/opencv-4.2.0/opencv-4.2.0/build/3rdparty/ippicv/ippicv_lnx/iw-- Lapack:NO-- Eigen: YES (ver 3.3.7)-- Custom HAL:NO-- Protobuf:build (3.5.1)-- -- NVIDIA CUDA: YES (ver 11.4, CUFFT CUBLAS)-- NVIDIA GPU arch: 30 35 37 50 52 60 61 70 75-- NVIDIA PTX archs:-- -- cuDNN: NO-- -- OpenCL:YES (no extra features)-- Include path:/home/cgm/opencv-4.2.0/opencv-4.2.0/3rdparty/include/opencl/1.2-- Link libraries:Dynamic load-- -- Python 2:-- Interpreter: /usr/bin/python (ver 2.7.18)-- Libraries: /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.18)-- numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.5)-- install path:lib/python2.7/dist-packages/cv2/python-2.7-- -- 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/cgm/.local/lib/python3.8/site-packages/numpy/core/include (ver 1.23.3)-- install path:lib/python3.8/dist-packages/cv2/python-3.8-- -- Python (for build):/usr/bin/python-- Pylint:/usr/bin/pylint (ver: 3.8.10, checks: 163)-- -- Java:-- ant: NO-- JNI: /usr/lib/jvm/java-8-openjdk-amd64/include /usr/lib/jvm/java-8-openjdk-amd64/include/linux /usr/lib/jvm/java-8-openjdk-amd64/include-- Java wrappers: NO-- Java tests:NO-- -- Install to:/usr/local-- ------------------------------------------------------------------- -- Configuring done-- Generating done-- Build files have been written to: /home/cgm/opencv-4.2.0/opencv-4.2.0/build
我们可以发现,我们编译已经成功,可以进行下一步,即make,但是值得注意的是,如果用多核make可能会报错
sudo make -j6
j6表示6核运行,查看自己CPU 的核数:
# uniq 可以去重连续出现的相同记录cat /proc/cpuinfo | grep "cpu cores" | uniq
报错:
In file included from /home/cgm/opencv-4.2.0/opencv-4.2.0/modules/python/src2/cv2.cpp:35:
/home/cgm/opencv-4.2.0/opencv-4.2.0/build/modules/python_bindings_generator/pyopencv_generated_include.h:44:10: fatal error: opencv2/viz/types.hpp: 没有那个文件或目录
44 | #include “opencv2/viz/types.hpp”
原因:仔细分析发现这个文件是 /home/cgm/opencv-4.2.0/opencv-4.2.0/modules/python/src2/cv2.cpp第35行包含了一个头文件#include “pyopencv_generated_include.h”
然后搜索打开这个头文件pyopencv_generated_include.h发现第44行就是出错没有找到的那个头文件#include “opencv2/viz/types.hpp”
然后make时在opencv-4.2.0 里没有找到这个头文件,确实也没有,我搜索后发现这个文件在opencv_contrib-4.2.0里面.
/home/cgm/opencv-4.2.0/opencv_contrib-4.2.0/modules/viz/src
解决办法: 将/home/cgm/opencv-4.2.0/opencv_contrib-4.2.0/modules/viz/include/opencv2/viz 添加进 /home/cgm/opencv-4.2.0/opencv-4.2.0/modules/python 的 CMakeLists.txt 里面.
include_directories("/home/cgm/opencv-4.2.0/opencv_contrib-4.2.0/modules/viz/include/opencv2/viz")
再重新cmake和make……….
4.安装
sudo make install
5.安装完成后还要配置环境变量
终端输入或用gedit(替换vim)打开:
sudo gedit /etc/ld.so.conf.d/opencv.conf
在里面添加:
/usr/local/lib
保存退出,配置库:这里报错参考 sudo ldconfig报错
sudo ldconfig
添加修改环境变量
sudo gedit ~/.bashrc
在末尾添加如下内容
#opencv4.2.0环境变量export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
保存退出,最后source
一下,让更改立即生效:
source ~/.bashrc
因为,对于OpenCV4以上的版本要使用OpenCV4才能正确查询到其版本,库以及头文件目录的值。具体命令如下所示:
pkg-config --modversion opencv4pkg-config --cflags opencv4pkg-config --libs opencv4
注意:opencv4以上才是pkg-config –cflags –libs opencv4,之前版本是pkg-config –cflags –libsopencv。
四.运行测试
我安装这个的目的暂时是为了运行SIFT,SURE,FREAK特征提取算法.
你们可以去测试自己的.
五.2023年3月17日遇到的问题记录
今天运行orb slam2,发现运行不了了
报错:make[2]: *** 没有规则可制作目标“/usr/lib/libOpenNI.so”,由“../lib/libORB_SSLAM2.so” 需求。停止。
解决:
sudo apt-get install libopenni-dev libopenni2-dev
在编译时Cmake警告:
CMake Warning at CMakeLists.txt:123 (add_executable):Cannot generate a safe runtime search path for target mono_euroc becausefiles in some directories may conflict with libraries in implicitdirectories:runtime library [libopencv_stitching.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_aruco.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_bgsegm.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_bioinspired.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_ccalib.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_dnn_objdetect.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_dnn_superres.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_dpm.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_highgui.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_face.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_freetype.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_fuzzy.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_hdf.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_hfs.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_img_hash.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_line_descriptor.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_quality.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_reg.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_rgbd.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_saliency.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_shape.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_stereo.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_structured_light.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_phase_unwrapping.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_superres.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_optflow.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_surface_matching.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_tracking.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_datasets.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_plot.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_text.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_dnn.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_ml.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_videostab.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_videoio.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_viz.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_ximgproc.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_video.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_xobjdetect.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_imgcodecs.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_objdetect.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_calib3d.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_features2d.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_flann.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_xphoto.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_photo.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_imgproc.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libruntime library [libopencv_core.so.4.2] in /usr/lib/x86_64-linux-gnu may be hidden by files in:/usr/local/libSome of these libraries may not be found correctly.
CMakeLists.txt里是这样的
find_package(OpenCV QUIET)if(NOT OpenCV_FOUND) find_package(OpenCV 2.4.3 QUIET) if(NOT OpenCV_FOUND)message(FATAL_ERROR "OpenCV > 2.4.3 not found.") endif()endif()
增加一些message命令查看OpenCV的信息
# If the package has been found, several variables will# be set, you can find the full list with descriptions# in the OpenCVConfig.cmake file.# Print some message showing some of themmessage(STATUS "OpenCV library status:")message(STATUS "config: ${OpenCV_DIR}")message(STATUS "version: ${OpenCV_VERSION}")message(STATUS "libraries: ${OpenCV_LIBS}")message(STATUS "include path: ${OpenCV_INCLUDE_DIRS}")
OpenCV_DIR 是/usr/lib/x86_64-linux-gnu/cmake/opencv4,并且有警告信息
解决:
设置OpenCV_DIR的路径,重新编译运行./build.sh
SET(OpenCV_DIR /usr/local/lib/cmake/opencv4/) # 设置OpenCV_DIR
SET(OpenCV_DIR /usr/local/lib/cmake/opencv4/) # 设置OpenCV_DIRfind_package(OpenCV QUIET)if(NOT OpenCV_FOUND) find_package(OpenCV 2.4.3 QUIET) if(NOT OpenCV_FOUND)message(FATAL_ERROR "OpenCV > 2.4.3 not found.") endif()endif()# If the package has been found, several variables will# be set, you can find the full list with descriptions# in the OpenCVConfig.cmake file.# Print some message showing some of themmessage(STATUS "OpenCV library status:")message(STATUS "config: ${OpenCV_DIR}")message(STATUS "version: ${OpenCV_VERSION}")message(STATUS "libraries: ${OpenCV_LIBS}")message(STATUS "include path: ${OpenCV_INCLUDE_DIRS}")
TO BE CONTINUED…