[CV] Computer Vision(2-1): Image Processing - Binary image processing
๐Computer Vision ์ ๋ฆฌ
Binary Image Processing
- Binary Image: an image only consisting of
0โs
and1โs
(ํ๋ฐฑ ์ด๋ฏธ์ง) Binarization(์ด์งํ): binary image occurs after thresholding operation
thresholding: T๊ฐ(์๊ณ๊ฐ)์ ์ค์ ํด์ T๋ณด๋ค ํฌ๋ฉด 1, ์์ผ๋ฉด 0
์ด๋ฏธ์ง์ histogram์ ๊ตฌํด์ ๋ ๊ฐ์ ํผํฌ ์ฌ์ด์ ๊ณจ์ง๊ธฐ๋ฅผ ์ฐพ์ผ๋ฉด ๋จ
T=60์ ์ค์ ํ๊ณ ์ด๋ณด๋ค ํฌ๋ฉด 1, ์์ผ๋ฉด 0์ผ๋ก ์ค์ โ ์ฐ๋ฆฌ๊ฐ ์ํ๋ target๋ง detection ๊ฐ๋ฅ
๊ทธ๋ผ ์ด๋ค ์๊ฐ ์ด์์ ์ธthreshold
์ธ๊ฐ?
๊ทธ๊ฑธ ๊ตฌํ๊ธฐ ์ํ algorithm = Otsuโs Binarization Algorithm
Otsuโs Binarization Algorithm
- histogram์์ ๊ฐ์ฅ ํ์คํธ์ฐจ, ๋ถ์ฐ์ด ์์ ์ง์ ์ด ์ด์์ ์ธ T๊ฐ์ด๋ค.
- Objective function: make two split pixel sets as uniform as possible
- Two sets of pixels after splitting should have small variances
- Otsuโs algorithm minimizes the weighted within-class variance:
T๊ฐ ๊ฐ์ฅ ์์
weight
์variance
๋ฅผ ๊ตฌํด์ผํจ
์์ ๋ชฉํ:
$T= argmin_{t} v_{within}(t)$
๋ด๋ถ ๋ถ์ฐ(within-class variance
) ๊ณ์ฐ:
$v_within(t)=w_0(t)v_0(t)+w_1(t)v_1(t)$
- $w_0,w_1$: ๋ ๊ทธ๋ฃน์ ํฝ์ ๋น์จ(Weight)
- $u_0,u_1$: ๊ทธ๋ฃน๋ณ ํ๊ตฐ(mean)
- $v_0,v_1$: ๊ทธ๋ฃน๋ณ ๋ถ์ฐ(variance)
์์ ์ ๋ฆฌ๋ฅผ ํ๋ค๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ ํํ๊ฐ ๋์ด:
$T= argmax_{t} v_{between}(t)$
$v_between(t)=w_0(t)(1-w_0(t))(u_0(t)-u_1(t))^2$ โ ๋ ๊ทธ๋ฃน ๊ฐ์ ํ๊ท ์ฐจ์ด๊ฐ ๊ฐ์ฅ ํฐ ์๊ฐ์ด ๊ฐ์ฅ ์ข์ $๐$๊ฐ
์ค๋ฅธ์ชฝ ๋ฌ ์ด๋ฏธ์ง๊ฐ ์ด์ค ํผํฌ๊ฐ ๊ฐ์ฅ ์๋ํ๋๋ฏ๋ก ๋ถ๋ฆฌ๊ฐ ์๋๋ค!
Morphology
Morphology: ์ด๋ฏธ์ง ์์ ์๋ ๋ฌผ์ฒด์ ํํ๋ฅผ ๋ณ๊ฒฝํ๋ ์ฐ์ฐ
- Can be applied to both binary and grayscale images, but commonly used in binary images
โ Morphological operations:
- Define a structuring element
- Convolve the structuring element with the image
- Thresholding the result of the convolution
- input, output ๋๋ค binary image
Structuring Element
ํํฐ ๊ฐ์ ๋๋ โ ์ด๋ฏธ์ง ์์ ์ด๊ฑธ ์ฌ๋ ค์ ์ด๋ค ํฝ์ ์ ๋ฐ๊ฟ์ง ๊ฒฐ์
Anchor point: ๊ตฌ์กฐ ์์์์ ์ค์ฌ ์ญํ ์ ํ๋ ์ขํ, ์ด ์ ์ ๊ธฐ์ค์ผ๋ก ์ด๋ฏธ์ง์ ์ ์ฉ
Morphology operation ์ ์ฉ ์์
Dilation(ํฝ์ฐฝ) & Erosion(์นจ์)
- Dilation:
- ๊ตฌ์กฐ ์์(Structuring Element)๊ฐ ์ด๋ฏธ์ง์ 1์์น์ ๊ฑธ์น ์๋ง ์์ผ๋ฉด ๊ทธ ์๋ฆฌ๋ฅผ 1๋ก ๋ฐ๊ฟ
- โ ํํ๊ฐ ๊ตต์ด์ง๊ณ ์ฐ๊ฒฐ์ฑ์ด ๋์์ง
- Erosion
- ๊ตฌ์กฐ ์์๊ฐ ์ด๋ฏธ์ง์ 1์์น ์ ๋ถ์ ์์ ํ ๋ง์์ผ ๊ทธ ์๋ฆฌ๋ฅผ 1๋ก ์ ์ง
- โ ํํ๊ฐ ๊น์ด๊ณ ์์์ง
์๋ ์ด๋ฏธ์ง์
structuring element
๊ฐ ๋ฟ๋ ๊ณณ์ด ์์ผ๋ฉด โ 1๋ก ์ฑ์
structuring element
์ ๋ถ๊ฐ ํด๋น ์ด๋ฏธ์ง ์์ญ์ ๋ค ๋ง์ง ์์ผ๋ฉด โ 0์ผ๋ก ๋ง๋ฆ
Opening & Closing
- Opening(์นจ์ ํ ํฝ์ฐฝ)
- ์์ ๋ ธ์ด์ฆ ์ ๊ฑฐ์ ํจ๊ณผ์
- ๋ชจ์ ์ ์ง + ์์ ์ ์ฌ๋ผ์ง
- Closing(ํฝ์ฐฝ ํ ์นจ์)
- ์์ ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ์ ํจ๊ณผ์
- ๋ด๋ถ์ ํ์ด๋ ๊นจ์ง ๋ถ๋ถ์ ๋ฉ์์ค
Opening & Closing ์์
Connect Component
Connect Component(์ฐ๊ฒฐ ์ฑ๋ถ): ์๋ก ์ธ์ ํ ํฝ์ ๋ค์ ์งํฉ์ ์๋ฏธ
- Pixel adjacency(ํฝ์
์ธ์ ์ฑ): ์ด๋ค ํฝ์
๋ค๊ณผ ๋ถ์ด์๋์ง๋ฅผ ์ ์
- ๐ฉ4-adjacency (๐ฉ4-connectivity): a pixel has 4 neighbors (N,S,E,W)
- ๐ฉ8-adjacency (๐ฉ8-connectivity): a pixel has 8 neighbors (N,S,E,W, NE,NW,SE,SW)
N4์์๋ ๋์๋จ๋ถ๋ง ์๊ฐํ๊ธฐ ๋๋ฌธ์ ๊ฐ ์งํฉ์ด ๋ฐ๋ก๋ฐ๋ก ์ ์๋จ N8์์๋ 8๋ฐฉํฅ ๋ชจ๋ ๊ด๊ณ๋ก ์๊ฐํด์ 2๊ฐ์ connected compoenet๊ฐ ์๊น
Connected Component Labeling
ํฝ์ ๋ฉ์ด๋ฆฌ(Connected Component)๋ฅผ ์ฐพ์์ผ ํ๋๋ฐ, ๊ทธ ๋ฐฉ๋ฒ์ด ๋ฐ๋ก Labeling(๋ ์ด๋ธ๋ง)
- ๊ฐ์ ์ฐ๊ฒฐ๋ ํฝ์ ๊ทธ๋ฃน์ ๊ฐ์ ์ซ์๋ก ๋งํน
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Connected Component LabelingAlgorithm
// Flood-fill algorithm to label connected components
// Input: Binary image ๐๐,๐,0โค๐โค๐โ1,0โค๐โค๐โ1
// Output: Labelled image l๐,๐,0โค๐โค๐โ1,0โค๐โค๐โ1
Initialize l: l(j,i) = 0 if b(j,i) = 0, l(j,i)=-1 if b(j,i)=1
l(0,:) = l(M-1, :) = l(:,0) = l(:, N-1) = 0
label := 1;
for (j=1 to M-2)
for (i=1 to N-2) {
if (l(j,i) == -1) {
flood_fill4(l, j, I, label);
label++;
}
}
// ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ํฝ์
์ ์ฌ๊ท์ ์ผ๋ก ๋ฐ๋ผ๊ฐ๋ฉฐ ๊ฐ์ ๋ ์ด๋ธ์ ๋ถ์ด๋ ๋ฐฉ์
// ๋ฌธ์ ์ : ์ฌ๊ท ํธ์ถ์ด ๋๋ฌด ๊น์ด์ง๋ฉด Stack Overflow ๋ฐ์
function flood_fill4(l,j,l,label) {
if(l(j,i)==-1) {
l(j,i)=label;
flood_fill4(l,j,i+1, label);
flood_fill4(l,j-1,i, label);
flood_fill4(l,j,i-1, label);
flood_fill4(l,j+1,I,label);
}
}
์ฌ๊ท ๋์ ๋ฐ๋ณต๋ฌธ ๊ธฐ๋ฐ โ Stack overflow๋ฐ์์ ๋ฐฉ์ง ํ(Queue)๋ฅผ ์ด์ฉํด ํ ์ค(๋ผ์ธ)์ ํ ๋ฒ์ ์ฒ๋ฆฌ