所达到的效果就是python2与python3都能使用opencv
参考:http://blog.csdn.net/jiandanjinxin/article/details/71438780
https://blog.kevin-brown.com/programming/2014/09/27/building-and-installing-opencv-3.html
1、安装依赖(其中安装numpy是个难点):
sudo apt-get update
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-setuptools python3-dev sudo easy_install3 pip
pip3 install numpy
sudo apt-get install cmake git libgtk2.0-dev
sudo apt install python3-dev libpython3.5-dev python3-numpy
2、编译opencv:
进入opencv源码文件
$ cd ~/opencv
$ mkdir build
$ cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE (编译类型)
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 (因为默认是python2,编译选项)
-D BUILD_opencv_python3=ON (编译选项)
-D BUILD_opencv_python2=OFF (编译选项)
(可选)
-D INSTALL_C_EXAMPLES=OFF (编译选项)
-D INSTALL_PYTHON_EXAMPLES=ON (编译选项)
-D BUILD_EXAMPLES=ON (编译选项)
-D PYTHON3_EXCUTABLE=/usr/bin/python3(文件路径)
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5m (文件路径)
-D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so (文件路径)
-D PYTHON_NUMPY_PATH=/usr/local/lib/python3.5/dist-packages ..(文件路径)
由于camke涉及多处查找命令,可用python中自带的查找位置的命令:
#PYTHON_INCLUDE_DIR
from distutils.sysconfig import get_python_inc
print(get_python_inc())
#PYTHON_PACKAGES_PATH
from distutils.sysconfig import get_python_lib
print(get_python_lib())
#PYTHON_EXECUTABLE
$(which python3)
#CMAKE_INSTALL_PREFIX
import sys
print(sys.prefix)
#PYTHON_NUMPY_PATH
import numpy
print numpy.__file__
opencv编译指定python3
出现这个问题 删除build重新
Python 3:
-- Interpreter: /home/kevin/.virtualenvs/cv/bin/python (ver 3.5.2)
-- Libraries: NO
-- numpy: NO (Python3 wrappers can not be generated)
-- packages path: lib/python3.5/site-packages
当cmake输出python3的各个路径正确,尤其build for python3时就可
make -j4
之后并没有执行make install
我只是将编译后的unix-install中的*.so文件移动到/usr/local/lib/python3.5/dist-packages中重命名为cv2然后就能使用了!好奇怪
也许是由于之前已经安装了python2以及系统g++的opencv,并且so文件只是相当于python3信息的转换,所以行得通!
熟悉了cmake,ccmake命令的使用,
不过为了防止以后出问题,还是保存一些参考教程
http://singlepig-blog.logdown.com/posts/2014/01/14/opencv-configuring-opencv-paths
http://www.cnblogs.com/xzd1575/p/5555523.html
http://www.afterimage.top/2016/07/ubuntu%E4%B8%8B%E5%A4%9A%E7%89%88%E6%9C%ACopencv%E5%88%87%E6%8D%A2/
https://zh.wikibooks.org/zh-cn/CMake_%E5%85%A5%E9%96%80/%E8%BC%B8%E5%87%BA%E4%BD%8D%E7%BD%AE%E8%88%87%E5%AE%89%E8%A3%9D
http://pinkyjie.com/2010/10/19/ubuntu-opencv-python/
http://zyplot.com/2017/05/13/CV2-install/
http://www.cppblog.com/colorful/archive/2012/05/05/173750.html