ubuntu 在pyenv环境中安装 opencv

#安装opencv依赖
sudo apt-get build-dep opencv
#安装cmake
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
sudo apt-get install cmake

#根据需要填写cmake中的python路径
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=~/.pyenv/versions/3.5.0/usr/local/ \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_legacy=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/tmp/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON \
-D PYTHON_EXECUTABLE=~/.pyenv/versions/3.5.0/bin/python \
-D PYTHON_LIBRARY=~/.pyenv/versions/3.5.0/lib/libpython3.5m.a \
-D PYTHON_INCLUDE_DIR=~/.pyenv/versions/3.5.0/include/python3.5m \
-D PYTHON_INCLUDE_DIRS=~/.pyenv/versions/3.5.0/include/python3.5m \
-D PYTHON_INCLUDE_DIRS2=~/.pyenv/versions/3.5.0/include/python3.5m \
-D INCLUDE_DIRS=~/.pyenv/versions/3.5.0/include/python3.5m \
-D INCLUDE_DIRS2=~/.pyenv/versions/3.5.0/include/python3.5m \
-D PYTHON_PACKAGES_PATH=~/.pyenv/versions/3.5.0/lib/python3.5/site-packages \
-D PYTHON_NUMPY_INCLUDE_DIR=~/.pyenv/versions/3.5.0/lib/python3.5/site-packages/numpy/core/include ..
make -j4 all install
#添加软链接
ln -s ~/.pyenv/versions/3.5.0/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

更多参考:
https://gist.github.com/pohmelie/cf4eda5df24303325b16
http://www.pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu
https://gist.github.com/rjw57/d50dd8a023afa84a5bdb

安装opencv的python脚本:来源:https://gist.github.com/pohmelie/906a79f8ad665d0591186549e191ae9f

import pathlib
import sys

import sh


def clone_if_not_exists(name, url, **kwargs):
    if not pathlib.Path(name).exists():
        print("Cloning", url, "...")
        sh.git.clone(url, name, depth=1, **kwargs)


def build_opencv():
    sh.pip.install("numpy")
    clone_if_not_exists("opencv", "https://github.com/PolarNick239/opencv.git", branch="stable_3.0.0")
    clone_if_not_exists("opencv_contrib", "https://github.com/PolarNick239/opencv_contrib.git", branch="stable_3.0.0")
    sh.rm("-rf", "opencv-build")
    sh.mkdir("opencv-build")
    sh.cd("opencv-build")
    python_path = pathlib.Path(sh.pyenv.which("python").stdout.decode()).parent.parent
    version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
    sh.cmake(
        "../opencv",
        "-DCMAKE_BUILD_TYPE=RELEASE",
        "-DCMAKE_INSTALL_PREFIX={}/usr/local".format(python_path),
        "-DWITH_CUDA=OFF",
        "-DWITH_FFMPEG=OFF",
        "-DINSTALL_C_EXAMPLES=OFF",
        "-DBUILD_opencv_legacy=OFF",
        "-DBUILD_NEW_PYTHON_SUPPORT=ON",
        "-DBUILD_opencv_python3=ON",
        "-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules",
        "-DBUILD_EXAMPLES=ON",
        "-DPYTHON_EXECUTABLE={}/bin/python".format(python_path),
        "-DPYTHON3_LIBRARY={}/lib/libpython{}m.so".format(python_path, version),
        "-DPYTHON3_PACKAGES_PATH={}/lib/python{}/site-packages/".format(python_path, version),
        "-DPYTHON3_NUMPY_INCLUDE_DIRS={}/lib/python{}/site-packages/numpy/core/include".format(python_path, version),
        "-DPYTHON_INCLUDE_DIR={}/include/python{}m".format(python_path, version),
        _out=sys.stdout,
    )
    sh.make("-j4", _out=sys.stdout)
    sh.make.install(_out=sys.stdout)


if __name__ == "__main__":
    build_opencv()
posted @ 2017-07-13 21:13  机器狗mo  阅读(852)  评论(0编辑  收藏  举报