refer to http://www.cnblogs.com/arkenstone/p/6490017.html
Ubuntu16.04下安装opencv
opencv基本上是搞CV必备套件之一了,支持的语言也非常多,但是安装起来有点麻烦(如果是在conda下安装的话则可以用conda install -c menpo opencv3=3.2.0)。通过官方文档和一些博客总结的安装流程大致如下:
1.安装官方给的opencv依赖包
- GCC 4.4.x or later
- CMake 2.6 or higher
- Git
- GTK+2.x or higher,including headers (libgtk2.0-dev) # 控制opencv GUI
- pkg-config
- Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev,python-numpy)
- ffmpeg or libav development packages: libavcodec-dev,libavformat-dev,libswscale-dev
- [optional] libtbb2 libtbb-dev
- [optional] libdc1394 2.x
-
[optional] libjpeg-dev,libpng-dev,libtiff-dev,libjasper-dev,libdc1394-22-dev
$ 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-install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev # 处理图像所需的包 $ sudo apt-install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-install libxvidcore-dev libx264-dev # 处理视频所需的包 $ sudo apt-install libatlas-base-dev gfortran # 优化opencv功能 $ sudo apt-install ffmpeg
** 其他一些包在之后的cmake的时候缺失的时候会进行安装,如果安装失败可以手动下载相应的包然后放入提示的目录下。而cmake下载tar.gz文件的时候可能不支持,需要进行配置,具体操作可以参考这里。
2.下载opencv3.2.0
这里需要下载opencv和opencv_contrib(后者会在cmake配置的时候用到),这是因为opencv3以后SIFT和SURF之类的属性被移到了contrib中,。
$ wget https://github.com/opencv/opencv/archive/3.2.0.zip # 从github上直接下载或者clone也可
$ wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip
3.配置编译opencv
将上述opencv包解压,然后cmake配置属性
$ cd opencv-3.1.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/home/fanzong/anaconda2/envs/tensorflow \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.2.0/modules \
-D PYTHON_EXCUTABLE=/home/fanzong/anaconda2/envs/tensorflow/bin/python \
-D WITH_TBB=D WITH_V4L=D WITH_QT=D WITH_GTK=D WITH_OPENGL=D BUILD_EXAMPLES=ON .. # cmake命令的使用方式:cmake [<some optional parameters>] <path to the OpenCV source directory>。如果命令报错的话可以试着把-D后面的空格去掉在执行一次。
$ make
$ sudo make install
CMAKE_INSTALL_PREFIX:安装的python目录前缀,其实就是指定了python模块的安装路径:CMAKE_INSTALL_PREFIX/lib/python2.7/site-packages。获取该路径的方式可以用python -c "import sys; print sys.prefix"
PYTHON_EXCUTABLE:指定python路径
OPENCV_EXTRA_MODULES_PATH: 指定opencv所需模块的路径,就是之前我们所说的contrib
其他cmake的具体变量说明可以看这里
4.完成安装并测试
编译之后应该会在CMAKE_INSTALL_PREFIX/lib/python2.7/site-packages/目录下找到cv2.so。打开python console,检测opencv版本python -c "import cv2; print cv2.__version__"
。如果正确安装的话则会输出3.2.0。
参考
http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html
http://stackoverflow.com/questions/31040746/cant-open-video-using-opencv