๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SW/์˜์ƒ์ฒ˜๋ฆฌ

OpenCV) Ubuntu 20.04์— OpenCV 4.4.0 ์„ค์น˜

์ž‘์„ฑ์ผ์ž - 23.05.22

 

 

https://webnautes.tistory.com/1433

์•„๋ž˜์˜ process๋Š” ์œ„์˜ ํ‹ฐ์Šคํ† ๋ฆฌ๋ธ”๋กœ๊ทธ์—์„œ ์ง„ํ–‰ํ•ด๋ณด๊ณ  ํ•„์ž๊ฐ€ ์ž์ฃผ ํ•„์š”ํ•˜์—ฌ ๊ฐ€์ ธ์™€ ์š”์•ฝํ•˜์˜€๋‹ค.

 

 

์‚ฌ์‹ค jupyter notebook๊ณผ ๊ฐ™์€ ํ™˜๊ฒฝ์—์„œ๋Š”

install opencv-python

install opencv-contrib-python ๋งŒ ํ•ด์ค˜๋„ ๋œ๋‹ค..

 

์ธ๋„ค์ผ์šฉ

 

๊ธฐ์กด OpenCV ํ™•์ธ

pkg-config --modversion opencv

์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, opencv์˜ ๋ฒ„์ „์ด ์ถœ๋ ฅ๋œ๋‹ค๋ฉด, opencv๊ฐ€ ๊ธฐ์„ค์น˜ ๋˜์–ด์žˆ๋Š” ๊ฒƒ์ด๊ณ , ์•„๋ž˜์˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋œจ๋ฉด opencv๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ์ง€ ์•Š์€ ๊ฒƒ์ด๋‹ค.

opencv ๋ฏธ์„ค์น˜์‹œ ์‘๋‹ต

 

opencv ์‚ญ์ œ

sudo apt-get purge libopencv* python-opencv
sudo apt-get autoremove

libopencv*์„ ์ž…๋ ฅํ•˜์—ฌ ๋ชจ๋“  ๋ฒ„์ „์˜  opencv ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ purge๋ฅผ ํ†ตํ•ด ์‚ญ์ œํ•œ๋‹ค.

 

 

์„ค์น˜์ค€๋น„

ํŒจํ‚ค์ง€ ์—…๊ทธ๋ ˆ์ด๋“œ

sudo apt update
sudo apt upgrade

๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ์ ์ธ ํŒจํ‚ค์ง€๋ฅผ ์—…๊ทธ๋ ˆ์ด๋“œํ•œ๋‹ค.

 

sudo apt-get install build-essential cmake
sudo apt-get install pkg-config
sudo apt-get install libjpeg-dev libtiff5-dev libpng-dev
sudo apt-get install ffmpeg libavcodec-dev libavformat-dev libswscale-dev libxvidcore-dev libx264-dev libxine2-dev
sudo apt-get install libv4l-dev v4l-utils
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev 
sudo apt-get install libgtk-3-dev

sudo apt-get install mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev  
sudo apt-get install libatlas-base-dev gfortran libeigen3-dev
sudo apt-get install python3-dev python3-numpy

 

 

 

์ด ๋งŽ์€ ํŒจํ‚ค์ง€๋“ค์„ ํ•œ๋ฒˆ์— ๋‹ค์šด๋ฐ›๊ธฐ ์œ„ํ•ด ์•„๋ž˜์ฒ˜๋Ÿผ ์ค„์—ฌ๋ณด์•˜๋‹ค.

sudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libpng-dev ffmpeg libavcodec-dev libavformat-dev libswscale-dev libxvidcore-dev libx264-dev libxine2-dev libv4l-dev v4l-utils libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgtk-3-dev mesa-utils libgl1-mesa-dri libgtkgl2.0-dev libgtkglext1-dev libatlas-base-dev gfortran libeigen3-dev python3-dev python3-numpy -y

์ญ‰ ๊ธ์–ด๋‹ค๊ฐ€ ์ž…๋ ฅํ•˜๋ฉด ๋ชจ๋‘ ํ•œ๋ฐฉ์— ๊น”๋ฆฐ๋‹ค. ์ด๋˜ํ•œ ํ•„์ž๊ฐ€ ์ž์ฃผ ํ•„์š”ํ•ด์„œ ์ค„์—ฌ๋ณด์•˜๋‹ค.

 

 

