OpenCV ビルド 4.2.0
概要
Windows,Win32API,C++ 環境で、OpenCVを使用したプログラムを作成使用としている。
環境構築として最も簡単なのは、MSYS2で opencv をインストールし、g++でビルドする方法であり、当初はこの環境を使用していた。
諸事情により cl.exe でビルドする環境を使用したい。
この場合、OpenCVはソースからビルドする必要がある。
ビルド環境
- VisualStudio
- 2015 Community Update 3
 - VisualStudio 2015 Community インストール
 
 - CMake
- 3.16.2
 - CMake
 
 
ポイント
- ビルドには opencv (https://github.com/opencv/opencv.git) だけでなく opencv_contrib (https://github.com/opencv/opencv_contrib.git) が必要
 - CMakeを使用して VisualStudio のビルド設定ファイルを生成する
 - VisualStudio では "バッチビルド" を使用し、"INSTALL"プロジェクトを選択してビルドする
 - "CMAKE_INSTALL_PREFIX" で指定したディレクトリにインストールされる
- CMake の "Where to build the binaries:" で指定するディレクトリはビルド用のディレクトリであり、デフォルト設定だと "CMAKE_INSTALL_PREFIX" はこの下位層の /install ディレクトリとなる
 
 - MSYS2がインストールされているとCMakeが MSYS2 のライブラリのパスを自動設定するが、これらは無効にする
 
ここでのビルドオプションの前提
- opencv ソースコード
- D:/data/project_git_dl/opencv
 
 - opencv_contrib ソースコード
- D:/data/project_git_dl/opencv_contrib
 
 - ビルドディレクトリ
- D:/data/work_build/opencv
 
 - インストール先
- C:¥usr¥opencv
 
 - CPUアーキテクチャ
- Win32(32bit)
 
 
手順
ソースの取得
gitの使用法は割愛
- opencv (https://github.com/opencv/opencv.git) を git で取得し、4.2.0 ブランチに切り替え
 - opencv_contrib (https://github.com/opencv/opencv_contrib.git) を git で取得し、4.2.0 ブランチに切り替え
 
CMakeの操作
"Where is the source code:" と "Where to build the binaries:" を指定し、"Configure" をクリック
"Visual Studio 14 2015","Win32"を選択して "Finish"
"CMAKE_INSTALL_PREFIX"を指定
(MSYS2 のライブラリのパスを自動設定された場合)
"WITH_GSTREAMER"のチェックを外す。キャプチャは割愛するが GSTREAMER_XXX の一連の定義は "Remove Entry"する。
(MSYS2 のライブラリのパスを自動設定された場合)
"WITH_EIGEN"のチェックを外す。キャプチャは割愛するが "Eigen3_DIR" 定義は "Remove Entry"する。
(MSYS2 のライブラリのパスを自動設定された場合)
"BUILD_opencv_python_bindings_generator","BUILD_opencv_python_tests" のチェックを外す。キャプチャは割愛するが PYTHON2_XXX, PYTHON3_XXX の一連の定義は "Remove Entry"する。
"Generate" をクリック
Selecting Windows SDK version 10.0.14393.0 to target Windows 10.0.18362.
Detected processor: AMD64
sizeof(void) = 4 on 64 bit processor. Assume 32-bit compilation mode
libjpeg-turbo: VERSION = 2.0.2, BUILD = opencv-4.2.0-libjpeg-turbo
found Intel IPP (ICV version): 2019.0.0 [2019.0.0 Gold]
at: D:/data/work_build/opencv/3rdparty/ippicv/ippicv_win/icv
found Intel IPP Integration Wrappers sources: 2019.0.0
at: D:/data/work_build/opencv/3rdparty/ippicv/ippicv_win/iw
Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
Could NOT find BLAS (missing: BLAS_LIBRARIES) 
LAPACK requires BLAS
A library with LAPACK API not found. Please specify library location.
Could NOT find JNI (missing: JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
OpenCV Python: during development append to PYTHONPATH: D:/data/work_build/opencv/python_loader
Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx2.cpp
Excluding from source files list: <BUILD>/modules/core/test/test_intrin128.avx512_skx.cpp
Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx2.cpp
Excluding from source files list: <BUILD>/modules/core/test/test_intrin256.avx512_skx.cpp
Excluding from source files list: <BUILD>/modules/core/test/test_intrin512.avx512_skx.cpp
Excluding from source files list: modules/imgproc/src/imgwarp.avx2.cpp
Excluding from source files list: modules/imgproc/src/resize.avx2.cpp
Excluding from source files list: modules/imgproc/src/sumpixels.avx512_skx.cpp
Registering hook 'INIT_MODULE_SOURCES_opencv_dnn': D:/data/project_git_dl/opencv/modules/dnn/cmake/hooks/INIT_MODULE_SOURCES_opencv_dnn.cmake
opencv_dnn: filter out cuda4dnn source code
Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx2.cpp
Excluding from source files list: <BUILD>/modules/dnn/layers/layers_common.avx512_skx.cpp
Excluding from source files list: modules/features2d/src/fast.avx2.cpp
General configuration for OpenCV 4.2.0 =====================================
  Version control:               4.2.0
  Platform:
    Timestamp:                   2020-01-03T02:28:03Z
    Host:                        Windows 10.0.18362 AMD64
    CMake:                       3.16.2
    CMake generator:             Visual Studio 14 2015
    CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
    MSVC:                        1900
  CPU/HW features:
    Baseline:                    SSE SSE2
      requested:                 SSE2
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX
      requested:                 SSE4_1 SSE4_2 AVX FP16
      SSE4_1 (17 files):         + SSE3 SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSE3 SSSE3 SSE4_1 POPCNT SSE4_2 AVX
  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe  (ver 19.0.24215.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2 /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP8   /MD /O2 /Ob2 /DNDEBUG 
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2 /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP8   /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/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 /Oi  /fp:precise  /arch:SSE /arch:SSE2   /MP8    /MD /O2 /Ob2 /DNDEBUG 
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise  /arch:SSE /arch:SSE2   /MP8  /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:X86  /INCREMENTAL:NO 
    Linker flags (Debug):        /machine:X86  /debug /INCREMENTAL 
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:
    3rdparty dependencies:
  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
    Disabled:                    python_bindings_generator python_tests world
    Disabled by dependency:      -
    Unavailable:                 java js python2 python3
    Applications:                tests perf_tests apps
    Documentation:               NO
    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-libjpeg-turbo (ver 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES
  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.54.100)
      avformat:                  YES (58.29.100)
      avutil:                    YES (56.31.100)
      swscale:                   YES (5.5.100)
      avresample:                YES (4.0.0)
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES
  Parallel framework:            Concurrency
  Trace:                         YES (with Intel ITT)
  Other third-party libraries:
    Intel IPP:                   2019.0.0 Gold [2019.0.0]
           at:                   D:/data/work_build/opencv/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2019.0.0)
              at:                D:/data/work_build/opencv/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)
  OpenCL:                        YES (NVD3D11)
    Include path:                D:/data/project_git_dl/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load
  Python (for build):            NO
  Java:                          
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO
  Install to:                    C:/usr/opencv
-----------------------------------------------------------------
Configuring done
Generating done
"Open Project" をクリック
"バッチ ビルド" を選択
"INSTALL","Release"をチェックし、"リビルド"
ビルド結果
"CMAKE_INSTALL_PREFIX"で指定されたディレクトリ(今回は C:¥usr¥opencv)以下に、バイナリ、スタティックライブラリ、ダイナミックライブラリ、ヘッダがインストールされる
下記のようなディレクトリ構成となる
- opencv
- etc
 - include
- opencv2
 
 - x86
- vc14
- bin
 - lib
 
 
 - vc14