1. 安装编译依赖的软件包
# 安装读写不同图片类型的库: sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev # 安装读取视频格式的库: sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev # 安装GTK库以使用OpenCV的GUI: sudo apt-get install libgtk2.0-dev # 安装用来优化一些OpenCV内部函数的库,比如矩阵操作: sudo apt-get install libatlas-base-dev gfortran liblapacke-dev
2. 编译OpenCV
下载并解压源码:
wget https://github.com/opencv/opencv/archive/3.2.0.tar.gz -O opencv.tar.gz wget https://github.com/opencv/opencv_contrib/archive/3.2.0.tar.gz -O opencv_contrib.tar.gz tar zxf opencv.tar.gz tar zxf opencv_contrib.tar.gz
编译:
cd opencv-3.2.0 mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=OFF -D INSTALL_PYTHON_EXAMPLES=OFF -D WITH_IPP=OFF -DBUILD_opencv_dnn=OFF -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.2.0/modules -D BUILD_EXAMPLES=OFF .. make -j4 # 编译时间好长啊,这还是四核一起编译呢
安装
编译没有出错的话,就可以安装了:
sudo make install sudo ldconfig
以上命令会安装OpenCV的python模块到:
(py3.6) veelion@gtx:~/opencv/opencv-3.2.0/build$ ls /usr/local/lib/python3.6/site-packages/ cv2.cpython-36m-x86_64-linux-gnu.so
把以上.so文件链接到py3.6虚拟环境下:
(py3.6) veelion@gtx:~$ cd ~/.virtualenvs/py3.6/lib/python3.6/site-packages/ (py3.6) veelion@gtx:~/.virtualenvs/py3.6/lib/python3.6/site-packages$ ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
测试OpenCV的安装
veelion@gtx:~$ workon py3.6 (py3.6) veelion@gtx:~$ python Python 3.6.1 (default, Mar 22 2017, 10:05:53) [GCC 5.4.1 20170304] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.2.0'
文章来自于我的学习笔记www.yuanrenxue.com