编译安装支持或不支持CUDA的opencv4以及配置相关环境

1、安装依赖

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2、下载opencv与opencv_contrib

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

3、准备工作

3.1、在opencv目录中创建build目录

mkdir build

3.2、把opencv_contrib拷贝到opencv里面

4、编译安装不使用CUDA的opencv

  • 1 、cmake
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON  -DOPENCV_ENABLE_NONFREE:BOOL=ON -D ENABLE_FAST_MATH=1  -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules  -D WITH_JAVA=OFF -D WITH_WEBP=OFF -D BUILD_EXAMPLES=off -D OPENCV_GENERATE_PKGCONFIG=ON ..
  • 2、make
make -j$(nproc)
sudo make install

==========================================================================

5、编译安装使用CUDA的opencv

5.1、手动下载文件并修改一些配置

  • 1、手动下载下面文件并放到opencv/modules/features2d/src中
boostdesc_bgm_bi.i        boostdesc_binboost_128.i  vgg_generated_48.i
boostdesc_bgm_hd.i        boostdesc_binboost_256.i  vgg_generated_64.i
boostdesc_bgm.i           boostdesc_lbgm.i          vgg_generated_80.i
boostdesc_binboost_064.i  vgg_generated_120.i

需要文件下载地址
并且把opencv/modules/features2d 拷贝到opencv_contrib/modules/xfeatures2d/test中

  • 2、手动下载ippicv_2020_lnx_intel64_20191018_general.tgz 放在opencv-4.5.0/3rdparty/ippicv/ 然后修改ippicv.cmake为
42行的"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"改为"file:///home/xxx/opencv/3rdparty/ippicv/"即可。

下载地址

5.2、build

创建build文件并camke
命令说明:
-D OPENCV_GENERATE_PKGCONFIG=ON:OpenCV4以上版本默认不使用pkg-config,该编译选项开启生成opencv4.pc文件,支持pkg-config功能。
-D CMAKE_INSTALL_PREFIX=/usr/local:指定安装目录。

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D WITH_CUDA=ON -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D CUDA_ARCH_BIN=7.5 -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -D HAVE_OPENCV_PYTHON3=ON -D PYTHON3_EXECUTABLE=/usr/local/bin/python3.7.7/bin/python3.7  -D WITH_WEBP=OFF -D BUILD_EXAMPLES=off -D OPENCV_GENERATE_PKGCONFIG=ON ..

5.5、make

make -j$(nproc)
sudo make install

=========================================================================

6、配置环境(不是必须)

cmake开启OPENCV_GENERATE_PKGCONFIG=ONpkg-config --libs opencv4 就应该可以找到lib 就不需要此步骤

6.1 找到opencv4.pc文件所在目录:

 sudo find / -iname opencv4.pc
==》/usr/local/lib/pkgconfig/opencv4.pc

6.2 将/usr/local/lib/pkgconfig/路径加入PKG_CONFIG_PATH:

sudo gedit  /etc/profile.d/pkgconfig.sh
加入
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
然后激活
source /etc/profile

6.3 验证

pkg-config --libs opencv4
-L/usr/local/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -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_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_viz -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev

7 、设置动态环境

7.1 配置加载动态库*.so的路径

sudo gedit  /etc/ld.so.conf.d/opencv4.conf “opencv4.conf 文件可能为空”
加入
/usr/local/lib
刷新
sudo ldconfig

8、配置python环境

8.1 查找so路径

sudo find / -iname cv2*.so
/usr/local/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so
/usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so
/usr/local/bin/python3.7.7/lib/python3.7/site-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so
/usr/local/bin/python3.7.7/lib/python3.7/site-packages/cv2.so

8.2建立软链接

sudo ln -s /usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so  /usr/local/lib/python3.7/site-packages/cv2.so

9、一个资料完整的opencv4.5文件包,在build中从新cmake,make就可

opencv4.5

10、编译时可能遇到的问题

  • opencv/opencv_contrib/modules/xfeatures2d/test/features2d/misc/java/src/cpp/features2d_converters.cpp:2:10: fatal error: common.h: 没有那个文件或目录
    2 | #include “common.h”

解决方法:
1、想办法让其找到common.h
2、想办法不让其编译此test模块,最暴力的方法把test文件夹删除

你可能感兴趣的:(C/C++相关学习,Linux/Win相关学习,opencv,python,计算机视觉)