Ubuntu 16.04 ์—์„œ OpenCV 3.x ์„ค์น˜

๋ฐ˜์‘ํ˜•

OpenCV ์„ค์น˜์— ๋“ค์–ด๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 


OpenCV๋Š” OS X, Linux, Windows, Android, iOS ์–ด๋””์„œ๋“ ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ์„ค์น˜ํ•˜๊ธฐ๊ฐ€ ์ข€ ๊นŒ๋‹ค๋กœ์šด Linux์—์„œ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์ŠคํŒ…ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.



์„ค์น˜๋ฅผ ์œ„ํ•œ ์ค€๋น„..

Linux์—์„œ OpenCV๋ฅผ ์„ค์น˜ํ•  ๋•Œ๋Š” ์ผ๋ฐ˜ ํ”„๋กœ๊ทธ๋žจ ์„ค์น˜์™€ ๋‹ฌ๋ฆฌ ๋ฏธ๋ฆฌ ์ค€๋น„๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ๋กœ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ๋ฐ›์•„์„œ ์ปดํŒŒ์ผํ•˜์—ฌ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„ค์น˜๋ฅผ ์œ„ํ•ด OpenCV ์†Œ์Šค ์ฝ”๋“œ์™€ ์ปดํŒŒ์ผ ๋„๊ตฌ๋ฅผ ์ค€๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


- OpenCV Source Code

- build-essential, CMake, GCC

- NVIDIA CUDA (Optional)


ํ˜น์‹œ ์ž์‹ ์˜ ๋…ธํŠธ๋ถ์ด๋‚˜ PC์— NVIDIA ๊ทธ๋ž˜ํ”ฝ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ๋“œ๋ผ์ด๋ฒ„์™€ ๋™์‹œ์— CUDA๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ GPU๋ฅผ ์ด์šฉํ•œ OpenCV ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ด ํฌ์ŠคํŠธ์—์„œ๋Š” CUDA ์„ค์น˜์™€ ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜๊นŒ์ง€ ๊ฐ™์ด ํฌ์ŠคํŒ… ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.


Linux์—์„œ ์‚ฌ์šฉํ•˜๋Š” C ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋Œ€๋ถ€๋ถ„ GCC๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์—ญ์‹œ ์ด ํฌ์ŠคํŠธ์—์„œ๋„ GCC๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋นŒ๋“œํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ OpenCV๋Š” gcc 4.9 ๋ฒ„์ „ ์ด์ƒ์—์„œ ์ปดํŒŒ์ผ์ด ์•ˆ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ํฌ์ŠคํŠธ์—์„œ๋Š” gcc 4.9 ๋ฒ„์ „์— ๋งž์ถฐ ์ปดํŒŒ์ผ์„ ์ง„ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.


๊ทธ๋Ÿผ ์ด์ œ ํ•„์š”ํ•œ ์ค€๋น„๋Š” ๋ชจ๋‘ ๋๋‚ฌ์œผ๋‹ˆ, ์„ค์น˜์— ์ง์ ‘ ๋“ค์–ด๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.



๋ณธ๊ฒฉ์ ์ธ OpenCV ์„ค์น˜

์ž ๋จผ์ € OpenCV ์†Œ์Šค ์ฝ”๋“œ๋ฅผ Github์—์„œ ๋ฐ›์•„ ๋จผ์ € ๋ฏธ๋ฆฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ๋†“๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


$ mkdir opencv

๋จผ์ € OpenCV ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ฐ›์•„๋†“์„ ์ ๋‹นํ•œ ์ด๋ฆ„์˜ ํด๋”๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

$ cd opencv

ํ•ด๋‹น ํด๋”์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.


$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib

git ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด OpenCV ์†Œ์Šค ์ฝ”๋“œ์™€ ํ™•์žฅํŒฉ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์Šต๋‹ˆ๋‹ค.


$ sudo apt install git

๋งŒ์•ฝ git์ด ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด, ์œ„ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด git์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์œผ๋กœ OpenCV ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ๋ชจ๋‘ ๋‹ค์šด๋กœ๋“œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.



์ด์ œ ์ปดํŒŒ์ผ ๋„๊ตฌ์™€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.



$ sudo apt update
$ sudo apt install build-essential checkinstall gcc-4.9 cmake pkg-config yasm libtiff5-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev  libv4l-dev python-dev python-numpy libtbb-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils libopenexr-dev python-tk  libeigen3-dev libx264-dev

