4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WindowsにOpenCVをビルドしてインストールする

Last updated at Posted at 2017-05-29

Extra module,Eigen,Qt,TBB,VTK,XIMEAを有効にしてOpenCVをビルドします。
パスなどはバージョンによって異なるので適宜読み替えてください。
今回実施した環境は以下の通りです。

環境

  • Windows10 (64bit)
  • Visual Studio 2015 Community
  • Python 2.7.13, 3.5.3
  • OpenCV 3.2.0
  • Eigen 3.3.3
  • Qt 5.8
  • TBB 2017 Update 7
  • VTK 7.1.1
  • XIMEA

OpenCVのビルド

準備

Python&NumPyのインストール

  1. 公式サイトからPython2.7系とPython3系をダウンロードする。Cドライブ直下にインストールする。
  2. ここから自分の環境にあったNumPyをダウンロードする。Python2.7, 3系ともにpip install ~.whlでインストールする。

ソフトのインストール

  1. Visual StudioCMakeをインストール。

ソースのダウンロード

  1. GitHubからopencvopencv_contribのソースをダウンロード。それぞれC:\opencvC:\opencv\dev\opencv_contribに展開。

  2. Eigenのlatest stable releaseをダウンロード。C:\opencv\dev\eigenに展開。

  3. Qtをインストール。コンポーネントの選択ではQt>Qt 5.8>msvc2015 64-bitのみを選択しインストールする。

  4. Intel® TBBのLatest releaseをダウンロード。C:\opencv\dev\tbb\bin\intel64\vc14に展開。

  5. VTKのソースをダウンロード。C:\VTKに展開。管理者権限でCMakeを起動し,sourceC:\VTK,buildC:\VTK\buildとしてConfigure。Generate→Open ProjectをクリックしてVisual Studioでプロジェクトを開く。”ALL_BUILD”,”INSTALL”をそれぞれビルドするとC:\Program FilesにVTKがインストールされる。

  6. XIMEA Windows Software Packageをインストール。

  7. 以下のようにパスを通す。

    環境変数のパスを設定
    C:\opencv
    C:\opencv\dev\eigen
    C:\opencv\dev\tbb\bin\intel64\vc14
    C:\XIMEA\API\x64
    C:\Qt\5.8\msvc2015_64\bin
    C:\Program Files\VTK
    C:\Program Files\VTK\bin

ビルド

  1. CMakeを起動.sourceC:\opencv,buildC:\opencv\buildとしてConfigureをクリック。

  2. 以下の設定が完了するまでConfigureを何度か押して赤い項目をなくす。

    以下チェックを入れる
    WITH_EIGEN
    WITH_OPENGL
    WITH_QT
    WITH_TBB
    WITH_VTK
    WITH_XIMEA
    以下チェックを外す
    WITH_CUDA
    WITH_CUFFT
    Name Value
    OPENCV_EXTRA_MODULES_PATH C:/opencv/dev/opencv_contrib/modules
    EIGEN_INCLDE_PATH C:/opencv/dev/eigen
    QT_QMAKE_EXECUTSBLE C:/Qt/5.8/msvc2015_64/bin/qmake.exe
    Qt5Concurrent_DIR C:/Qt/5.8/msvc2015_64/lib/cmake/Qt5Concurrent
    Qt5Core_DIR C:/Qt/5.8/msvc2015_64/lib/cmake/Qt5Core
    Qt5Gui_DIR C:/Qt/5.8/msvc2015_64/lib/cmake/Qt5Gui
    Qt5OpenGL_DIR C:/Qt/5.8/msvc2015_64/lib/cmake/Qt5OpenGL
    Qt5Test_DIR C:/Qt/5.8/msvc2015_64/lib/cmake/Qt5Test
    Qt5Widgets_DIR C:/Qt/5.8/msvc2015_64/lib/cmake/Qt5Widgets
    TBB_ENV_INCLDE C:/opencv/dev/tbb/include
    TBB_ENV_LIB C:/opencv/dev/tbb/lib/intel64/vc14/tbb.lib
    TBB_ENV_LIB_DEBUG C:/opencv/dev/tbb/lib/intel64/vc14/tbb_debug.lib
    TBB_VER_FILE C:/opencv/dev/tbb/include/tbb/tbb_stddef.h
  3. 赤い項目がなければGenerate→Open Projectをクリック。Visual Studioでプロジェクトを開く。

  4. Visual Studioでプロジェクトが開かれたら,構成Release,プラットフォームx64とし,”ALL_BUILD”を右クリック→ビルド。エラーがなければ”INSTALL”を右クリック→ビルド。

  5. C:\Python27\Lib\site-packages,C:\Python35\Lib\site-packages内にそれぞれ,「cv2.pyd」,「cv2.cp35-win_amd64.pyd」ができる.

  6. C:\opencvC:\opencv\build\install\binにパスを通す.

stringutils.cppでエラーが出るとき

以下の3行をコメントアウトするとうまくいく。

