<button id="ssm6u"><optgroup id="ssm6u"></optgroup></button>
  • 跳轉至

    Ubuntu從源碼編譯OpenCV

    概要

    本文講解了如何從編碼開始編譯OpenCV

    keywords OpenCV Linux 編譯

    下載源碼

    下載下面的兩個包.

    https://github.com/opencv/opencv

    https://github.com/opencv/opencv_contrib

    opencv_contrib 包里面放著一些opencv 中的非免費包, 不能用于商業.

    有專利保護. 比如xfeature2d 就是在opencv_contrib中的.

    $ git clone https://github.com/opencv/opencv.git
    $ git clone https://github.com/opencv/opencv_contrib.git
    

    安裝依賴

    閱讀OpenCV 編譯的官方文檔

    安裝需要的第三方包.

    https://docs.opencv.org/trunk/d7/d9f/tutorial_linux_install.html

    [安裝編譯器] sudo apt-get install build-essential
    [必須依賴] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
    [可選依賴] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
    

    可選依賴,如果安裝python-opencv還是需要的。

    可能還有別的依賴, 后續, 如果不全的話會報錯. 繼續裝就是了.

    注意, 還有python3的numpy 也要裝

    安裝Numpy

    分別給python2 跟python3 通過pip/pip3 安裝numpy

    numpyopencv 必須要依賴的包.

    python3 安裝numpy

    提前裝好,免得后面麻煩.

    sudo  apt-get install python3-numpy
    

    或者使用pip3進行安裝.

    sudo pip3 install numpy
    

    不過他們的安裝路徑不太一樣

    可以通過如下的方式獲取包的安裝路徑

    import numpy
    numpy.__path__
    

    如果是python2的話也類似

     sudo apt-get install python-numpy
    

    使用Anaconda的朋友, 可能需要自己在后續的CMakeGUI那自行配置一下python3的相關路徑

    CMakeGUI

    這里是視頻教程.

    Youtube - TaiWan - how to build opencv on Ubuntu Linux
    https://www.youtube.com/watch?v=NcZpA55ISzo

    安裝cmake-gui 也就是cmake的圖形化界面, 配置起來更方便一些.

    sudo apt-get install cmake-qt-gui
    

    運行

    以管理員身份

    sudo cmake-gui
    

    利用cmake 打開opencv 源碼所在的文件夾.

    where is the source code -> opencv的路徑

    為產生的二進制文件,單獨創建一個文件夾opencv_bin

    配置configure

    設定默認配置 Unix xxxx

    然后就會加載出一系列需要選擇的參數.

    直接在搜索框中搜索你想要查詢的參數.

    CMAKE-GUI的配置

    OPENCV_EXTRA_MOUDLES_PATH 這個參數是最重要, 需要填入第三方拓展包的路徑

    /home/fange/XXXX/opencv_contrib/modules
    

    注意 后面是子文件夾modules的路徑哦!!!

    首先是,python2 跟python3的系列參數, 如果使用系統默認的python, 都會被自動檢測到.

    • PYTHON3_EXECUTABLE

    • PYTHON3_INCLUDE_DIR

    • PYTHON3_LIBRARY_DIR

    • ......

    接下來,是查看python2/python3的numpy路徑是否正確. PYTHON3_NUMPY_INCLUDE_DIRS

    查看路徑的方式也比較簡單:

    import numpy
    numpy.__path__
    

    如果你還有其他需求可以自行配置.

    例如跟OpenCV的Tensorflow擴展,PyTorch 拓展等等, 需要自己配置路徑.

    Generate生成安裝用的二進制文件

    在CMAKEGUI中配置好了之后, 可以點擊Generate 按鈕, 如果你的環境不滿足依賴, 需要根據提示, 安裝對應的包.

    如果滿足依賴, 則可以成功的編譯出安裝所需的二進制文件.

    不出意外,這里可能會卡你一下, 因為下載確實慢

    data: Download: face_landmark_model.dat
    

    需要下載很多數據, 中間可能會斷開, 沒關系 多試幾次.

    生成日志如下

    Looking for ccache - not found
    Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found suitable version "1.2.8", minimum required is "1.2.3") 
    Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.8") 
    Checking for module 'gtk+-3.0'
      No package 'gtk+-3.0' found
    Checking for module 'gstreamer-base-1.0'
      No package 'gstreamer-base-1.0' found
    Checking for module 'gstreamer-video-1.0'
      No package 'gstreamer-video-1.0' found
    Checking for module 'gstreamer-app-1.0'
      No package 'gstreamer-app-1.0' found
    Checking for module 'gstreamer-riff-1.0'
      No package 'gstreamer-riff-1.0' found
    Checking for module 'gstreamer-pbutils-1.0'
      No package 'gstreamer-pbutils-1.0' found
    Checking for module 'gstreamer-base-0.10'
      No package 'gstreamer-base-0.10' found
    Checking for module 'gstreamer-video-0.10'
      No package 'gstreamer-video-0.10' found
    Checking for module 'gstreamer-app-0.10'
      No package 'gstreamer-app-0.10' found
    Checking for module 'gstreamer-riff-0.10'
      No package 'gstreamer-riff-0.10' found
    Checking for module 'gstreamer-pbutils-0.10'
      No package 'gstreamer-pbutils-0.10' found
    Looking for linux/videodev.h
    Looking for linux/videodev.h - not found
    Looking for linux/videodev2.h
    Looking for linux/videodev2.h - found
    Looking for sys/videoio.h
    Looking for sys/videoio.h - not found
    Checking for module 'libavresample'
      No package 'libavresample' found
    Checking for module 'libgphoto2'
      No package 'libgphoto2' found
    found Intel IPP (ICV version): 2017.0.3 [2017.0.3]
    at: /home/fange/Software/OpenCVSRC/opencv_bin/3rdparty/ippicv/ippicv_lnx
    found Intel IPP IW sources: 2017.0.3
    at: /home/fange/Software/OpenCVSRC/opencv_bin/3rdparty/ippicv/ippiw_lnx
    Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
    Could NOT find Atlas (missing:  Atlas_CLAPACK_INCLUDE_DIR Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY) 
    A library with BLAS API found.
    A library with LAPACK API found.
    Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
    Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN) 
    The imported target "vtkRenderingPythonTkWidgets" references the file
       "/usr/lib/x86_64-linux-gnu/libvtkRenderingPythonTkWidgets.so"
    but this file does not exist.  Possible reasons include:
    
    * The file was deleted, renamed, or moved to another location.
    
    * An install or uninstall procedure did not complete successfully.
    
    * The installation package was faulty and contained
       "/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
    but not all the files it references.
    
    The imported target "vtk" references the file
       "/usr/bin/vtk"
    but this file does not exist.  Possible reasons include:
    
    * The file was deleted, renamed, or moved to another location.
    
    * An install or uninstall procedure did not complete successfully.
    
    * The installation package was faulty and contained
       "/usr/lib/cmake/vtk-6.2/VTKTargets.cmake"
    but not all the files it references.
    
    Found VTK 6.2.0 (/usr/lib/cmake/vtk-6.2/UseVTK.cmake)
    Caffe:   NO
    Protobuf:   NO
    Glog:   NO
    freetype2:   YES
    harfbuzz:    YES
    No preference for use of exported gflags CMake configuration set, and no hints for include/library directories provided. Defaulting to preferring an installed/exported gflags CMake configuration if available.
    Failed to find installed gflags CMake configuration, searching for gflags build directories exported with CMake.
    Failed to find gflags - Failed to find an installed/exported CMake configuration for gflags, will perform search for installed gflags components.
    Failed to find gflags - Could not find gflags include directory, set GFLAGS_INCLUDE_DIR to directory containing gflags/gflags.h
    Failed to find glog - Could not find glog include directory, set GLOG_INCLUDE_DIR to directory containing glog/logging.h
    Module opencv_sfm disabled because the following dependencies are not found: Glog/Gflags
    Module opencv_ovis disabled because OGRE3D was not found
    data: Download: face_landmark_model.dat
    CMake Warning at /home/fange/Software/OpenCVSRC/opencv/cmake/OpenCVDownload.cmake:190 (message):
      data: Download failed: 28;"Timeout was reached"
    
      For details please refer to the download log file:
    
      /home/fange/Software/OpenCVSRC/opencv_bin/CMakeDownloadLog.txt
    
    Call Stack (most recent call first):
      /home/fange/Software/OpenCVSRC/opencv_contrib/modules/face/CMakeLists.txt:13 (ocv_download)
    
    
    CMake Warning at /home/fange/Software/OpenCVSRC/opencv_contrib/modules/face/CMakeLists.txt:26 (message):
      Face: Can't get model file for face alignment.
    
    
    freetype2:   YES
    harfbuzz:    YES
    Checking for modules 'tesseract;lept'
      No package 'tesseract' found
      No package 'lept' found
    Tesseract:   NO
    xfeatures2d/boostdesc: Download: boostdesc_lbgm.i
    xfeatures2d/vgg: Download: vgg_generated_48.i
    xfeatures2d/vgg: Download: vgg_generated_64.i
    xfeatures2d/vgg: Download: vgg_generated_80.i
    xfeatures2d/vgg: Download: vgg_generated_120.i
    
    General configuration for OpenCV 4.0.0-pre =====================================
      Version control:               3.4.1-410-g84b3b5b
    
      Extra modules:
        Location (extra):            /home/fange/Software/OpenCVSRC/opencv_contrib/modules
        Version control (extra):     3.4.1-85-gfbc4d82
    
      Platform:
        Timestamp:                   2018-04-30T13:33:51Z
        Host:                        Linux 4.13.0-39-generic x86_64
        CMake:                       3.5.1
        CMake generator:             Unix Makefiles
        CMake build tool:            /usr/bin/make
        Configuration:               Release
    
      CPU/HW features:
        Baseline:                    SSE SSE2 SSE3
          requested:                 SSE3
        Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
          requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
          SSE4_1 (3 files):          + SSSE3 SSE4_1
          SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
          FP16 (2 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
          AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
          AVX2 (9 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
          AVX512_SKX (1 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX
    
      C/C++:
        Built as dynamic libs?:      YES
        C++ Compiler:                /usr/bin/c++  (ver 5.4.0)
        C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
        C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
        C Compiler:                  /usr/bin/cc
        C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
        C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
        Linker flags (Release):      
        Linker flags (Debug):        
        ccache:                      NO
        Precompiled headers:         YES
        Extra dependencies:          dl m pthread rt
        3rdparty dependencies:
    
      OpenCV modules:
        To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz xfeatures2d ximgproc xobjdetect xphoto
        Disabled:                    js world
        Disabled by dependency:      -
        Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java matlab ovis sfm
        Applications:                tests perf_tests apps
        Documentation:               NO
        Non-free algorithms:         NO
    
      GUI: 
        GTK+:                        YES (ver 2.24.30)
          GThread :                  YES (ver 2.48.2)
          GtkGlExt:                  NO
        VTK support:                 YES (ver 6.2.0)
    
      Media I/O: 
        ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
        JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
        WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x0202)
        PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.54)
        TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.6)
        JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
        OpenEXR:                     build (ver 1.7.1)
    
      Video I/O:
        DC1394:                      YES (ver 2.2.4)
        FFMPEG:                      YES
          avcodec:                   YES (ver 56.60.100)
          avformat:                  YES (ver 56.40.101)
          avutil:                    YES (ver 54.31.100)
          swscale:                   YES (ver 3.1.101)
          avresample:                NO
        GStreamer:                   NO
        libv4l/libv4l2:              NO
        v4l/v4l2:                    linux/videodev2.h
        gPhoto2:                     NO
    
      Parallel framework:            pthreads
    
      Trace:                         YES (with Intel ITT)
    
      Other third-party libraries:
        Intel IPP:                   2017.0.3 [2017.0.3]
               at:                   /home/fange/Software/OpenCVSRC/opencv_bin/3rdparty/ippicv/ippicv_lnx
        Intel IPP IW:                sources (2017.0.3)
                  at:                /home/fange/Software/OpenCVSRC/opencv_bin/3rdparty/ippicv/ippiw_lnx
        Lapack:                      NO
        Eigen:                       YES (ver 3.2.92)
        Custom HAL:                  NO
        Protobuf:                    build (3.5.1)
    
      OpenCL:                        YES (no extra features)
        Include path:                /home/fange/Software/OpenCVSRC/opencv/3rdparty/include/opencl/1.2
        Link libraries:              Dynamic load
    
      Python 2:
        Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
        Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
        numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0)
        packages path:               lib/python2.7/dist-packages
    
      Python 3:
        Interpreter:                 /usr/bin/python3 (ver 3.5.2)
        Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
        numpy:                       /home/fange/.local/lib/python3.5/site-packages/numpy/core/include (ver )
        packages path:               lib/python3.5/dist-packages
    
      Python (for build):            /usr/bin/python2.7
    
      Java:                          
        ant:                         NO
        JNI:                         NO
        Java wrappers:               NO
        Java tests:                  NO
    
      Matlab:                        NO
    
      Install to:                    /usr/local
    -----------------------------------------------------------------
    
    Configuring done
    Generating done
    

    通過這份文件, 我們可以清楚的知道, 那些包/功能, 是可以并將會安裝的.

    并告知你, opencv的安裝路徑是/usr/local

    安裝OpenCV

    進入到生成的二進制文件夾內執行

    $ sudo make install
    

    編譯時間較長, 請耐心等待.

    編譯過程

    測試代碼

    代碼與運行代碼的方式,寫在了文件夾 OpenCV測試程序CPP跟PY

    CPP 需要編寫對應的CMakeList.txt

    比較簡單的方式是

    $ python
    
    >>> import cv2
    >>> cv2.__version__
    

    最后的展示效果

    效果展示


    韩国精品无码一区二区三区,精品无码一区二区三区AV,欧洲丰满美熟女乱又伦AV,亚洲午夜久久久影院伊人