OpenCV 3.1.0与Python 3.5

时间:2016-10-14 20:34:24

标签: python linux opencv python-3.5 opencv3.1

在完成其他SO答案中推荐的几个不同的教程,指南和步骤后,我没有设法安装OpenCV以便在我的Ubuntu 16.04系统中使用Python 3.5。

只要OpenCV 3.1.0正式支持Python 3.x,我该如何正确安装它?

1 个答案:

答案 0 :(得分:2)

在收集了不同教程和指南的步骤和故障排除解决方案之后,我在我的系统中管理了相应的Python 3.5和OpenCV库。

安装是在virtualenv下执行的,因此无需清理系统中以前的安装尝试占用空间。

按照此处介绍的步骤安装:

  • openCV 3.1.0
  • opencv_contrib 3.1.0
  • numpy的
  • SciPy的
  • scikit
  • matplotlib
  • 用Cython
  • VENV

最后,可能需要20Gb的空间,如果您之前尚未安装任何这些软件包。

你需要gcc-4.9 +来编译OpenCV,我用gcc-5.4测试它

安装OpenCV依赖项

sudo apt-get build-dep -y opencv

创建并设置virtualenv

sudo apt-get install python3-venv
python3.5 -m venv python35-opencv31
source ~/python35-opencv31/bin/activate
pip install matplotlib
pip install numpy
pip install scipy
pip install scikit-learn
pip install cython
pip install -U scikit-image

编译OpenCV 3.1.0和openvc_contrib 3.1.0

依赖关系

sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

获取存储库

mkdir ~/git
cd ~/git
git clone https://github.com/opencv/opencv.git
cd ./opencv
git checkout 3.1.0

cd ~/git
git clone https://github.com/Itseez/opencv_contrib.git
cd ./opencv_contrib
git checkout 3.1.0

确保找到一些库

ffmpeg libs

sudo -i
mkdir /usr/include/ffmpeg
cd /usr/include/ffmpeg
ln -sf /usr/include/x86_64-linux-gnu/libavcodec/*.h ./
ln -sf /usr/include/x86_64-linux-gnu/libavformat/*.h ./
ln -sf /usr/include/x86_64-linux-gnu/libswscale/*.h ./

如果在编译期间,在尝试查找某些ffmpeg库时出现任何问题,请卸载ffmpeg和build it from source

与opencv_contrib模块的python绑定

echo "\nfind_package(HDF5)\ninclude_directories(\${HDF5_INCLUDE_DIRS})" >> ~/git/opencv/modules/python/common.cmake

编译

source ~/python35-opencv31/bin/activate
mkdir ~/opencv3.1.0
cd ~/git/opencv/
mkdir release
cd ./release
export CC=$(which gcc)
export CXX=$(which g++)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=~/opencv3.1.0 \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/git/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON \
    -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..

输出应包括:

--   Python 2:
--     Interpreter:                 /home/rodrigo/anaconda/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
--     numpy:                       /home/rodrigo/anaconda/lib/python2.7/site-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python2.7/site-packages
-- 
--   Python 3:
--     Interpreter:                 /home/rodrigo/python35-opencv/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /home/rodrigo/python35-opencv/lib/python3.5/site-packages/numpy/core/include (ver 1.11.2)
--     packages path:               lib/python3.5/site-packages
-- 
--   Python (for build):            /home/rodrigo/anaconda/bin/python2.7

现在:

make

如果成功,那么:

make install

将OpenCV库添加到您的virtualenv

cd ~/python35-opencv31/lib/site-packages
ln -s ~/opencv3.1.0/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so

完成!

测试它是否按预期工作:

cd ~
source ~/python35-opencv31/bin/activate
python
import cv2
cv2.__version__

它应导入cv2并显示版本号3.1.0。