一、卸载自带OpenCV依赖
sudo apt-get purge libopencv
sudo apt-get purge python-numpy
sudo apt autoremove
二、安装依赖(没有安装全可能导致后续编译错误)
sudo apt install -y build-essential cmake git pkg-config libgtk2.0-dev libopenexr-dev
sudo apt install -y gfortran libblas-dev liblapack-dev libeigen3-dev
sudo apt install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libavutil-dev libavresample-dev libxvidcore-dev libx264-dev libv4l-dev
三、下载opencv和opencv_contrib源码包
opencv地址:https://opencv.org/releases/ 选择OpenCV3 GitHub版本容易编译,下载速度可能会比较慢
opencv_contrib地址:https://github.com/opencv/opencv_contrib/releases 和opencv版本对应
分别解压两个压缩包,放置于同一个文件夹src下
四、编译OpenCV
编译之前的工作
-
将 {opencv的路径}/modules/stitching/src/precomp.hpp 中第 91 行的 opencv2/xfeatures2d/cuda.hpp 修改为 {opencv_contrib的路径}/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp
-
将 {opencv 的路径}/modules/stitching/include/opencv2/stitching/detail/matchers.hpp 中第 52 行的 opencv2/xfeatures2d/cuda.hpp 修改为 {opencv_contrib的路径}/modules/xfeatures2d/include/opencv2/xfeatures2d/cuda.hpp。
-
编译过程可能出现缺少boostdesc_bgm.i文件报错
下载压缩包boostdesc_bgm.i,解压,将其中所有的文件放置opencv_contrib/modules/xfeatures2d/src/ 路径下即可,重新编译 -
提醒:如果是在嵌入式Linux系统编译,请提前预留15g以上的空间,否则可能在编译过程中会因空间不足编译失败
cd src
cd opencv
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_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
make
sudo make install
sudo ldconfig
五、链接Python
$ cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6/
$ sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.6/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2/python-3.6/cv2.so cv2.so
参考博客
https://blog.csdn.net/HuanCaoO/article/details/105933471