๋‘ ๋ช…๋ น์–ด๋ฅผ ๋ชจ๋‘ ํƒ€์ดํ•‘ํ•ฉ๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ ๋„๊ตฌ์—๋Š” build-essential, pkg-config๊ฐ€ ํ•ด๋‹นํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ชจ๋‘ OpenCV ์˜์ƒ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์ž…๋‹ˆ๋‹ค. ์•„๋งˆ ๋Œ€๋ถ€๋ถ„ ์ต์ˆ™ํ•œ ๋‹จ์–ด๋“ค์ด๋‚˜ ํŒจํ‚ค์ง€์ผ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.


$ sudo add-apt-repository ppa:mc3man/gstffmpeg-keep
$ sudo apt update
$ sudo apt install ffmpeg gstreamer0.10-ffmpeg

์ถ”๊ฐ€๋กœ ์„ค์น˜ํ•ด์•ผ ํ•  mpeg ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
(๋ฐ˜๋“œ์‹œ ์„ค์น˜ํ•ด์•ผํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ OpenCV Full Package ์„ค์น˜๋ฅผ ์œ„ํ•ด์„œ๋Š” ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.)

์—ฌ๊ธฐ์„œ ์ž ๊น, OpenCV์— ๋Œ€ํ•œ ์„ค์น˜๋ฅผ ์ผ์‹œ ์ค‘๋‹จํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.



NVIDIA Driver, CUDA ์„ค์น˜ (์˜ต์…˜, 64-bit only)

์ปดํŒŒ์ผ ํ•˜๊ธฐ ์ „์—, CUDA ์—ฐ๋™์„ ์›ํ•˜์‹œ๋Š” ๋ถ„๋“ค์€ OpenCV ์„ค์น˜ ์ดํ›„, ๋ฐ˜๋“œ์‹œ ์ด ๊ณผ์ •์„ ๊ฑฐ์น˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ์‹  ๋ถ„๋“ค์€ ๋ฐ”๋กœ, ๋ฐ‘์œผ๋กœ ์ด๋™ํ•˜์…”์„œ OpenCV ์„ค์น˜๋ฅผ ๊ณ„์† ์ง„ํ–‰ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. (CUDA์— ๋Œ€ํ•œ ๋ณ„๋„์˜ ์„ค๋ช…์€ ํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ณธ์ธ์ด ์ •๋ง๋กœ ์•Œ๊ณ  ์“ฐ์‹œ๋Š” ๋ถ„๋งŒ ์ฝ๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.)


๋จผ์ € NVIDIA ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์น˜ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


Linux์—์„œ ์„ค์น˜ํ•˜๋Š” NVIDIA ๋“œ๋ผ์ด๋ฒ„๋Š” ๋‘ ๊ฐ€์ง€์˜ ์ข…๋ฅ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜๋Š” Linux Repository์—์„œ ์ œ๊ณตํ•˜๋Š” NVIDIA ๋“œ๋ผ์ด๋ฒ„, ๋˜ ํ•˜๋‚˜๋Š” NVIDIA ํ™ˆํŽ˜์ด์ง€์—์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ์‚ฌ์šฉํ•˜๋Š” ๋“œ๋ผ์ด๋ฒ„๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.


๋‘˜ ๋‹ค ์„ค์น˜ํ•˜๋Š” ๋ฐ๋Š” ํฐ ๋ฌธ์ œ๊ฐ€ ์—†์ง€๋งŒ, ์‰ฝ๊ฒŒ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” Repository๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœ๋“œ๋ฆฌ๊ณ , ์ž์‹ ์ด ๊ผญ ์ตœ์‹  ๋ฒ„์ „์˜ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์จ์•ผํ•œ๋‹ค๋ฉด, ํ™ˆํŽ˜์ด์ง€์—์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.


์ด ํฌ์ŠคํŠธ์—์„œ๋Š” Repository์—์„œ ์ œ๊ณตํ•˜๋Š” ๋“œ๋ผ์ด๋ฒ„์˜ ์„ค์น˜๋กœ ์“ฐ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


ํ„ฐ๋ฏธ๋„์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.


$ sudo apt install nvidia-367 nvidia-367-dev nvidia-prime

