1、下载OpenCV的源码
OpenCV官网上有linux版本的源码包可以下载,不过最好是从git上下载,这样可以保证下载得到的是最新的代码:
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
unzip opencv.zip
安装依赖项
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
在github上下载opencv-contrib,请注意选择OpenCV-contrib版本,一定要和opencv版本对应。
git clone https://github.com/Itseez/opencv_contrib.git
将opencv-contrib解压到opencv-3.2.0文件夹中
3、编译安装
进入到OpenCV的文件夹中,创建一个build目录,进行编译:
cd opencv-3.2.0
mkdir build
cd build
cmake ..
make -j4
make install
3、测试是否安装成功
root@VB:# python
>>>import cv2
>>>cv2.__version__
'3.2.0'
显示出版本号,则表示opencv安装成功。
《注意》:如果打开python后,import cv2时,提示:
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
则需要安装python-opencv模块,在命令窗口中键入:
pip install opencv-python
或者
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
安装成功后,即可导入cv2,此时能够正确导入cv2,且正确显示版本号:
>>> import cv2
>>> cv2.__version__
'3.2.0'
参考链接:
1.http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/
2.http://www.cnblogs.com/paladinzxl/p/6749446.html