Visual Studio 2017을 이용하여 OpenCV 메인 소스와 OpenCV Extra module 소스를 모두 빌드하는 방법을 설명합니다.

더불어 Python 3.x 버전에서 OpenCV 3.3을 사용하는 환경을 구축하는 방법까지 설명합니다.



1. OpenCV 3.3.0 소스 코드 다운로드
   - https://github.com/opencv/opencv/releases
   - https://github.com/opencv/opencv_contrib/releases

2. Anaconda 다운로드 & 설치: https://www.continuum.io/downloads

3. CMake 다운로드 & 설치: https://cmake.org/download/

4. C:\opencv 폴더에 OpenCV 3.3.0 소스 코드 압축 풀기

   - C:\opencv ┬ mybuild
                    ├ opencv-3.3.0
                    └ opencv_contrib-3.3.0

5. CMake를 이용하여 opencv.sln 파일 생성

6. Visual Studio 2017을 이용하여 빌드 (Release & Debug)


Posted by kkokkal
:

OpenCV 3.3 버전이 정식으로 릴리즈되었습니다. 원래 7월 중순으로 스케줄이 잡혀있었습니다만 약간 늦어져서 어제 릴리즈가 되었네요. OpenCV 3.3 버전에 대한 소개는 아래 링크를 참고하세요.

http://opencv.org/opencv-3-3.html


OpenCV 3.3 버전에서 가장 큰 변경점은 dnn(deep neural network) 모듈이 이전에 opencv_contrib 쪽에 있다가 이번에 메인인 opencv 소스쪽으로 옮겨졌다는 것입니다. 그리고 Halide(할라이드)라는 새로운 영상 처리 전용 언어를 처음으로 적용하였고, 다양한 최적화와 기능 향상을 추가했다고 합니다. 좀 더 자세한 변경점은 아래 링크를 참고하시구요..

https://github.com/opencv/opencv/wiki/ChangeLog


위 페이지에 나타난 몇가지 항목에 대해 간략한 번역을 해봤습니다. (번역하지 않은 문장도 있으니 자세한 사항은 원문 링크를 참고하세요.)


  • High-level API has been modified and is even more convenient now.
    고수준 API가 개선되어 사용하기 더 편리해졌습니다.

  • The regression tests have been expanded, some new tests have been added. Now, there are 46 of them.
    회귀 테스트가 확장었고, 몇몇 새로운 테스트가 추가되었습니다. 이제 46개의 테스트가 있습니다.

  • Many bugs have been fixed in Torch and TF loaders, as well as in some processing layers. Now we check that on a certain set of networks the results from OpenCV DNN match or very close to the results from the original frameworks. We also check that the results claimed in the papers for such networks are achievable with OpenCV DNN.
    Torch와 TF 로더(loader)와 몇몇 처리 레이어에 있던 많은 버그가 수정되었습니다. 이제 특정 네트워크에서 OpenCV DNN의 결과와 원본 프레임워크의 결과가 일치하는지를 확인합니다. 또한 특정 논문에서 주장하는 결과를 OpenCV DNN을 통해 달성할 수 있는지를 확인합니다.

  • Performance has been substantially improved. Layer fusion has been implemented and some performance-critical layers have been optimized using AVX, AVX2, SSE and NEON. An external BLAS (OpenBLAS, MKL, ATLAS) is not needed anymore.
    성능이 크게 향상되었습니다. 레이어 융햡이 구현되었으며 몇몇 성능이 중요한 레이어는 AVX, AVX2, SSE, NEON 등을 사용하여 최적화되었습니다. OpenBLAS, MKL, ATLAS 같은 외부 BLAS는 더이상 필요하지 않습니다.

  • New samples in C++ and Python have been added.
    새로운 C++과 Python 예제가 추가되었습니다.

  • The optional Halide backend has been added. It can accelerate OpenCV DNN on GPU when the GPU is fast enough. See Deep Learning in OpenCV for details about the module.
    Halide 코드가 추가되었습니다. 이로써 GPU가 충분히 빠른 경우 GPU에서 OpenCV DNN을 빠르게 동작시킬 수 있습니다. 모듈에 대한 자세한 내용은 "Deep Learning in OpenCV"을 참고하십시오.

  • Upgraded IPPICV from 2015.12 to 2017.2 version brought ~15% speed improvement into core and imgproc modules (measured as geometrical mean over the corresponding performance tests).
    IPPICV가 2015.12 버전에서 2017.2 버전으로 업그레이드되어 core와 imgproc 모듈에서 속도가 15%까지 향상되었습니다.

  • Dynamic dispatching of SSE4.2/AVX/AVX2 code has been implemented. Previously, OpenCV had to be built with SSE4.x/AVX/AVX2 turned on in order to use such optimizations and that made it incompatible with older hardware. Now the OpenCV binaries automatically adapt to the real hardware and make use of new instructions if they are available while retaining compatibility with older hardware. All the existing AVX/AVX2 optimizations in OpenCV have been refactored to use this technology. AVX acceleration of DNN also uses dynamic dispatching.
    SSE4.2/AVX/AVX2 코드의 동적 디스패칭이 구현되었습니다. 이전에는 이러한 최적화를 사용하기 위해 SSE4.x/AVX/AVX2를 켜고 OpenCV를 빌드해야했기 때문에 OpenCV는 이전 하드웨어와 호환되지 않았습니다. 이제 OpenCV 바이너리는 실제 하드웨어에 자동으로 적응하고 구형 하드웨어와의 호환성을 유지하면서 사용 가능한 새로운 명령어를 사용합니다. 기존에 있던 모든 OpenCV AVX/AVX2 최적화 기법은 리펙토링되어 이러한 기술을 사용합니다. DNN을 위한 AVX 가속화도 동적 디스패치를 사용합니다.

  • OpenCV can now be configured and built as C++ 11 library. Pass -DENABLE_CXX11=ON to CMake. On some modern Linux distributions, like the latest Fedora, it’s enabled by default.
    OpenCV는 이제 C++11 라이브러리를 사용하여 구성할 수 있습니다. CMake에서 -DENABLE_CXX11=ON 을 설정하세요. 최신 Fedora같은 최근의 리눅스 배포판에서는 C++11 구성이 기본으로 활성화되어 있습니다.

  • New features for C++ 11 users have been added:
    C++11 사용자를 위한 새로운 기능이 추가되었습니다.

  • Support for hardware-accelerated video encoding/decoding using Intel GPUs through Intel Media SDK has been implemented for Linux (in the form of backends for cv::VideoCapture and cv::VideoWriter).
    리눅스에서 인텔 미디어 SDK를 통해 인텔 GPU를 사용하는 하드웨어 가속 비디오 인코딩/디코딩에 대한 지원이 구현되었습니다.

