opencv 3.4 编译 与 python3.5 封装

本文详细介绍了如何在Ubuntu 16.04.2 LTS上为Python 3.5.2安装最新版OpenCV 3.2.0的过程。包括下载源码包、安装依赖库、配置编译选项、编译安装等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已装有ROS的CV2,虽然建立了VIRTURALENV目录,但搜索的顺序仍然是先环境变量中的PYTHONPATH,因此注意区别是哪个版本的CV2

Installing OpenCV 3.2.0 for Python 3.5.2 on Ubuntu 16.04.2 LTS

Posted on Sun 26 February 2017 in Software Engineering

Introduction

I just freshly finished the installation of Ubuntu 16.04.2 LTS on my new laptop, so I had to install the programming environments I need. As I am passionate of image processing and computer vision, I needed to re-install the latest version of OpenCV (3.2.0) to be able to use it with Python 3.5.2

The best way to work with Python libraries on a Linux platform is by relying on Python virtual environments which saves headaches caused by versions dependencies. This is why I suppose you have already installedvirtualenv.

Nota bene

Do not even give a try to install the wrapper package for OpenCV python bindings:

pip install opencv-python

The reason is that this easy way of doing things will lead you to install a package which is not enough mature as you can not even deal with videos and can not even display an image usingcv2.imshow() function. So do not be lazy and follow the below steps.

Installation steps

I am sharing with you what worked for me on my machine (64 bits):

  • Download OpenCV 3.2.0:
wget -O 3.2.0.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
unzip 3.2.0.zip
  • Download SIFT and SURF OpenCV 3.2.0 implementations:
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip
unzip opencv_contrib.zip
  • Install the all the packages needed to compile a debian package:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential cmake pkg-config
  • Install the libraries needed to process different video and images formats:
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
  • Install computation libraries:
sudo apt-get install libatlas-base-dev numpy gfortran
  • Install Python 3.5 development libraries:
sudo apt-get install python3.5-dev
  • Create a virtual environment where to install OpenCV 3.2.0:
virtualenv cv
cd cv
source bin/activate
  • Setting OpenCV 3.2.0 (this takes time, be patient):
cd ~/3.2.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 ..
  • Compiling and installing OpenCV 3.2.0:
make -j4
sudo make install
sudo ldconfig
  • Rename cv2.cpython-35m-x86_64-linux-gnu.so to cv2.so
sudo mv /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so /usr/local/lib/python3.5/site-packages/cv2.so
  • Simlinking OpenCV3.2.0 to the cv virtual environment:
cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

These previous steps lead me to what I wanted to get:

(cv) begueradj@h4ck-B1ll:~$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值