stringutils.cpp
	static std::map<QString, QString> replacements3 = { { "EV", "AF" },
		                                            //{ "Ü", "A" },
		                                            //{ "Ö", "A" },
		                                            //{ "Ä", "A" },
		                                            { "O", "G" },
		                                            { "Z", "S" },
		                                            { "M", "N" },
		                                            { "KN", "N" },
		                                            { "K", "C" },
		                                            { "SCH", "SSS" },
		                                            { "PH", "FF" } };

確認

import cv2
print(cv2.getBuildInformation())
General configuration for OpenCV 3.2.0-dev =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            C:/opencv/dev/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2017-06-08T23:48:41Z
    Host:                        Windows 10.0.14393 AMD64
    CMake:                       3.8.1
    CMake generator:             Visual Studio 14 2015 Win64
    CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
    MSVC:                        1900

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3
      requested:                 SSSE3
    Dispatched code generation:  SSE4_1 FP16 AVX AVX2
      requested:                 SSE4_1 AVX FP16 AVX2
      SSE4_1 (0 files):          + SSE4_1
      FP16 (1 files):            + SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (1 files):             + SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (1 files):            + SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24215.1)
    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 /Oi       /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /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 /Oi       /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/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         /MP8  /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 /Oi         /MP8  /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL 
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 Qt5::Test Qt5::Concurrent Qt5::OpenGL vfw32 xiapi64 vtkRenderingOpenGL2 vtkCommonCore vtksys vtkCommonDataModel vtkCommonMath vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkIOImage vtkDICOMParser vtkmetaio vtkzlib vtkjpeg vtkpng vtktiff vtkImagingCore vtkRenderingCore vtkCommonColor vtkCommonComputationalGeometry vtkFiltersCore vtkFiltersGeneral vtkFiltersGeometry vtkFiltersSources vtkglew vtkInteractionStyle vtkFiltersExtraction vtkFiltersStatistics vtkImagingFourier vtkalglib vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOCore vtkFiltersTexture vtkRenderingFreeType vtkfreetype vtkIOExport vtkRenderingGL2PSOpenGL2 vtkgl2ps vtkIOGeometry vtkIOLegacy Qt5::Core Qt5::Gui Qt5::Widgets glu32 opengl32 C:/opencv/dev/tbb/lib/intel64/vc14/tbb.lib
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 core flann imgproc ml objdetect phase_unwrapping photo plot reg surface_matching video viz xphoto bgsegm dnn face fuzzy imgcodecs shape videoio xobjdetect highgui superres ts bioinspired dpm features2d line_descriptor saliency text calib3d ccalib cvv datasets rgbd stereo structured_light tracking videostab xfeatures2d ximgproc aruco optflow stitching python2 python3
    Disabled:                    world contrib_world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java cnn_3dobj dnn_modern freetype hdf matlab sfm

  Windows RT support:            NO

  GUI: 
    QT 5.x:                      YES (ver 5.8.0)
    QT OpenGL support:           YES (Qt5::OpenGL 5.8.0)
    OpenGL support:              YES (glu32 opengl32)
    VTK support:                 YES (ver 7.1.1)

  Media I/O: 
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.24)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (ver )
      avformat:                  YES (ver )
      avutil:                    YES (ver )
      swscale:                   YES (ver )
      avresample:                YES (ver )
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       YES
    Intel PerC:                  NO

  Parallel framework:            TBB (ver 2017.0 interface 9107)

  Other third-party libraries:
    Use Intel IPP:               2017.0.2 [2017.0.2]
               at:               C:/opencv/build/3rdparty/ippicv/ippicv_win
    Use Intel IPP IW:            prebuilt binaries (2017.0.2)
    Use Intel IPP Async:         NO
    Use Lapack:                  NO
    Use Eigen:                   YES (ver 3.3.3)
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                C:/opencv/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 C:/Python27/python.exe (ver 2.7.13)
    Libraries:                   C:/Python27/libs/python27.lib (ver 2.7.13)
    numpy:                       C:/Python27/lib/site-packages/numpy/core/include (ver 1.12.1)
    packages path:               C:/Python27/Lib/site-packages

  Python 3:
    Interpreter:                 C:/Python35/python.exe (ver 3.5.3)
    Libraries:                   C:/Python35/libs/python35.lib (ver 3.5.3)
    numpy:                       C:/Python35/lib/site-packages/numpy/core/include (ver 1.12.1)
    packages path:               C:/Python35/Lib/site-packages

  Python (for build):            C:/Python27/python.exe

  Java:
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     NO

  Tests and samples:
    Tests:                       YES
    Performance tests:           YES
    C/C++ Examples:              NO

  Install path:                  C:/opencv/build/install

  cvconfig.h is in:              C:/opencv/build
-----------------------------------------------------------------

確認2

import cv2
import numpy as np

im = cv2.imread("cat.png")
cv2.imshow("cat",im)
cv2.waitKey(0)
cv2.destroyAllWindows()

cat.png

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?