OpenCV installation

Ref: https://docs.opencv.org/master/

License: 3-clause BSD License

Dependencies

build-essential, cmake, git, libgtk2.0-dev, pkg-config, libavcodec-dev, libavformat-dev, libswscale-dev๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ์„ค๋ช…์— ๋‚˜์˜ค๋Š” ํŒจํ‚ค์ง€๋ฅผ ํ•œ ๋ฒˆ์— ์„ค์น˜ํ•˜๋ ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

wget https://raw.githubusercontent.com/hhk7734/conf/master/opencv/package_install.sh package_install.sh ;\
sleep 1 \
&& sudo chmod +x package_install.sh \
&& ./package_install.sh

Update

sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y

Python

sudo apt install -y python-dev python3-dev python3-pip \
&& python3 -m pip install -U pip \
&& python3 -m pip install --user numpy

GCC, CMake, git

sudo apt install -y build-essential cmake git

GUI, GL

sudo apt install -y libgtk2.0-dev libgtk-3-dev libqt4-dev \
&& sudo apt install -y mesa-utils libgl1-mesa-dri libqt4-opengl-dev

pkg-config

sudo apt install -y pkg-config

FFmpeg

sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev

parallelism

sudo apt install -y libtbb2 libtbb-dev

Image codec

sudo apt install -y libjasper-dev ;\
sudo apt install -y libjpeg-dev libtiff5-dev \
&& sudo apt install -y libpng12-dev || sudo apt install -y libpng-dev

libjasper-dev ์„ค์น˜๋ฅผ ์‹คํŒจํ•œ ๊ฒฝ์šฐ, https://github.com/mdadams/jasper

์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ€๋ฉด, OpenCV๊ฐ€ ๋นŒ๋“œ๋  ๋•Œ ๋นŒ๋“œ๋ฉ๋‹ˆ๋‹ค.

Video codec

sudo apt install -y libxvidcore-dev \
&& sudo apt install -y libx264-dev \
&& sudo apt install -y libxine2-dev \
&& sudo apt install -y libv4l-dev v4l-utils

Streaming

sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

IEEE 1394 digital camera

sudo apt install -y libdc1394-22-dev

Mathmatic

sudo apt install -y gfortran libatlas-base-dev libeigen3-dev libhdf5-serial-dev

Building OpenCV from Source Using CMake

Donwload source

cd && git clone https://github.com/opencv/opencv.git
cd && git clone https://github.com/opencv/opencv_contrib.git

opencv_contrib๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค. 2019-04-08 ๊ธฐ์ค€์œผ๋กœ 2.4.13.6, 3.4.6, 4.1.0์ด ์ตœ์‹  ๋ฒ„์ „์ž…๋‹ˆ๋‹ค. ์œ„ ๋ช…๋ น์„ ํ†ตํ•ด ๋‹ค์šด๋ฐ›๋Š” ๊ฒฝ์šฐ ๊ฐ€์žฅ ์ตœ์‹  ๋ฆด๋ฆฌ์ฆˆ๋ฅผ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๋ฒ„์ „์„ ๋ฐ›๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์œ„์™€๊ฐ™์ด clone ํ›„์— branch์™€ tag๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

git branch -a
git tag -l --sort=-v:refname

ํ™•์ธ๋œ ๋ฒ„์ „ ์ค‘์— ์›ํ•˜๋Š” ๋ฒ„์ „์œผ๋กœ checkout ํ•ฉ๋‹ˆ๋‹ค.

cd ~/opencv \
&& git checkout tags/4.1.0
cd ~/opencv_contrib \
&& git checkout tags/4.1.0

CMake

CMake๋ฅผ ํ†ตํ•ด Makefile์„ ๋งŒ๋“ค ๋•Œ, ์‚ฌ์šฉํ•˜๋Š” ์˜ต์…˜์— ๋”ฐ๋ผ ์ถ”๊ฐ€์ ์œผ๋กœ ์„ค์น˜ํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งคํฌ๋กœ ์„ค์ •์„ ํ†ตํ•ด ์„ค์ •์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • -DOPENCV_PYTHON_SKIP_DETECTION=ON: Python ํŒจํ‚ค์ง€ ์„ค์น˜ ์•ˆํ•จ
  • -DOPENCV_EXTRA_MODULES_PATH=<path>/opencv_contrib/modules: OpenCV extra module ์„ค์น˜