OpenCV ์„ค์น˜

๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“  ํ›„ ์•„๋ž˜์˜ process๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

wget -O opencv.zip https://github.com/opencv/opencv/archive/4.4.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.4.0.zip
unzip opencv.zip
unzip opencv_contrib.zip

๊ทธ๋ƒฅ opencv์™€ contrib_opencv ๋‘˜๋‹ค ์„ค์น˜ํ•ด์ค€๋‹ค.

ํ•„์ž๋Š” arucomarker๋“ฑ ํ™•์žฅ opencv๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

์ด์ œ,  opencv-4.4.0์— build๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด cmakeํ•œ๋‹ค. 

 

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=OFF -D WITH_IPP=OFF -D WITH_1394=OFF -D BUILD_WITH_DEBUG_INFO=OFF -D BUILD_DOCS=OFF -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=OFF -D BUILD_PACKAGE=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D WITH_QT=OFF -D WITH_GTK=ON -D WITH_OPENGL=ON -D BUILD_opencv_python3=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.4.0/modules -D WITH_V4L=ON  -D WITH_FFMPEG=ON -D WITH_XINE=ON -D OPENCV_ENABLE_NONFREE=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D OPENCV_SKIP_PYTHON_LOADER=ON -D OPENCV_GENERATE_PKGCONFIG=ON ../
-- General configuration for OpenCV 4.4.0 =====================================
--   Version control:               unknown
-- 
--   Extra modules:
--     Location (extra):            /home/skh/opencv/opencv_contrib-4.4.0/modules
--     Version control (extra):     unknown
-- 
--   Platform:
--     Timestamp:                   2023-05-22T14:43:49Z
--     Host:                        Linux 5.15.0-72-generic x86_64
--     CMake:                       3.16.3
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (15 files):         + SSSE3 SSE4_1
--       SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (4 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.4.0)
--     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 -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -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 -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -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-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -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-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections -Wl,--as-needed  
--     Linker flags (Debug):        -Wl,--gc-sections -Wl,--as-needed  
--     ccache:                      NO
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf java js julia matlab ovis python2 sfm ts viz
--     Applications:                apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       GtkGlExt:                  NO
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build Jasper (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     FFMPEG:                      YES
--       avcodec:                   YES (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.100)
--       avresample:                NO
--     GStreamer:                   YES (1.16.3)
--     v4l/v4l2:                    YES (linux/videodev2.h)
--     Xine:                        YES (ver 1.2.9)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/skh/opencv/opencv-4.4.0/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.10)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
--     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.17.4)
--     install path:                lib/python3.8/dist-packages
-- 
--   Python (for build):            /usr/bin/python3
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/skh/opencv/opencv-4.4.0/build

์™€ ๊ฐ™์ด opencv ๊ด€๋ จ ์ปดํŒŒ์ผ ์„ค์ • ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

์•„๋ž˜ ์„ธ์ค„

-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/skh/opencv/opencv-4.4.0/build

 

๋ถ€๋ถ„์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค๋ฉด, ์ •์ƒ์ ์œผ๋กœ ๋œ ๊ฒƒ์ด๋‹ค.

 

์ด ๋•Œ, python3 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•ญ๋ชฉ์ด ๋ณด์ด์ง€ ์•Š๋Š”๊ฒฝ์šฐ๋Š”, cmakeํ•  ๋•Œ, python3๊ด€๋ จ ๊ฒฝ๋กœ๋“ค์„ ์ง์ ‘ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

python3 ์ดํ›„ ๋ถ€ํ„ฐ์˜ ๊ฒฝ๋กœ๊ฐ€ python3 ์™€ ๊ด€๋ จ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค.

 

 

time make -j$(nproc)

์ด์ œ, make๋ช…๋ น์„ ํ†ตํ•ด ์ปดํŒŒ์ผ์„ ์‹œ์ž‘ํ•œ๋‹ค. ์•ž์— time์„ ๋ถ™์—ฌ ์ปดํŒŒ์ผ ํ›„ ์™„๋ฃŒ ์‹œ๊ฐ„์ด ๋œจ๋„๋ก ํ•œ๋‹ค.

๋๋‹ค.

 

sudo make install

์ด์ œ, ๊ฒฐ๊ณผ๋ฌผ์„ ์„ค์น˜ํ•œ๋‹ค.