라즈베리파이에서 OpenCV 3.2를 설치하는 방법을 정리해보겠습니다. 라즈베리파이에 OpenCV를 설치한다는 것은 OpenCV 소스 코드를 직접 빌드하여 헤더 파일을 복사하고 OpenCV 라이브러리를 생성하는 작업을 의미합니다. 과정이 좀 길지만 차근차근 알아보겠습니다.
OpenCV 소스 코드를 빌드하려면 빌드 관련 도구나 필요한 라이브러리 패키지를 라즈베리파이에 미리 설치해두어야 합니다. 예를 들어, 정지 영상 포맷과 관련된 jpeg 라이브러리나 png 라이브러리, 비디오 파일 포맷 또는 스트리밍 관련 라이브러리 같은 것들을 미리 설치해야 정상적으로 빌드됩니다.
[빌드 관련 도구 설치] 실제 빌드 작업에 앞서 OpenCV 빌드에 필요한 패키지를 설치합니다.
[파이썬] OpenCV를 빌드한 후 C/C++ 뿐만 아니라 Python으로도 프로그래밍을 테스트하려고 합니다. 개인적으로는 Python 2.x 버전을 사용하지 않고 Python 3.x 버전만 사용할 것이구요.. 이를 위해서 python3-dev 또는 python3-numpy 패키지가 필요한데, 얘네 둘은 이미 설치가 되어 있을 겁니다. (없으면 설치해야죠.)
pi@RaspberryPi3:~/opencv $ mkdir build && cd build
[cmake 구성] 아래 명령어가 좀 긴데요, 대충 보면 예제 파일들을 생성하고, Python 2.x 말고 Python 3.x를 지원하도록 하는 등의 옵션을 지정합니다. Extra 모듈 소스 코드 위치를 지정하고, OpenCV 소스 코드 위치가 ../opencv-3.2.0/ 임을 명시합니다. 그리고 cmake가 동작하면서 나타나는 출력 메시지를 모두 cmake_messages.txt 파일에 기록합니다.
참고로 cmake_message.txt 파일에 기록된 내용을 아래 [더보기]에 정리해두었습니다.
-- General configuration for OpenCV 3.2.0 ===================================== -- Version control: unknown -- -- Extra modules: -- Location (extra): /home/pi/opencv/opencv_contrib-3.2.0/modules -- Version control (extra): unknown -- -- Platform: -- Timestamp: 2017-04-10T20:06:49Z -- Host: Linux 4.9.30-v7+ armv7l -- CMake: 3.6.2 -- CMake generator: Unix Makefiles -- CMake build tool: /usr/bin/make -- Configuration: RELEASE -- -- C/C++: -- Built as dynamic libs?: YES -- C++ Compiler: /usr/bin/c++ (ver 4.9.2) -- C++ flags (Release): -fsigned-char -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -mfp16-format=ieee -mfpu=neon -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -mfpu=neon-fp16 -O3 -DNDEBUG -DNDEBUG -- C++ flags (Debug): -fsigned-char -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -mfp16-format=ieee -mfpu=neon -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -mfpu=neon-fp16 -g -O0 -DDEBUG -D_DEBUG -- C Compiler: /usr/bin/cc -- C flags (Release): -fsigned-char -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 -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -mfp16-format=ieee -mfpu=neon -ffunction-sections -fvisibility=hidden -mfpu=neon-fp16 -O3 -DNDEBUG -DNDEBUG -- C flags (Debug): -fsigned-char -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 -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -mfp16-format=ieee -mfpu=neon -ffunction-sections -fvisibility=hidden -mfpu=neon-fp16 -g -O0 -DDEBUG -D_DEBUG -- Linker flags (Release): -- Linker flags (Debug): -- ccache: NO -- Precompiled headers: YES -- Extra dependencies: /usr/lib/arm-linux-gnueabihf/libpng.so /usr/lib/arm-linux-gnueabihf/libz.so /usr/lib/arm-linux-gnueabihf/libtiff.so /usr/lib/arm-linux-gnueabihf/libjasper.so /usr/lib/arm-linux-gnueabihf/libjpeg.so gtk-x11-2.0 gdk-x11-2.0 pangocairo-1.0 atk-1.0 cairo gdk_pixbuf-2.0 gio-2.0 pangoft2-1.0 pango-1.0 gobject-2.0 glib-2.0 fontconfig freetype gthread-2.0 gstbase-1.0 gstreamer-1.0 gstvideo-1.0 gstapp-1.0 gstriff-1.0 gstpbutils-1.0 v4l1 v4l2 avcodec avformat avutil swscale avresample dl m pthread rt -- 3rdparty dependencies: libwebp IlmImf libprotobuf tegra_hal -- -- OpenCV modules: -- To be built: core flann imgproc ml photo reg surface_matching video dnn freetype fuzzy imgcodecs shape videoio highgui objdetect plot superres xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo tracking videostab xfeatures2d ximgproc aruco optflow phase_unwrapping stitching structured_light python3 -- Disabled: world contrib_world -- Disabled by dependency: - -- Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 ts viz cnn_3dobj cvv hdf matlab sfm -- -- GUI: -- QT: NO -- GTK+ 2.x: YES (ver 2.24.25) -- GThread : YES (ver 2.42.1) -- GtkGlExt: NO -- OpenGL support: NO -- VTK support: NO -- -- Media I/O: -- ZLib: /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.8) -- JPEG: /usr/lib/arm-linux-gnueabihf/libjpeg.so (ver ) -- WEBP: build (ver 0.3.1) -- PNG: /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.2.50) -- TIFF: /usr/lib/arm-linux-gnueabihf/libtiff.so (ver 42 - 4.0.3) -- JPEG 2000: /usr/lib/arm-linux-gnueabihf/libjasper.so (ver 1.900.1) -- OpenEXR: build (ver 1.7.1) -- GDAL: NO -- GDCM: NO -- -- Video I/O: -- DC1394 1.x: NO -- DC1394 2.x: NO -- FFMPEG: YES -- avcodec: YES (ver 56.1.0) -- avformat: YES (ver 56.1.0) -- avutil: YES (ver 54.3.0) -- swscale: YES (ver 3.0.0) -- avresample: YES (ver 2.1.0) -- GStreamer: -- base: YES (ver 1.4.4) -- video: YES (ver 1.4.4) -- app: YES (ver 1.4.4) -- riff: YES (ver 1.4.4) -- pbutils: YES (ver 1.4.4) -- OpenNI: NO -- OpenNI PrimeSensor Modules: NO -- OpenNI2: NO -- PvAPI: NO -- GigEVisionSDK: NO -- Aravis SDK: NO -- UniCap: NO -- UniCap ucil: NO -- V4L/V4L2: Using libv4l1 (ver 1.6.0) / libv4l2 (ver 1.6.0) -- XIMEA: NO -- Xine: NO -- gPhoto2: NO -- -- Parallel framework: pthreads -- -- Other third-party libraries: -- Use IPP: NO -- Use VA: NO -- Use Intel VA-API/OpenCL: NO -- Use Lapack: NO -- Use Eigen: YES (ver 3.2.2) -- Use Cuda: NO -- Use OpenCL: YES -- Use OpenVX: NO -- Use custom HAL: YES (carotene (ver 0.0.1)) -- -- OpenCL: <Dynamic loading of OpenCL library> -- Include path: /home/pi/opencv/opencv-3.2.0/3rdparty/include/opencl/1.2 -- Use AMDFFT: NO -- Use AMDBLAS: NO -- -- Python 2: -- Interpreter: /usr/bin/python2.7 (ver 2.7.9) -- -- Python 3: -- Interpreter: /usr/bin/python3.4 (ver 3.4.2) -- Libraries: /usr/lib/arm-linux-gnueabihf/libpython3.4m.so (ver 3.4.2) -- numpy: /usr/lib/python3/dist-packages/numpy/core/include (ver 1.8.2) -- packages path: lib/python3.4/dist-packages -- -- Python (for build): /usr/bin/python2.7 -- -- Java: -- ant: NO -- JNI: NO -- Java wrappers: NO -- Java tests: NO -- -- Matlab: Matlab not found or implicitly disabled -- -- Tests and samples: -- Tests: NO -- Performance tests: NO -- C/C++ Examples: YES -- -- Install path: /usr/local -- -- cvconfig.h is in: /home/pi/opencv/build -- ----------------------------------------------------------------- -- -- Configuring done -- Generating done -- Build files have been written to: /home/pi/opencv/build
[빌드!!!] 이제 실제 빌드 작업을 진행합니다. 라즈베리파이3은 4개의 코어가 있지만, -j4 옵션을 주면 발열때문에 문제가 생길까봐 -j2 옵션으로 빌드를 진행합니다. 혹시라도 에러가 발생할까봐 컴파일 메시지는 build_messages.txt 파일에도 저장해둡니다. 빌드 작업은 2시간 넘게 걸렸던 것같고, 간간히 선풍기로 라즈베리파이 보드를 식히면서 진행했습니다.
pi@RaspberryPi3:~/opencv/build/ $ make -j2 2>&1 | tee build_messages.txt
[설치 및 설치 확인] 아래 명령어를 입력한 후 3.2.0 이 출력되면 정상적으로 설치가 된겁니다.
[Python 테스트] python3을 실행하여 OpenCV를 잘 불러오는지 확인해봅니다.
pi@RaspberryPi3:~/opencv/build/ $ python3 Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> print(cv2.__version__) 3.2.0 >>> exit() pi@RaspberryPi3:~/opencv/build/ $