OpenCV 3.3은 아래 링크에서 다운로드 받을 수 있습니다.

https://github.com/opencv/opencv/releases/tag/3.3.0


위 사이트에서 opencv-3.3.0-vc14.exe 파일은 Windows 환경에서 Visual Studio 2015를 이용하여 미리 빌드한 LIB, DLL 파일을 제공합니다. 그러나 이 DLL 파일은 opencv_contrib 쪽 코드는 포함되어 있지 않기 때문에 opencv_contrib 코드까지 사용하려면 아래 두 소스 코드 압축 파일을 다운로드 받아 직접 빌드해야 합니다.

https://github.com/opencv/opencv/archive/3.3.0.zip

https://github.com/opencv/opencv_contrib/archive/3.3.0.zip


이전에 OpenCV 3.3.0-rc 버전의 경우에는 structured_edge_detection.cpp 파일을 수정해야 정상 빌드가 되었었는데요("OpenCV 3.3.0-rc 버전 릴리즈 & Visual Studio에서 빌드하기" 글 참조), OpenCV 3.3 정식 릴리즈에서는 이 코드가 수정이 되어 있네요 :-)


그렇기때문에 CMake로 적당히 설정하면 잘 빌드됩니다. 실제로도 Visual Studio 2017 버전으로 잘 빌드되는 것을 확인했구요.. 추후 시간이 되면 Visual Studio 2017 버전으로 OpenCV 3.3을 빌드하는 방법을 간략히 정리해보도록 하죠.



Posted by kkokkal
:

올해 2월에 조립 PC를 새로 장만해서 Visual Studio 2015를 깔았고, 이후 Visual Studio 2017 버전이 나와서 Visual Studio 2015를 지우고 Visual Studio 2017만 사용하려고 했었습니다. 그런데 nVidia Cuda가 Visual Studio 2017을 아직 지원하지 않아서 다시 Visual Studio 2015를 설치를 했습니다. 그리고나서 OpenCV 최신 소스를 Visual Studio 2015로 새로 빌드하려고 CMake에서 "Visual Studio 14 2015 Win64"를 선택했는데, 이상한 에러가 발생하네요.



The CXX compiler identification is unknown
The C compiler identification is unknown


일단 눈으로 보이는 에러 메시지는 CXX, C 컴파일러를 찾지 못한다는 것인데요, 자세한 내용을 확인하기 위해 C:/opencv/mybuild/CMakeFiles/CMakeOutput.log 파일을 열어보았습니다.


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: 
Build flags:
Id flags: 