์ด ๋ช…๋ น์–ด๋ฅผ ์น˜๊ธฐ ์ „์—, ๋ช‡ ๊ฐ€์ง€ ์•Œ๋ ค๋“œ๋ฆฌ์ž๋ฉด, nvidia-367์—์„œ 367์€ ๋“œ๋ผ์ด๋ฒ„ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ์›ํ•˜์‹œ๋Š” ๋“œ๋ผ์ด๋ฒ„ ๋ฒ„์ „์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์…”์„œ ์„ค์น˜ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. dev๋Š” ๊ฐœ๋ฐœ์šฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜†์— ์žˆ๋Š” nvidia-prime์€ NVIDIA Optimus ๋“œ๋ผ์ด๋ฒ„ ์ „์šฉ์ด๋ฏ€๋กœ ๋…ธํŠธ๋ถ์—์„œ ์ž์‹ ์ด Optimus๋ฅผ ์ง€์›ํ•˜๋Š” ๋…ธํŠธ๋ถ์„ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๊ฒฝ์šฐ์—๋งŒ ์„ค์น˜๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. 


Non-Optimus, PC NVIDIA Graphic Card์—์„œ๋Š” prime์„ ์„ค์น˜ํ•˜์‹œ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.


์„ค์น˜๊ฐ€ ๋๋‚ฌ์œผ๋ฉด Linux๋ฅผ ์žฌ๋ถ€ํŒ…ํ•ด์ค๋‹ˆ๋‹ค.



๋‹ค์Œ์€ CUDA๋ฅผ ์„ค์น˜ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


CUDA ์—ญ์‹œ Repository์—์„œ ์ œ๊ณตํ•˜๋Š” CUDA, NVIDIA ํ™ˆํŽ˜์ด์ง€์—์„œ ์ œ๊ณตํ•˜๋Š” CUDA๊ฐ€ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ, Repository์—์„œ ์ œ๊ณตํ•˜๋Š” CUDA๋Š” ์„ค์น˜ํ•˜์ง€ ๋งˆ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ํ•ด๋‹น CUDA๋Š” CUDA์˜ ์ผ๋ถ€๋งŒ ์ ์žฌ๋˜์–ด ์žˆ์–ด ์‹ค์ œ ์ฝ”๋”ฉ์— ๋Œ€ํ•œ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋งŽ์ด ๋น ์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. (cuDNN์ด๋‚˜ NVCC ๋“ฑ) CUDA ์„ค์น˜๋Š” ๋ฐ˜๋“œ์‹œ NVIDIA ํ™ˆํŽ˜์ด์ง€์—์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ์„ค์น˜ํ•˜๊ธฐ๋ฅผ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.


๋˜, CUDA ๋ฒ„์ „์€ ์ž์‹ ์ด ํ˜„์žฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” NVIDIA ๊ทธ๋ž˜ํ”ฝ ์นด๋“œ์— ๋งž์ถฐ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„์ „ ํ˜ธํ™˜์— ๋Œ€ํ•œ ์ •๋ณด๋Š” NVIDIA ํ™ˆํŽ˜์ด์ง€์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ €๋Š” CUDA 8.0 ๋ฒ„์ „์„ ์„ค์น˜ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


Download NVIDIA CUDA


์œ„ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ, NVIDIA CUDA๋ฅผ ๋‹ค์šด๋กœ๋“œ ํŽ˜์ด์ง€๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. ํŽ˜์ด์ง€์— ๋“ค์–ด๊ฐ€๋ฉด, ์šด์˜์ฒด์ œ๋Š” Linux, ์•„ํ‚คํ…์ฒ˜๋Š” x86_64, ๋ฐฐํฌํŒ์€ Ubuntu, ๋ฒ„์ „์€ 16.04๋ฅผ ๋ˆ„๋ฅด์‹œ๊ณ , local ์„ค์น˜๋ณธ์„ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ ์šฉ๋Ÿ‰์€ ์•ฝ 1.2GiB์ž…๋‹ˆ๋‹ค.


$ chmod +x cuda_8.0.44_linux.run

์„ค์น˜ ํŒŒ์ผ์— ์‹คํ–‰ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.


$ sudo ./cuda_8.0.44_linux.run

๊ด€๋ฆฌ์ž ๊ถŒํ•œ์œผ๋กœ ์„ค์น˜ ํŒŒ์ผ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.


๊ณ„์•ฝ์„œ ๋™์˜๋ฌธ์ด ๋‚˜์˜ค๋ฉด, accept๋ฅผ ์ž…๋ ฅํ•˜๊ณ  Enter๋ฅผ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.


Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?

