opencv4 python3源码安装

环境:ubuntu16.04 anaconda3
之前在windows上都是直接下载编译好的版本,这一次我想从源码编译opencv,并生成python3的动态链接。
新建一个虚拟环境(不需要的可以跳过这一步):

conda create -n learn python=3.6.2

在这个虚拟环境安装Numpy:
pip install numpy

安装环境依赖项:
sudo apt-get install build-essential cmake unzip pkg-config

下载opencv和opencv_contrib源码:
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip

下载完了之后解压缩,在opencv目录下新建一个build文件夹:
cd ~/opencv-4.0.0 mkdir build cd build

用cmake 进行编译,可以设置的参数有很多,可以自行查一下,这边我们是给python3.6生成动态链接,需要设置好PYTHON3_EXECUTABLE等路径(路径一定要正确,不然无法生成链接):
cmake -D OPENCV_ENABLE_NONFREE=ON \ -D BUILD_opencv_python3=yes \ -D BUILD_opencv_python2=no \ -D PYTHON3_EXECUTABLE=~/anaconda3/envs/learn/bin/python3.6m \ -D PYTHON3_INCLUDE_DIR=~/anaconda3/envs/learn/include/python3.6m \ -D PYTHON3_LIBRARY=~/anaconda3/envs/learn/lib/libpython3.6m.so \ -D PYTHON3_NUMPY_INCLUDE_DIRS=~/anaconda3/envs/learn/lib/python3.6/site-packages/numpy/core/include \ -D PYTHON3_PACKAGES_PATH=~/anaconda3/envs/learn/lib/python3.6/site-packages\ -D PYTHON_DEFAULT_EXECUTABLE=~/anaconda3/envs/learn/bin/python3.6m ..

如果成功,会有很长一串输出,找一下类似下面的输出,确认一下路径是否正确:
--Python 3: --Interpreter:/home/qogori/anaconda3/envs/learn/bin/python3.6m (ver 3.6.2) --Libraries:/home/qogori/anaconda3/envs/learn/lib/libpython3.6m.so (ver 3.6.2) --numpy:/home/qogori/anaconda3/envs/learn/lib/python3.6/site-packages/numpy/core/include (ver 1.19.1) --packages path:/home/qogori/anaconda3/envs/learn/lib/python3.6/site-packages -- --Python (for build):/home/qogori/anaconda3/envs/learn/bin/python3.6m

之后,
make -j4

如果顺利则编译完毕,安装:
sudo make install

此时,在/usr/local/python/cv2/python-3.6位置会生成符号链接:
$cd /usr/local/python/cv2/python-3.6 $ ls cv2.cpython-36m-x86_64-linux-gnu.so $ sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

将符号链接绑定到python:
$ cd ~/anaconda3/envs/learn/lib/python3.6/site-packages $ ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so

此时已经成功。可以看一下当前该虚拟环境下opencv的版本:
$ python Python 3.6.2 |Anaconda, Inc.| (default, Oct5 2017, 07:59:26) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '4.0.0' >>> quit()

编译的时候,可能会出现的错误:
一:~/opencv_contrib-4.0.0/modules/xfeatures2d/src/boostdesc.cpp:653:37: fatal error: boostdesc_bgm.i: 没有那个文件或目录:
解决方案:
在这里下载缺少的文件解压缩都放到~/opencv_contrib-4.0.0/modules/xfeatures2d/src 文件下,验证码:e1wc,再次编译即可
二:~/opencv_contrib-4.0.0/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp:7:82: fatal error: features2d/test/test_detectors_invariance.impl.hpp: 没有那个文件或目录
解决方案:
将opencv-4.0.0/modules/features2d/test/下的文件
test_descriptors_invariance.impl.hpp test_descriptors_regression.impl.hpp test_detectors_invariance.impl.hpp test_detectors_regression.impl.hpp test_invariance_utils.hpp

拷贝到opencv_contrib-4.0.0/modules/xfeatures2d/test/下
将opencv_contrib-4.0.0/modules/xfeatures2d/test/test_features2d.cpp文件里
#include "features2d/test/test_detectors_regression.impl.hpp" #include "features2d/test/test_descriptors_regression.impl.hpp"

改为
#include "test_detectors_regression.impl.hpp" #include "test_descriptors_regression.impl.hpp"

将opencv_contrib-4.0.0/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp文件里
#include "features2d/test/test_detectors_invariance.impl.hpp" #include "features2d/test/test_descriptors_invariance.impl.hpp"

改为
#include "test_detectors_invariance.impl.hpp" #include "test_descriptors_invariance.impl.hpp"



参考链接:https://blog.csdn.net/u011736771/article/details/85960300
https://zhuanlan.zhihu.com/p/129369660
【opencv4 python3源码安装】

    推荐阅读