Ubuntu下编译安装Opencv(任何版本都通用)

前言

之前一直捣鼓想在ubuntu下安装编译opencv的版本,但是无奈一直没有成功。最近终于搞定,特此记录一下。

第一步: 更新一下ubuntu下的包

  • 更新一下ubuntu下的包
    sudo apt-get update
    sudo apt-get upgrade
    

第二步:安装操作系统需要的相应的库

  • 安装操作系统需要的相应的库
    #移除之前x264
    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 qt5-default 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
     
    # 可选的依赖,建议都装了吧
    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库

  • 安装python库,如果你之前装了python3啥的就不用安装了。下面主要是安装python的组件和numpy
    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
    

第四步:创建虚拟环境(可选)

  • 这里创建了虚拟环境安装,好处是隔离环境。如果不需要的话可以略过。
    # 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 and OpenCV_contrib

  • 下载OpenCV 和 OpenCV_contrib
    git clone https://github.com/opencv/opencv.git
    cd opencv
    # 这里的checkout是将版本切换到3.3.1,原来直接从github下载整个opencv就可以切换版本的,之前我不知道,安装的总是最新的... 
    git checkout 3.3.1 
    cd ..
    ######################################################################
    # 对于opencv_contrib也是同理
    git clone https://github.com/opencv/opencv_contrib.git
    cd opencv_contrib
    git checkout 3.3.1
    cd ..
    

第六步:编译和安装带contrib模块的Opencv

  • 编译和安装带contrib模块的Opencv
    • 先创建一个build目录

      cd opencv
      mkdir build
      cd build
      
    • CMake

      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 ..
      
    • 编译和安装
      可以通过nproc来查看你的cpu支持多少线程。我的支持12线程,那我就用12线程吧。

      make -j12
      sudo make install
      sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
      sudo ldconfig
      
    • 为虚拟环境创建符号链接(软链接)(可选)

      Depending upon Python version you have, paths would be different. OpenCV’s Python binary (cv2.so) can be installed either in directory site-packages or dist-packages. Use the following command to find out the correct location on your machine.

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

      It should output paths similar to one of these (or two in case OpenCV was compiled for both Python2 and Python3):

      ############ 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
      

      Double check the exact path on your machine before running the following commands

      ############ 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
      

第七步:测试Opencv3

  • 测试Opencv3

    We will test a red eye remover application written in OpenCV to test our C++ and Python installations. Download RedEyeRemover.zip and extract it into a folder.

    文件地址:RedEyeRemover.zip

    • 首先测试CPP代码
      # compile
      # There are backticks ( ` ) around pkg-config command not single quotes
      g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes
      # run
      ./removeRedEyes
      
    • 然后测试Pyhont代码
      • 启动python虚拟环境(可选)
        ############ For Python 2 ############
        workon facecourse-py2
         
        ############ For Python 3 ############
        workon facecourse-py3
        
      • 快速测试
        # open ipython (run this command on terminal)
        ipython
        # import cv2 and print version (run following commands in ipython)
        import cv2
        print cv2.__version__
        # If OpenCV3 is installed correctly,
        # above command should give output 3.3.1
        # Press CTRL+D to exit ipython
        
      • 或者运行RedEyeRemoverdemo
        python removeRedEyes.py
      • 退出python虚拟环境(可选)
        deactivate

参考

  1. Install OpenCV3 on Ubuntu

猜你喜欢

转载自blog.csdn.net/luke_sanjayzzzhong/article/details/89514731