(y)es/(n)o/(q)uit: n


--> ์šฐ๋ฆฌ๋Š” ์ด๋ฏธ NVIDIA ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์น˜ํ–ˆ์œผ๋ฏ€๋กœ n์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.


Install the CUDA 8.0 Toolkit?

(y)es/(n)o/(q)uit: y

--> NVIDIA CUDA 8.0 Toolkit์„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

Enter Toolkit Location
   [ default is /usr/local/cuda-8.0 ]: 

--> ์„ค์น˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ Enter๋ฅผ ๋ˆ„๋ฅด๋ฉด, default ๊ฒฝ๋กœ์— ์„ค์น˜๋ฉ๋‹ˆ๋‹ค.

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

--> ์›ฌ๋งŒํ•˜๋ฉด, y๋ฅผ ๋ˆŒ๋Ÿฌ์ค์‹œ๋‹ค. (๋Œ€๋ถ€๋ถ„์˜ ์ปดํŒŒ์ผ๋Ÿฌ๋“ค์€ ๋ณดํ†ต ์œ„ ๊ฒฝ๋กœ๋กœ CUDA๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.)

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: n

--> ์ƒ˜ํ”Œ ์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜์‹œ๋ฉด, y, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด n์„ ๋ˆ„๋ฆ…์‹œ๋‹ค.

Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...

--> ๊ทธ๋Ÿผ ์ด์ œ CUDA 8.0 Toolkit ์„ค์น˜๊ฐ€ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€ CUDA Toolkit ์„ค์น˜๋ฅผ ๋ชจ๋‘ ๋งˆ์ณค์Šต๋‹ˆ๋‹ค.


OpenCV ์ปดํŒŒ์ผ/์„ค์น˜ (์ด์–ด์„œ..)

์ž ์ด์ œ OpenCV ์ปดํŒŒ์ผ์„ ์œ„ํ•œ ๋ชจ๋“  ์ค€๋น„๊ฐ€ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์ด์ œ๋Š” ๋ณธ๊ฒฉ์ ์ธ OpenCV ์ปดํŒŒ์ผ์— ๋“ค์–ด๊ฐˆํ…๋ฐ์š”. ์—ฌ๊ธฐ์„œ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์‹œ๊ฐ„์„ ์กฐ๊ธˆ ๊ธธ๊ฒŒ ์žก์œผ์…”์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ ํ•˜๋Š” ์‹œ๊ฐ„์€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์‚ฌ์šฉํ•˜๋Š” ์ปดํ“จํ„ฐ์˜ CPU ์„ฑ๋Šฅ์— ๋”ฐ๋ผ ๋‹ค๋ฅด์ง€๋งŒ i7์„ ์‚ฌ์šฉํ•˜๋Š” ์ €๋„ ๋ชจ๋“  ์ฝ”์–ด์™€ ์“ฐ๋ ˆ๋“œ๋ฅผ ์†Œ๋น„ํ•˜์—ฌ ์ตœ์†Œ 1์‹œ๊ฐ„ 30๋ถ„์ด ์†Œ์š”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. (๊ทธ๋ณด๋‹ค ์ ์€ ์ฝ”์–ด๋Š” ์•„๋งˆ 3์‹œ๊ฐ„๋„ ๋„˜๊ฒŒ ๊ฑธ๋ฆฌ์ง€ ์•Š์„๊นŒ... ์˜ˆ์ƒํ•ด๋ด…๋‹ˆ๋‹ค.. ^^;)


$ mkdir build

์ž ๋นŒ๋“œ๋ฅผ ์œ„ํ•ด ์•„๊นŒ opencv ํด๋”์—์„œ build ํด๋”๋ฅผ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํด๋”์— ์ปดํŒŒ์ผํ•œ ๊ฒฐ๊ณผ๋ฌผ์ด ๋‹ด๊ฒจ์งˆ๊ฒ๋‹ˆ๋‹ค.


$ cd build

ํ•ด๋‹น ํด๋”์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.


$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr -D -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_TBB=ON -D WITH_IPP=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules  ../

