OpenCV3 安装

Opencv 安装

本文主要说明了在ubuntu上通过源码安装Opencv3,包含各种独立接口.具体可以参照LearnOpencv:
https://www.learnopencv.com/install-opencv3-on-ubuntu/

更新系统源

    sudo apt-get update
    sudo apt-get upgrade

安装系统依赖库

    # 移除先前安装的x264</h3>
    sudo apt-get remove x264 libx264-dev

    #安装依赖项
    sudo apt-get install build-essential checkinstall cmake pkg-config yasm
    sudo apt-get install git gfortran
    sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev

    #  Ubuntu 14.04
    sudo apt-get install libtiff4-dev
    #  Ubuntu 16.04
    sudo apt-get install libtiff5-dev

    sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
    sudo apt-get install libxine2-dev libv4l-dev
    sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
    sudo apt-get install libqt5-dev libgtk2.0-dev libtbb-dev
    sudo apt-get install libatlas-base-dev
    sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
    sudo apt-get install libvorbis-dev libxvidcore-dev
    sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
    sudo apt-get install x264 v4l-utils

    # 可选依赖项,一般DL都需要
    sudo apt-get install libprotobuf-dev protobuf-compiler
    sudo apt-get install libgoogle-glog-dev libgflags-dev
    sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

python环境

    #python2.7与3接口
    sudo apt-get install python-dev python-pip python3-dev python3-pip
    sudo -H pip2 install -U pip numpy
    sudo -H pip3 install -U pip numpy
这里按照的python库一般在虚拟环境下进行,以防止和实际全局环境发成冲突
    # Install virtual environment
    sudo pip2 install virtualenv virtualenvwrapper
    sudo pip3 install virtualenv virtualenvwrapper
    echo "# Virtual Environment Wrapper"  >> ~/.bashrc
    echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
    source ~/.bashrc

    ############ For Python 2 ############
    # create virtual environment
    mkvirtualenv facecourse-py2 -p python2
    workon facecourse-py2

    # now install python libraries within this virtual environment
    pip install numpy scipy matplotlib scikit-image scikit-learn ipython

    # quit virtual environment
    deactivate
    ######################################

    ############ For Python 3 ############
    # create virtual environment
    mkvirtualenv facecourse-py3 -p python3
    workon facecourse-py3

    # now install python libraries within this virtual environment
    pip install numpy scipy matplotlib scikit-image scikit-learn ipython

    # quit virtual environment
    deactivate
    ######################################

OpenCV与OpenCV_contrib

此处均从github的源码下载
    git clone https://github.com/opencv/opencv.git
    cd opencv
    git checkout 3.3.1
    cd ..
    #contrib
    git clone https://github.com/opencv/opencv_contrib.git
    cd opencv_contrib
    git checkout 3.3.1
    cd ..

开始编译

    cd opencv
    mkdir build
    cd build

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D BUILD_EXAMPLES=ON ..

      # find out number of CPU cores in your machine
     nproc
     # substitute 4 by output of nproc
     make -j4
     sudo make install
     sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
     sudo ldconfig

\(python使用opencv需要将opencv编译的cv2.so加入到python环境中,实际放在python \\ 下的site-package或者dist-package,查找cv2.so位置\)

    find /usr/local/lib -type f -name "cv2*.so"

    # 结果出现可能如下
    ############ For Python 2 ############
    ## binary installed in dist-packages
    /usr/local/lib/python2.6/dist-packages/cv2.so
    /usr/local/lib/python2.7/dist-packages/cv2.so
    ## binary installed in site-packages
    /usr/local/lib/python2.6/site-packages/cv2.so
    /usr/local/lib/python2.7/site-packages/cv2.so

    ############ For Python 3 ############
    ## binary installed in dist-packages
    /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so
    /usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so
    ## binary installed in site-packages
    /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
    /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so

制作软连接,需要注意路径

    ############ For Python 2 ############
    cd ~/.virtualenvs/facecourse-py2/lib/python2.7/site-packages
    ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so

    ############ For Python 3 ############
    cd ~/.virtualenvs/facecourse-py3/lib/python3.6/site-packages
    ln -s /usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

测试OpenCV

C++接口

    # compile
    g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes
    # run
    ./removeRedEyes

python 接口

激活虚拟环境

  ############ For Python 2 ############
  workon facecourse-py2

  ############ For Python 3 ############
  workon facecourse-py3

  ipython
  import cv2
  print cv2.__version__

  # 或者执行python removeRedEyes.py

  # 退出虚拟环境
  deactivate

本文作者: 张峰
本文链接: http://www.enjoyai.site/2017/11/23/InstallOpenCV/
版权声明: 本博客所有文章,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!

posted @ 2017-11-23 21:43  圆滚滚的小峰峰  阅读(1393)  评论(0编辑  收藏  举报