cd ~/opencv \
&& mkdir build \
&& cd build \
&& cmake ..\
-DCMAKE_BUILD_TYPE=Release \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DBUILD_PERF_TESTS=OFF

Checking memory size and swapon

์ปดํŒŒ์ผํ•˜๊ธฐ ์ „์— ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ๊ฐ€ ์ตœ์†Œ 3 GB๊ฐ€ ๋˜๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

free -ht
total used free shared buff/cache available
Mem: 1.8G 635M 219M 29M 1.0G 1.2G
Swap: 0B 0B 0B
Total: 1.8G 635M 219M

๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒฝ์šฐ swap ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ปดํŒŒ์ผ ์†๋„๋ฅผ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด make์—์„œ -j[N-jobs] ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ N-jobs ๊ฐ’์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ๊ฐ€ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค.

ํ•„์š”ํ•œ ํฌ๊ธฐ๋งŒํผ swapfile ํฌ๊ธฐ๋ฅผ ์ •ํ•œ ๋‹ค์Œ swap ๋ฉ”๋ชจ๋ฆฌ๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

sudo fallocate -l 1.2G /swapfile
sudo chmod 600 /swapfile \
&& sudo mkswap /swapfile \
&& sudo swapon /swapfile

build

make
info

๋ฉ”๋ชจ๋ฆฌ์™€ ๋””์Šคํฌ ํฌ๊ธฐ๊ฐ€ ์ถฉ๋ถ„ํ•œ ๊ฒฝ์šฐ ๋น ๋ฅธ ์ปดํŒŒ์ผ์„ ์œ„ํ•ด make ๋Œ€์‹  make -j$(expr $(expr $(nproc) \* 6) \/ 5) ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

CoreDiskRAMSwapCommand
4 core(Odroid C1+)16 GB0.8 GB3.0 GBmake -j4
4 core(Rpi 3B+)16 GB0.8 GB3.0 GBmake -j4
4 core(Rpi 4B+)16 GB3.7 GB1.0 GBmake -j4
4 core(Odroid C2)16 GB1.7 GB2.0 GBmake -j4
6 core(Odroid N2)16 GB1.8 GB1.5 GBmake
6 core(Odroid N2)16 GB1.8 GB2.5 GBmake -j7
6 core(Odroid N2)16 GB3.6 GB0.7 GBmake -j7
8 core(Odroid XU4)16 GB1.9 GB2.5 GBmake -j9

์ปดํŒŒ์ผ์ด ๋๋‚˜๋ฉด ํ• ๋‹นํ–ˆ๋˜ Swap์„ ํ•ด์ œํ•˜๊ณ  ์ƒ์„ฑํ•œ swapfile์„ ์‚ญ์ œํ•ด ์ค๋‹ˆ๋‹ค.

sudo swapoff /swapfile \
&& sudo rm /swapfile

์ƒ์„ฑ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

sudo make install \
&& sudo ldconfig

C++

sudo find / -name opencv* | grep .pc
/usr/local/lib/pkgconfig/opencv4.pc

opencv 4.x ๊ฐ€ ์„ค์น˜๋œ ๊ฒฝ์šฐ opencv4.pc๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํŒŒ์ผ์— ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ •๋ณด, ๊ฒฝ๋กœ๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ปดํŒŒ์ผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

g++ -o [output] [input] $(pkg-config opencv4 --libs --cflags)

Python

virtual environment

๊ฐ€์ƒํ™˜๊ฒฝ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

python3 -m venv [venv_path]

์œ ์ € ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์„ค์น˜๋œ cv2 ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

sudo find / -name cv2 -type d
/usr/local/lib/python3.6/dist-packages/cv2

์œ„์—์„œ ํ™•์ธํ•œ cv2 ํŒจํ‚ค์ง€๋ฅผ

sudo cp -r /usr/local/lib/python3.6/dist-packages/cv2 [venv_path]/lib/python3.6/site-packages/

๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ํ™œ์„ฑํ™”ํ•ฉ๋‹ˆ๋‹ค.

source [venv_path]/bin/activate

OpenCV๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, numpy์™€ matplotlib๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. (numpy๋Š” ํ•„์ˆ˜๊ณ , matplotlib์€ ์„ ํƒ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค.)

python3 -m pip install -U pip \
&& python3 -m pip install --user -U numpy matplotlib

python์— OpenCV ๋ชจ๋“ˆ์ด ์ •์ƒ์ ์œผ๋กœ ๋กœ๋”ฉ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

python3 -c "import cv2 as cv"
Last updated on