์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•˜๋Š” cmake ๋ช…๋ น์–ด๋Š” ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด OpenCV๋ฅผ ์–ด๋–ป๊ฒŒ ์ปดํŒŒ์ผํ•˜๊ณ  ์‹ถ์€์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” OpenCV ํ™•์žฅ ๋ชจ๋“ˆ๊นŒ์ง€ ๊ฐ™์ด ์„ค์น˜ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ™•์žฅ ๋ชจ๋“ˆ์€ OpenCV ์ฝ”๋”ฉ ํ›„ ๋‚ด๊ฐ€ ๋งŒ๋“  ์ด๋ฏธ์ง€๋‚˜ ์˜์ƒ์„ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ํ™•๋Œ€ํ•˜์—ฌ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ํ™•์žฅ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. 


์›๋ž˜ cmake ๋ช…๋ น์–ด๋Š” make ๋ช…๋ น์„ ์ข€ ๋” ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•˜๋Š” ๋ฒ”์šฉ ๋นŒ๋“œ ๋„๊ตฌ๋กœ OS X, Linux, Windows ๋ชจ๋‘์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜, CMake๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋”ฐ๋ผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์›ํ•˜๋Š” ์ปดํŒŒ์ผ ์˜ต์…˜์„ ์ฝ”๋“œ๋กœ ์ง์ ‘ ์งค ์ˆ˜ ์žˆ์–ด์„œ ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.


์—ฌ๊ธฐ๊นŒ์ง€ cmake์— ๋Œ€ํ•œ ๊ฐ„๋žตํ•œ ์„ค๋ช…์ด์—ˆ๊ณ , OpenCV ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” CMake ๋ช…๋ น์–ด ์˜ต์…˜์€ CMakeLists.txt ํŒŒ์ผ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด ํฌ์ŠคํŠธ์—์„œ๋Š” ์ž์„ธํžˆ ๋‹ค๋ฃจ์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.


์ปดํŒŒ์ผ ์˜ต์…˜ ์„ค์ •์„ ๋ชจ๋‘ ๋๋งˆ์ณค์œผ๋ฉด, ๋‹ค์Œ์€ ์ปดํŒŒ์ผ์— ๋“ค์–ด๊ฐ€๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


$ make -j $(nproc)

์œ„ ๋ช…๋ น์–ด๋ฅผ ์น˜๋ฉด, ๋ณธ๊ฒฉ์ ์ธ ์ปดํŒŒ์ผ์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค. NVCC(CUDA)๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” NVCC ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊นŒ์ง€ ๋ชจ๋‘ ์ปดํŒŒ์ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋” ์‹œ๊ฐ„์ด ์†Œ์š”๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ „์— ๋ช…๋ น์–ด์— ๋Œ€ํ•ด ๊ฐ„๋žตํžˆ ์„ค๋ช…ํ•˜์ž๋ฉด....


$(nproc)๋Š” Linux์—์„œ ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํ”„๋กœ์„ธ์„œ์˜ ์ฝ”์–ด/์“ฐ๋ ˆ๋“œ ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ์‹œ์Šคํ…œ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. i7์€ 8 ์“ฐ๋ ˆ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— 8๊ฐœ์ด๋ฉฐ, ์œ„ ๋ช…๋ น์–ด๋ฅผ ์น  ๊ฒฝ์šฐ, ํ”„๋กœ์„ธ์„œ์— ์žˆ๋Š” 8๊ฐœ์˜ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํŒŒ์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ, ์•„๋งˆ ๋ฒ„๋ฒ…์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์ปดํŒŒ์ผ ์ค‘์— PC๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ์ปดํŒŒ์ผ์— ์‚ฌ์šฉํ•  ์“ฐ๋ ˆ๋“œ ๊ฐฏ์ˆ˜๋ฅผ ๋‚ฎ์ถฐ์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ์›ํ™œํ•˜๊ฒŒ ๋‹ค๋ฅธ ์ž‘์—…๋„ ๊ฐ€๋Šฅํ•˜๋‹ˆ ์ด ์  ์ฐธ๊ณ ํ•˜์‹œ๊ณ  ์ปดํŒŒ์ผ์„ ์ง„ํ–‰ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.


$ sudo make install

๋ชจ๋“  ์ปดํŒŒ์ผ์„ ๋๋งˆ์ณค์œผ๋ฉด..(๋ฒŒ์จ?..) ์ปดํŒŒ์ผํ•œ ๋ฐ”์ด๋„ˆ๋ฆฌ ์ฝ”๋“œ๋ฅผ ๊ด€๋ฆฌ์ž ๊ถŒํ•œ๊ณผ make ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.


