VMware-workstation14+Ubuntu 16.04.3+Anaconda3-5.0.1环境下安装OpenCV3.3.1教程
1. 安装OpenCV依赖包
打开终端,输入以下指令
$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg8-dev libpng12-dev libtiff5-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
$ sudo apt-get install libxvidcore-dev libx264-dev # 处理视频所需的包
$ sudo apt-get install libatlas-base-dev gfortran # 优化opencv功能
$ sudo apt-get install ffmpeg
2.下载OpenCV源文件
$ cd ~
$ wget -O opencv-3.3.1.zip https://github.com/opencv/opencv/archive/3.3.1.zip
$ unzip opencv-3.3.1.zip
$ wget -O opencv_contrib-3.3.1.zip https://github.com/opencv/opencv_contrib/archive/3.3.1.zip
$ unzip opencv_contrib-3.3.1.zip
也可以先下载好,手动解压
3.编译OpenCV
在终端输入如下指令:
$ cd ~/opencv-3.3.1/
$ mkdir build
$ cd build
在终端输入以下cmake指令,注意每行后面没有回车:
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/zyy/anaconda3 \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.1/modules \
-D PYTHON_EXECUTABLE=~/anaconda3/bin/python \
-D ENABLE_PRECOMPILED_HEADERS=OFF
-D BUILD_EXAMPLES=ON ..
如下图所示:
等待,如果没有错误,出现以下结果:
继续执行以下指令
$ make -j4 #等待好久,两三个小时
$ sudo make install
$ sudo ldconfig
4.完成安装OpenCV
$ ls -l /home/zyy/anaconda3/lib/python3.6/site-packages/
从一长串结果中看到:
总用量 8940
-rw-r--r-- 1 root root 5094200 12月 22 16:22 cv2.cpython-36m-x86_64-linux-gnu.so
可以对这个.so文件重命名:
$ cd /home/zyy/anaconda3/lib/python3.6/site-packages/
$ sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
也可以把它放到环境里
$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so
5. 测试OpenCV
$ cd ~
$ workon cv
$ python
Python 3.5.2 (default, Jul 5 2016, 12:43:10)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0'
>>>
至此,OpenCV已经成功安装,最后可以删除安装包及包含的build文件夹下内容
$ cd ~
$ rm -rf opencv-3.1.0 opencv_contrib-3.1.0 opencv.zip opencv_contrib.zip