ubuntu 16.04 配置Python2.7 和 Python3.5 同时调用OpenCV

前端之家收集整理的这篇文章主要介绍了ubuntu 16.04 配置Python2.7 和 Python3.5 同时调用OpenCV前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

安装OpenCV


OpenCV 官网,下载见 SourceForge and GitHub

若使用官网版本安装不成功,则可试试Github版本。
或者

git clone https://github.com/Itseez/opencv.git

安装依赖库

sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential
sudo apt-get -y install checkinstall
sudo apt-get -y install cmake
sudo apt-get -y install pkg-config
sudo apt-get -y install yasm
sudo apt-get -y install libtiff4-dev
sudo apt-get -y install libjpeg-dev
sudo apt-get -y install libjasper-dev
sudo apt-get -y install libavcodec-dev
sudo apt-get -y install libavformat-dev
sudo apt-get -y install libswscale-dev
sudo apt-get -y install libdc1394-22-dev
sudo apt-get -y install libxine-dev
sudo apt-get -y install libgstreamer0.10-dev
sudo apt-get -y install libgstreamer-plugins-base0.10-dev
sudo apt-get -y install libv4l-dev
sudo apt-get -y install python-dev
sudo apt-get -y install python-numpy
sudo apt-get -y install libtbb-dev
sudo apt-get -y install libqt4-dev
sudo apt-get -y install libgtk2.0-dev
sudo apt-get -y install libfaac-dev
sudo apt-get -y install libmp3lame-dev
sudo apt-get -y install libopencore-amrnb-dev
sudo apt-get -y install libopencore-amrwb-dev
sudo apt-get -y install libtheora-dev
sudo apt-get -y install libvorbis-dev
sudo apt-get -y install libxvidcore-dev
sudo apt-get -y install x264
sudo apt-get -y install v4l-utils
sudo apt-get -y install ffmpeg
sudo apt-get -y install unzip

OpenCV下载

wget https://codeload.github.com/opencv/opencv/zip/3.2.0
unzip 3.2.0 && cd opencv-3.2.0
mkdir build
cd build

或者 推荐下面的下载方法

git clone https://github.com/Itseez/opencv.git
cd opencv

编译安装

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j4
sudo make install

测试

python
import cv2

配置Python3.5 调用OpenCV


以上OpenCV的python解释器版本是2.7,Python3.5想调用OpenCV,如何配置呢

安装依赖库

sudo apt-get update
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-setuptools python3-dev sudo easy_install3 pip
pip3 install numpy
sudo apt-get install cmake git libgtk2.0-dev

特别注意

sudo apt install python3-dev libpython3.5-dev python3-numpy

1. 进入opencv-3.2.0/build/路径下,执行ccmake

sudo ccmake .

2. 添加python3路径
向下翻页,找到python3,手动添加自己的python3路径

具体添加细节如下

CMAKE_BUILD_TYPE=Release 
CMAKE_INSTALL_PREFIX=/usr/local 
PYTHON3_EXECUTABLE=/usr/bin/python3 
PYTHON3_INCLUDE_DIR=/usr/include/python3.5 
PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include

命令行配置方法

cmake -D CMAKE_BUILD_TYPE=RELEASE \
 -D CMAKE_INSTALL_PREFIX=/usr/local \  -D PYTHON3_EXECUTABLE=/usr/bin/python3 \  -D PYTHON3_INCLUDE_DIR=/usr/include/python3.5 \  -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so \  -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.5/dist-packages/numpy/core/include ..

3. 生成make文件

按c键配置。如果有错,例如找不到xx库,自行调整对应选项或者安装对应库。配置成功后,按g键生成makefile。

4. make一下

make一下。多核处理器优势很大,几分钟就make好了。

sudo make -j12  # 根据你自己电脑核心数
sudo make install

5. 验证

python3
import cv2

6.查询OpenCV版本

pkg-config --modversion opencv

或者

python
import cv2
cv2.__version__

参考文献


ImportError: No module named ‘cv2’ in Ubuntu 16.04 for Python3 in OpenCV3

Install OpenCV 3.0 and Python 3.4+ on Ubuntu

Linux-ubuntu16.04 Python3.5配置OpenCV3.2

Caffe + Ubuntu 15.04 + CUDA 7.5 在服务器上安装配置及卸载重新安装(已测试可执行)

基于ubuntu16.04配置opencv3.1+python3.5

ubuntu 16.04 安装 opencv3.2.0 with python 3.5

ubuntu14/16安装python3-opencv3

Ubuntu 16.04: How to install OpenCV

猜你在找的Ubuntu相关文章