$ sudo ldconfig

์ฃผ์š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— OpenCV ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.


$ pkg-config --modversion opencv

pkg-config ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ํ˜„์žฌ ์„ค์น˜๋œ opencv ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฒ„์ „์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

3.1.0์ด๋ผ๊ณ  ๋‚˜์˜ค์‹œ๋ฉด, ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.


์ด์ œ ๊ทธ๋Ÿผ ์˜ˆ์ œ๋กœ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ณ , ์ปดํŒŒ์ผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


์œ„ ์ฝ”๋“œ๋Š” ์ž„์˜์˜ ์ด๋ฏธ์ง€๋ฅผ ํ•œ ์žฅ ๋ถˆ๋Ÿฌ์™€์„œ, ๊ทธ ์ด๋ฏธ์ง€๋ฅผ ๋„์›Œ์ฃผ๋Š” ์•„์ฃผ ์‰ฌ์šด ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค

์ปดํŒŒ์ผ์„ ํ•œ ๋ฒˆ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


$ g++ -o main main.cpp `pkg-config opencv --cflags --libs`

g++ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ์ปดํŒŒ์ผํ•ฉ๋‹ˆ๋‹ค.


ํ‚ค์•ผ.. ~_~ 

(์ถœ์ฒ˜: ๋‚˜๋ฌด์œ„ํ‚ค)


๋งˆ์น˜๋ฉฐ...

์ฒ˜์Œ ์„ค์น˜๋ถ€ํ„ฐ๊ฐ€ ๋นก์‹ (?) OpenCV ์„ค์น˜์˜€์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  Linux์—์„œ๋งŒ ์„ค์น˜๊ฐ€ ์ด๋ ‡๊ฒŒ ์กฐ๊ธˆ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.. ;; Windows์—์„œ๋Š” ์„ค์น˜๊ฐ€ ์ด๊ฒƒ๋ณด๋‹ค๋Š” ์‰ฌ์›Œ์„œ ํ•œ ํŽธ์€ ๋‹คํ–‰์ด์ง€๋งŒ Windows์—์„œ๋„ OpenCV 3.x ๋ฒ„์ „ ์„ค์น˜ํ•˜๊ธฐ๋Š” ๊ฝค ๊ณ ๋‹ฌํ”ˆ ๊ฒƒ์œผ๋กœ ์••๋‹ˆ๋‹ค.


๋งˆ์ง€๋ง‰์œผ๋กœ ๊ฐ„๋‹จํ•œ ์˜ค๋ฅ˜ ์กฐ์น˜ ๋ฐฉ๋ฒ•์„ ์˜ฌ๋ฆฌ๊ณ , ์ด๋ฒˆ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์„ฑ๊ณตํ•˜์‹  ๋ถ„๋“ค ๋ชจ๋‘ ์ถ•ํ•˜๋“œ๋ฆฌ๊ณ  ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค. 




(๋ณ„์ฒจ) ์˜ค๋ฅ˜ ์กฐ์น˜

์ปดํŒŒ์ผ ๋„์ค‘, GCC ๋ฒ„์ „์ด ๋„ˆ๋ฌด ๋†’์•„์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒฝ์šฐ.


์šฐ๋ถ„ํˆฌ ๋ฆฌ๋ˆ…์Šค 16.04 ๋ฒ„์ „์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ gcc 5.3 ์ด์ƒ์˜ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์„ค์น˜๋˜๋‹ค ๋ณด๋‹ˆ 4.9 ๋ฒ„์ „์„ ์„ค์น˜ํ•ด๋„ ํ•ด๋‹น ๋ฒ„์ „์ด ๋‚ฎ์•„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ๊ทธ๋ƒฅ gcc 5.3 ๋ฒ„์ „์„ ์ œ๊ฑฐํ•˜๋ฉด ๊ทธ๋งŒ์ด์ง€๋งŒ, '๋‚˜๋Š” ์ œ๊ฑฐํ•˜๊ธฐ ์‹ซ๋‹ค' ๋Š” ๋ถ„๋“ค์€ update-alternatives ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด gcc ๊ธฐ๋ณธ ๋ช…๋ น์–ด๋ฅผ 4.9 ๋ฒ„์ „์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20

