熟练拆装opencv4.4等高版本

前期准备

建立make目标文件夹

cd /usr/local/
sudo mkdir opencv4.4.0

开始下载和编译

cd ~/opencv4.4.0
# 下载opencv并解压
# sudo add-apt-repository “deb http://security.ubuntu.com/ubuntu xenial-security main”
# sudo apt update
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev
git clone https://github.com/opencv/opencv.git
cd opencv // 就是clone的文件夹
mkdir build && cd build 
# make三连
# CMAKE_INSTALL_PREFIX的参数写install路径,
# OPENCV_EXTRA_MODULES_PATH的参数写opencv_contrib的路径,但3.2.0的总是不成功,可以不写
# BUILD_opencv_python、PYTHON_LIBRARY、PYTHON3_NUMPY_INCLUDE_DIRS
# 4.4.0版本cmake
sudo cmake -D WITH_TBB=ON -D WITH_EIGEN=ON -D OPENCV_GENERATE_PKGCONFIG=ON  -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF  -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=O -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4.5.4 ..

# 3.2.0版本cmake
sudo cmake -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_GTK=ON -D ENABLE_PRECOMPILED_HEADERS=OFF -D OPENCV_GENERATE_PKGCONFIG=ON  -D BUILD_DOCS=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=ON  -D WITH_OPENCL=OFF -D WITH_CUDA=OFF -D BUILD_opencv_gpu=OFF -D BUILD_opencv_gpuarithm=OFF -D BUILD_opencv_gpubgsegm=O -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3.2.0 -D INSTALL_PYTHON_EXAMPLES=ON  -D OPENCV_ENABLE_NONFREE=ON -D BUILD_opencv_python=ON -D PYTHON_EXECUTABLE=/usr/bin/python2.7 -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython2.7.so -D PYTHON_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ ..



sudo make -j16
sudo make install

配置环境

# 配置环境
sudo gedit /etc/ld.so.conf.d/opencv.conf
# 文末加上
/usr/local/opencv4.4.0/lib
# 配置生效
sudo ldconfig

如果没有生成.pc文件的方法

cd /usr/local/opencv4.4.0/lib
sudo mkdir pkgconfig && cd pkgconfig
sudo touch opencv.pc
sudo gedit opencv.pc

把以下内容 放入opencv.pc,注意更改前四行的文件夹目录,与make的目标文件夹相同

prefix=/usr/local/opencv4.4.0
exec_prefix=${prefix}
includedir=/usr/local/opencv4.4.0/include
libdir=/usr/local/opencv4.4.0/lib

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.4.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir}

配置环境变量

# 配置bash
sudo gedit /etc/bash.bashrc
# 文末加上
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv4.4.0/lib/pkgconfig
export PKG_CONFIG_PATH
# 保存,执行这两行命令使得配置生效:
source /etc/bash.bashrc
sudo updatedb

但后面需要将cv2.so软链接放入python的dist-package中。usr/local/lib优先级高于usr/lib。

sudo ln -s /usr/local/opencv3.2.0/lib/python2.7/dist-packages/cv2.so /opt/ros/melodic/lib/python2.7/dist-packages/cv2.so
sudo ln -s /usr/local/opencv3.2.0/lib/python2.7/dist-packages/cv2.so /usr/local/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so

测试

pkg-config --modversion opencv
python
import cv2
print(cv2.__version__)

拆掉

参考博客

关于pkg-config和import cv2的版本结果不一致

参考链接
参考链接2

PS:搞了一下午呜呜呜,求点赞~~

你可能感兴趣的:(slam,c++,ubuntu)