Ubuntu python opencv安装配置

1.安装基本的开发包

sudo apt-get update upgrade
sudo apt-get install build-essential cmake git pkg-config

#加载图片工具包
sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

sudo apt-get install libgtk2.0-dev

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

sudo apt-get install libatlas-base-dev gfortran

2.下载opencv

cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
unzip opencv.zip

#下载扩展功能
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
unzip opencv_contrib.zip

3.Python虚拟环境

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
rm get-pip.py

sudo pip install virtualenv virtualenvwrapper

#ubuntu 下 ~/.bashrc添加环境变量:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

#生效
source ~/.bashrc

#首先在linux安装好python2和python3 使用virtualenvwrapper创建不同的环境
#python3有很多新的特性 如果老的代码还是python2 那虚拟环境能便于对版本的控制
mkvirtualenv cv2 -p python2
mkvirtualenv py3 -p python3

4.编译

#切换工作环境 此处python2版本在2.7+
workon cv2
cd ~/opencv
mkdir build
cd build 
cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_C_EXAMPLES=ON \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
  -D BUILD_EXAMPLES=ON ..

#编译
make -j4

#安装
sudo make install
sudo ldconfig

安装成功之后 编译之后生成的so文件:

/usr/local/lib/python-2.7/site-packages/

#创建链接
cd ~/.virtualenvs/cv2/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

5.验证一下

workon cv2
python
>>> import cv2
>>> cv2.__version__

你可能感兴趣的:(Ubuntu python opencv安装配置)