Ubuntu16.04下在Python3.5安装OpenCV

1. 在 Ubuntu 16.04 上安装 OpenCV 的依赖库

Ctrl+Alt+T打开终端

例行公事的刷新升级。。。

$ sudo apt-get update
$ sudo apt-get upgrade

 

 安装开发工具和相关库。。。

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3.5-dev

 

 2.下载OpenCV源码(这里介绍3.1.0版本,需新版本https://github.com/opencv/opencv,修改..../3.1.0.zip 后缀即可,如:/3.3.1.zip)

注意:所以安装均在根目录下执行

cd ~(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.安装Python3环境

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

利用虚拟环境(减少安装错误)

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

更新加载~/.bashrc文件

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashr
source ~/.bashrc

创建Python虚拟环境

mkvirtualenv cv -p python3

workon cv (进入虚拟环境)

在Python虚拟环境中安装NumPy

pip install numpy

4.在Ubuntu16.04上配置编译OpenCV

确保在虚拟环境中再执行以下命令进行配置

$ cd ~/opencv-3.1.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
    -D BUILD_EXAMPLES=ON ..

确定自己电脑的内核数为几,就在 j 后面写几,进行编译

make -j4
sudo make install

5.建立sym-link,将OpenCV cv2.so绑定到Python3的cv虚拟环境中:

cd /usr/local/lib/python3.5/site-packages/
sudo mv cv2.cpython-35m-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

6.测试是否安装成功

$ 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'
>>>

以上就成功完成安装了!!!

 

7.解决安装OpenCV后根目录内存过小

前6步成功后!!!

删除!!! 

删除最开始安装的OpenCV zip文件和解压文件!!!

对后期编译没有影响!!!

 

本文借鉴https://www.jianshu.com/p/2975af275871

如有其它问题,可移步至此

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页