The output was:
1
Microsoft (R) Build Engine 버전 14.0.25420.1]
Copyright (C) Microsoft Corporation. All rights reserved.

빌드 시작: 2017-08-03 오전 10:47:35
1 노드의 "C:\opencv\mybuild\CMakeFiles\3.9.0\CompilerIdCXX\CompilerIdCXX.vcxproj" 프로젝트(기본 대상)입니다.
PrepareForBuild:
  "Debug\" 디렉터리를 만들고 있습니다.
  "Debug\CompilerIdCXX.tlog\" 디렉터리를 만들고 있습니다.
InitializeBuildStatus:
  "AlwaysCreate"이(가) 지정되었기 때문에 "Debug\CompilerIdCXX.tlog\unsuccessfulbuild"을(를) 만들고 있습니다.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\CL.exe /c /nologo /W0 /WX- /Od /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc140.pdb" /Gd /TP /errorReport:queue CMakeCXXCompilerId.cpp
  CMakeCXXCompilerId.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\link.exe /ERRORREPORT:QUEUE /OUT:".\CompilerIdCXX.exe" /INCREMENTAL:NO /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:".\CompilerIdCXX.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:".\CompilerIdCXX.lib" /MACHINE:X64 Debug\CMakeCXXCompilerId.obj
LINK : fatal error LNK1158: 'rc.exe'을(를) 실행할 수 없습니다. [C:\opencv\mybuild\CMakeFiles\3.9.0\CompilerIdCXX\CompilerIdCXX.vcxproj]
"C:\opencv\mybuild\CMakeFiles\3.9.0\CompilerIdCXX\CompilerIdCXX.vcxproj" 프로젝트를 빌드했습니다(기본 대상). - 실패

빌드하지 못했습니다.

"C:\opencv\mybuild\CMakeFiles\3.9.0\CompilerIdCXX\CompilerIdCXX.vcxproj"(기본 대상)(1)->
(Link 대상) ->
  LINK : fatal error LNK1158: 'rc.exe'을(를) 실행할 수 없습니다. [C:\opencv\mybuild\CMakeFiles\3.9.0\CompilerIdCXX\CompilerIdCXX.vcxproj]

    경고 0개
    오류 1개

경과 시간: 00:00:00.81

...


중간에 보면 rc.exe 를 실행할 수 없다는 메시지가 나타납니다. rc.exe는 "Microsoft (R) Windows (R) Resource Compiler"를 의미하는 것인데, 이 파일을 찾을 수 없다는 의미인거네요. 시작 메뉴에서 "Visual Studio 2015 Tools 명령 프롬프트 열기" 메뉴를 찾아서 실행하고, 나타난 도스창에서 rc.exe를 입력하면 실제로도 해당 파일을 찾을 수 없다고 나타납니다.


이와 반대로 "Open Visual Studio 2017 Tools Command Prompt" 커맨드 창에서는 rc.exe /? 명령을 실행하면 아래와 같이 프로그램이 잘 실행됩니다.


결국 원인을 알 수 없는 이유로 인하여 Visual Studio 2015 환경에서는 rc.exe 파일을 찾지 못한다는 것이고, 이는 기본적으로 Visual Studio 2017 커맨드창 환경과 Visual Studio 2015 커맨드창의 PATH 설정이 다르기 때문일 것입니다. 그래서 두 창에서 각각 PATH 명령을 입력해서 그 차이를 비교해보고, 실제 rc.exe 파일이 있는 위치를 검토해보니 아래 디렉토리가 PATH에 있느냐 없느냐의 차이인 것으로 보였습니다.


C:\Program Files (x86)\Windows Kits\10\bin\10.0.15063.0\x86


바로 이 폴더에 Windows SDK 버전 10의 rc.exe 파일이 있네요. 그러므로 Visual Studio 2015 버전에서도 이 디렉토리를 PATH 환경 변수에 추가해주면 되는데요, 음.. 반대로 위 폴더에 있는 rc.exe 파일을 Visual Studio 2015 커맨드창 PATH 경로에 복사해주어도 정상적으로 동작을 합니다. 그래서 해결책은 이렇습니다.


1. C:\Program Files (x86)\Windows Kits\10\bin\10.0.15063.0\x86 폴더에서

2. rc.exe 파일과 rcdll.dll 파일을 복사해서

3. C:\Program Files (x86)\Windows Kits\10\bin\x86\ 폴더에 붙여넣습니다.


그랬더니 CMake가 잘 동작을 하네요~ :-)



Posted by kkokkal
: