Ubuntu 下python3 安装opencv3.1.0及问题解决


方法/步骤

  1. 1

    由于ubuntu14.04/16.04都自带了python2.7和python3.5,所以无需在安装python。

    检查python是否安装:

    ~ whereis python

  2. 2

    首先需要的是 pip:

    ~ sudo apt-get install python3-setuptools python3-dev sudo easy_install3 pip

    然后是 numpy:

    ~ pip3 install numpy

    安装依赖:

    ~ sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

  3. 3

    安装opencv3:

    ~ wget https://codeload.github.com/opencv/opencv/zip/3.2.0

    ~ unzip 3.2.0 && cd 3.2.0

    cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local  PYTHON3_EXECUTABLE=/usr/bin/python3 PYTHON_INCLUDE_DIR=/usr/include/python3.4 PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.4m.so PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.4/dist-packages/numpy/core/include ..

    ~ make -j4

    ~ sudo make install

  4. 4

    测试一下:

    python3.5

    >>> import cv2

    >>> cv2.__version__

    '3.2.0'

    >>>

    恭喜您,现在可以在python3.5中调用opencv3了!

    错误:在编译安装opencv3.1时可能会报错
    modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/build.make:290: recipe for target ‘modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o’ failed 
    make[2]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o] Error

    这是CUDA配置出错。

    解决办法1:不用CUDA在cmake时设置-DWITH_CUDA=OFF

    解决办法2:GraphCut在cuda7.5中弃用,在cuda8.0中被移除。

    修改 ~/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp文件 
    把第45行修改成:

    #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)

    即在 \#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) 加上 || (CUDART_VERSION >= 8000)


你可能感兴趣的:(Ubuntu 下python3 安装opencv3.1.0及问题解决)