gcc์™€ g++ ๋ชจ๋‘, 5 ๋ฒ„์ „๊ณผ 4.9 ๋ฒ„์ „์— ๋Œ€ํ•œ ์„ค์ •์„ ์œ„์™€ ๊ฐ™์ด ํ•ด์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.


$ sudo update-alternatives --config gcc 

gcc ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ์„ค์ • ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.


์ €๋Š” Priority์˜ ์ˆซ์ž๋ฅผ ์œ„์— ์žˆ๋Š” ๊ฒƒ๊ณผ ๋ฐ˜๋Œ€๋กœ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Priority๋Š” ๊ทธ์ € ์šฐ์„  ์ˆœ์œ„์— ๋ถˆ๊ณผํ•œ ๊ฒƒ์ด๋ฏ€๋กœ auto mode์ผ ๋•Œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๋ณ„๋กœ ์ค‘์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์„ค์ •์ด ๋‹ค๋ฅด๋‹ค๊ณ  ํ•ด์„œ ๋‹นํ™ฉํ•˜์‹ค ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. 


์—ฌ๊ธฐ์„œ ๋ณธ์ธ์ด ์›ํ•˜์‹œ๋Š” ๋ฒ„์ „์˜ gcc์˜ ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜์‹œ๊ณ  Enter๋ฅผ ๋ˆ„๋ฅด์‹œ๋ฉด gcc์˜ ๊ธฐ๋ณธ ๋ช…๋ น์–ด ๋ฒ„์ „์ด ๋ฐ”๋€Œ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


gcc์˜ ๋ช…๋ น์–ด๊ฐ€ 4.9 ๋ฒ„์ „์œผ๋กœ ๋‚ด๋ ค๊ฐ”์Šต๋‹ˆ๋‹ค.


๋งˆ์ฐฌ๊ฐ€์ง€๋กœ g++๋„ ๋˜‘๊ฐ™์ด ์ ์šฉํ•˜์‹œ๊ณ , ๋‹ค์‹œ ์ปดํผ์ผ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.



OpenCV ๋ฒ„์ „์ด 2.4.9.1๋กœ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ

pkg-config ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด๋ดค๋”๋‹ˆ OpenCV ๋ฒ„์ „์ด 3.1.0์œผ๋กœ ๋‚˜์˜ค์ง€ ์•Š๊ณ , 2.4.9.1๋กœ ๋‚˜์˜ค๊ณ , ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋”๋‹ˆ ์ œ๋Œ€๋กœ ์ปดํŒŒ์ผ์ด ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.


์šฐ๋ถ„ํˆฌ ๋ฆฌ๋ˆ…์Šค 16.04 ์—๋Š” ํฌ์•ˆํ•˜๊ฒŒ๋„ OpenCV 2.4.9.1 ๋ฒ„์ „์ด ๋ ˆํฌ์ง€ํ„ฐ๋ฆฌ์—์„œ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๊ฒŒ๋‹ค๊ฐ€ ์ด๊ฒƒ์ด ๊ธฐ๋ณธ์œผ๋กœ ์„ค์น˜๊ฐ€ ๋˜์–ด ์žˆ์–ด์„œ, ์„ค์น˜ํ•˜๋Š” ๋ฐ ์ €๋„ ๋งŽ์€ ์• ๋ฅผ ๋จน์—ˆ๋˜ ๋ถ€๋ถ„ ์ค‘์— ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. (์ถœ์ฒ˜: http://webnautes.tistory.com/1030 )


$ sudo apt autoremove opencv-doc opencv-data libopencv-dev libopencv2.4-java libopencv2.4-jni python-opencv libopencv-core2.4 libopencv-gpu2.4 libopencv-ts2.4 libopencv-photo2.4 libopencv-contrib2.4 libopencv-imgproc2.4 libopencv-superres2.4 libopencv-stitching2.4 libopencv-ocl2.4 libopencv-legacy2.4 libopencv-ml2.4 libopencv-video2.4 libopencv-videostab2.4 libopencv-objdetect2.4 libopencv-calib3d2.4 

Python์—์„œ ์‚ฌ์šฉ๋˜๋Š” OpenCV ์ œ๊ณต ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ชจ๋“  2.4.9.1 ๋ฒ„์ „์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.


$ pkg-config --modversion opencv

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ๋ฒ„์ „์„ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด, ์ •์ƒ์ ์œผ๋กœ 3.1.0์ด ๋‚˜์˜ค๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•
TAGS.

Tistory Comments