1. 程式人生 > >Windows下VS2017編譯OpenCV 3.4.0-rc

Windows下VS2017編譯OpenCV 3.4.0-rc

打開 extra 地址 erl lap 頭文件 mas encode master

簡述

很久沒有用過OpenCV了,這次需要做一點圖像處理相關的工作,又需要用起來,這裏記錄一下編譯的過程。之前介紹過使用vs2015編譯opencv2.4的帖子在這裏。
編譯好的文件在這裏https://pan.baidu.com/s/1qXCWxkw

1、下載源碼

這裏就不下載源碼壓縮包了,直接從github上克隆一下。

# 因為訪問github較慢,這裏直接使用的國內碼雲同步倉庫
git clone https://gitee.com/mirrors/opencv.git
# github上的地址為:https://github.com/opencv/opencv.git

克隆之後可以將3.4.0-rc分支打包出來,你也可以直接切換到3.4.0-rc分支或者直接使用master的代碼。

cd opencv
git archive -o ../opencv3.4.0.zip 3.4.0-rc

打包出來的壓縮包是不含git倉庫的相關文件的,不是很大,可以解壓到你想解壓的目錄。

2、使用cmake生成VS工程

我這裏就沒有使用命令行,直接使用的cmake-gui進行的配置。
選擇源碼目錄和構建目錄之後,點擊configure按鈕,然後選擇編譯器為visual studio 2015 2017 win64,等待配置結束。(配置的過程中會去下載opencv_ffmpeg.dll等文件,過程可能比較慢)

我使用的構建選項改變如下(Tools-->My Changes)

Commandline options:
-DBUILD_JAVA:BOOL="0" -DENABLE_LTO:BOOL="1" -DWITH_GSTREAMER:BOOL="0" -DCPACK_BINARY_ZIP:BOOL="1" -DBUILD_TESTS:BOOL="0" -DENABLE_CXX11:BOOL="1" -DBUILD_PERF_TESTS:BOOL="0" -DCPACK_SOURCE_7Z:BOOL="0" -DCPACK_BINARY_NSIS:BOOL="0" 


Cache file:
BUILD_JAVA:BOOL=0
ENABLE_LTO:BOOL=1
WITH_GSTREAMER:BOOL=0
CPACK_BINARY_ZIP:BOOL=1
BUILD_TESTS:BOOL=0
ENABLE_CXX11:BOOL=1
BUILD_PERF_TESTS:BOOL=0
CPACK_SOURCE_7Z:BOOL=0
CPACK_BINARY_NSIS:BOOL=0

配置輸出信息如下

General configuration for OpenCV 3.4.0-rc =====================================
  Version control:               unknown

  Platform:
    Timestamp:                   2017-12-20T08:35:12Z
    Host:                        Windows 10.0.14393 AMD64
    CMake:                       3.7.2
    CMake generator:             Visual Studio 15 2017 Win64
    CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe
    MSVC:                        1912

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2
      SSE4_1 (3 files):          + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 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

  C/C++:
    Built as dynamic libs?:      YES
    C++11:                       YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x64/cl.exe  (ver 19.12.25831.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /GL /Oi      /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP4  /MD /O2 /Ob2 /DNDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /GL /Oi      /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP4  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/bin/Hostx86/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /GL /Oi        /MP4  /MD /O2 /Ob2 /DNDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /GL /Oi        /MP4  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /LTCG /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64  /LTCG /debug /INCREMENTAL 
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann highgui imgcodecs imgproc ml objdetect photo python_bindings_generator shape stitching superres ts video videoio videostab
    Disabled:                    js world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz
    Applications:                apps
    Documentation:               YES (C:/Program Files/doxygen/bin/doxygen.exe 1.8.10)
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.34)
    TIFF:                        build (ver 42 - 4.0.9)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)

  Video I/O:
    Video for Windows:           YES
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (ver 57.89.100)
      avformat:                  YES (ver 57.71.100)
      avutil:                    YES (ver 55.58.100)
      swscale:                   YES (ver 4.6.100)
      avresample:                YES (ver 3.5.0)
    DirectShow:                  YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2017.0.3 [2017.0.3]
           at:                   C:/OpenCV/opencv3.4.0/build/3rdparty/ippicv/ippicv_win
    Intel IPP IW:                sources (2017.0.3)
              at:                C:/OpenCV/opencv3.4.0/build/3rdparty/ippicv/ippiw_win
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO

  NVIDIA CUDA:                   NO

  OpenCL:                        YES (no extra features)
    Include path:                C:/OpenCV/opencv3.4.0/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            C:/Program Files/Python36/python.exe

  Matlab:                        NO

  Install to:                    C:/OpenCV/opencv3.4.0/build/install
-----------------------------------------------------------------

Configuring done

配置完成後點擊Generate按鈕創建VS解決方案即可。

3、編譯

你可以直接進入cmake的構建輸出目錄,雙擊OpenCV.sln使用VS2017打開,然後構建。
也可以使用命令行進行編譯

# Release版構建命令如下
C:\OpenCV\opencv3.4.0\build>msbuild /p:configuration=Release /maxcpucount:4 OpenCV.sln
# Debug版本只需要將上面的Release改為Debug即可
# 構建完成後,使用下面命令進行安裝(安裝輸出到build下的install目錄,實際上面構建完成就已經安裝)
C:\OpenCV\opencv3.4.0\build>msbuild /p:configuration=Release /maxcpucount:4  INSTALL

編譯完成安裝後,即可到build/install目錄下查看相關的頭文件和庫文件。

4、遇到的錯誤和解決辦法

1、perl --version錯誤

       “C:\OpenCV\opencv3.4.0\build\OpenCV.sln”(默認目標) (1) ->
       “C:\OpenCV\opencv3.4.0\build\doc\doxygen_cpp.vcxproj.metaproj”(默認目標) (50) ->
       “C:\OpenCV\opencv3.4.0\build\doc\doxygen_cpp.vcxproj”(默認目標) (52) ->
       (CustomBuild 目標) ->
         CUSTOMBUILD : error : Problems running bibtex. Verify that the command ‘perl --version‘ works from the command
        line. Exit code: 1 [C:\OpenCV\opencv3.4.0\build\doc\doxygen_cpp.vcxproj]

    46 個警告
    1 個錯誤

這個錯誤與docgen_cpp.vcxproj這個工程相關,那麽是生成文檔相關的,這裏就不該了,直接去掉doxygen文檔生成選項。
在命令行參數中添加-DBUILD_DOCS:BOOL="0"或者在cmake-gui中將BUILD_DOC選中的勾去掉。
然後重新ConfigureGenerate一下。然後重新編譯即可。

Windows下VS2017編譯OpenCV 3.4.0-rc