ビルドしたOpenCVを動かしてみた

ビルドしたOpenCVOpenCLが使えるようになったはずなので、実際に試してみた。

ビルド構成

import cv2
print(cv2.getBuildInformation())

上のコードで出力したビルド構成がこちら。Qtを使っているとモジュールのimportに失敗するのでQtを外している。

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

Extra modules:
Location (extra): G:/lib/opencv-3.4.0/dev/opencv_contrib/modules
Version control (extra): unknown

Platform:
Timestamp: 2018-01-31T14:34:38Z
Host: Windows 6.1.7601 AMD64
CMake: 3.10.2
CMake generator: MSYS Makefiles
CMake build tool: D:/soft/pleiades/eclipse/msys/bin/make.exe
Configuration: Release

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 (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

C/C++:
Built as dynamic libs?: YES
C++11: YES
C++ Compiler: D:/soft/pleiades/eclipse/mingw/bin/g++.exe (ver 7.1.0)
C++ flags (Release): -fsigned-char -mstackrealign -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -mstackrealign -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: D:/soft/pleiades/eclipse/mingw/bin/gcc.exe
C flags (Release): -fsigned-char -mstackrealign -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 -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -mstackrealign -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 -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--gc-sections
Linker flags (Debug): -Wl,--gc-sections
ccache: NO
Precompiled headers: NO
Extra dependencies: opengl32 glu32
3rdparty dependencies:

OpenCV modules:
To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dpm face features2d flann fuzzy highgui img_hash imgcodecs imgproc 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 dnn_modern freetype hdf java matlab ovis sfm
Applications: tests perf_tests examples apps
Documentation: NO
Non-free algorithms: NO

Windows RT support: NO

GUI:
Win32 UI: YES
OpenGL support: YES (opengl32 glu32)
VTK support: YES (ver 8.1.0)

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)
GStreamer: NO
DirectShow: YES

Parallel framework: none

Trace: YES (built-in)

Other third-party libraries:
Lapack: NO
Eigen: YES (ver 3.3.3)
Custom HAL: NO

OpenCL: YES (no extra features)
Include path: G:/lib/opencv-3.4.0/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 2:
Interpreter: D:/soft/Anaconda3/envs/py2714/python.exe (ver 2.7.14)
Libraries: D:/soft/Anaconda3/envs/py2714/python27.dll (ver 2.7.14)
numpy: D:/soft/Anaconda3/pkgs/numpy-1.13.3-py27h9ac254c_0/Lib/site-packages/numpy/core/include (ver 1.13.3)
packages path: D:/soft/Anaconda3/envs/py2714/DLLs

Python 3:
Interpreter: D:/soft/Anaconda3/python.exe (ver 3.6.3)
Libraries: D:/soft/Anaconda3/python36.dll (ver 3.6.3)
numpy: D:/soft/Anaconda3/pkgs/numpy-1.13.3-py36ha320f96_0/Lib/site-packages/numpy/core/include (ver 1.13.3)
packages path: D:/soft/Anaconda3/DLLs

Python (for build): D:/soft/Anaconda3/envs/py2714/python.exe

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

Matlab: NO

Install to: D:/soft/cv4py/

                                                                                                                                • -
Pythonから呼んでみた

以下のパスを通してからPythonを実行する。

OpenCVはインストール先直下のbinフォルダではないので注意。

OpenCLを動かすにはUMatというMat互換のクラスを使うだけで大丈夫っぽい。
こちら*1を参考にして呼んでみる。

import cv2

img = cv2.imread('lena.png', cv2.IMREAD_COLOR)
uimg = cv2.UMat(img)
tmp = cv2.cvtColor(uimg, cv2.COLOR_BGR2RGB)

for i in range(10000):
    tmp = cv2.cvtColor(uimg, cv2.COLOR_BGR2RGB)

cv2.imshow('Test', tmp)
cv2.waitKey()

れなぴん(lena.png)のRGBを無駄に1万回ひっくり返す処理を呼んでいる。

こんな感じで色がひっくり返って表示される。
なんか元画像より少し大きいような??ま、いいか。

実行時に以下の出力が出るのでOpenCLが動いていると思われる。

[ INFO:0] Initialize OpenCL runtime...
[ INFO:0] Successfully initialized OpenCL cache directory: C:\Users\maminus\AppData\Local\Temp\opencv\3.4.0\opencl_cache\
[ INFO:0] Preparing OpenCL cache configuration for context: 32-bit--Advanced_Micro_Devices__Inc_--Pitcairn--2527_8

GPU-Zで見ると動かしたときだけGPUの使用率が上がっている。たぶん動いている。

C++

次はC++から呼んでみる。
パスは以下を通す。

Qtは自分でビルドしたので、git cloneしたトップフォルダqt5からqtbase\binと降りたフォルダにバイナリが入っている。デベロッパービルドを実施するとqtbase\binにビルドされるらしい。
あれ?よく考えたらVTKにパス通してなくても動いている??
vizモジュールを使わない限り不要なのかな。

ビルド構成の差分は

  • cvvモジュールがPython版はUnavailable。C++版はTo be built
  • GUIPython版はQt非対応。C++版は対応

インストール先はPython向けとC++向けで別々に入れている。ビルドも2回やった。

Pythonと同様に無駄に1万回色を入れ替えてから表示する。

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

int main(void)
{
    std::cout << cv::getBuildInformation() << std::endl;

    cv::Mat img = cv::imread("lena.png", cv::IMREAD_COLOR);
    cv::UMat umat = img.getUMat(cv::ACCESS_READ);
    cv::UMat tmp;

    for (int i=0;i<10000;i++) {
        cv::cvtColor(umat, tmp, cv::COLOR_BGR2RGB);
    }

    cv::imshow("Blue", tmp);
    cv::waitKey();

    return 0;
}

当たり前だけど、Pythonと同じように動いた。

色々あったけど、なんとか動く環境になってよかった。
ただ、ビルド構成で「Parallel framework」が「none」になってるから、もうちょっと頑張った方が良いかもしれない。