Post

[CV] Computer Vision(2-1): Image Processing - Binary image processing

[CV] Computer Vision(2-1): Image Processing - Binary image processing

๐Ÿ‘“Computer Vision ์ •๋ฆฌ

Binary Image Processing


  • Binary Image: an image only consisting of 0โ€™s and 1โ€™s(ํ‘๋ฐฑ ์ด๋ฏธ์ง€)
  • Binarization(์ด์ง„ํ™”): binary image occurs after thresholding operation

  • alt text

    thresholding: T๊ฐ’(์ž„๊ณ„๊ฐ’)์„ ์„ค์ •ํ•ด์„œ T๋ณด๋‹ค ํฌ๋ฉด 1, ์ž‘์œผ๋ฉด 0

alt text

์ด๋ฏธ์ง€์˜ 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:

alt text

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$ โ†’ ๋‘ ๊ทธ๋ฃน ๊ฐ„์˜ ํ‰๊ท  ์ฐจ์ด๊ฐ€ ๊ฐ€์žฅ ํฐ ์ˆœ๊ฐ„์ด ๊ฐ€์žฅ ์ข‹์€ $๐‘‡$๊ฐ’

alt text

์˜ค๋ฅธ์ชฝ ๋‹ฌ ์ด๋ฏธ์ง€๊ฐ€ ์ด์ค‘ ํ”ผํฌ๊ฐ€ ๊ฐ€์žฅ ์ž˜๋‚˜ํƒ€๋‚˜๋ฏ€๋กœ ๋ถ„๋ฆฌ๊ฐ€ ์ž˜๋œ๋‹ค!

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

alt text

Structuring Element
ํ•„ํ„ฐ ๊ฐ™์€ ๋А๋‚Œ โ†’ ์ด๋ฏธ์ง€ ์œ„์— ์ด๊ฑธ ์˜ฌ๋ ค์„œ ์–ด๋–ค ํ”ฝ์…€์„ ๋ฐ”๊ฟ€์ง€ ๊ฒฐ์ •
Anchor point: ๊ตฌ์กฐ ์š”์†Œ์—์„œ ์ค‘์‹ฌ ์—ญํ• ์„ ํ•˜๋Š” ์ขŒํ‘œ, ์ด ์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ด๋ฏธ์ง€์— ์ ์šฉ

alt text

Morphology operation ์ ์šฉ ์˜ˆ์‹œ

Dilation(ํŒฝ์ฐฝ) & Erosion(์นจ์‹)

  • Dilation:
    • ๊ตฌ์กฐ ์š”์†Œ(Structuring Element)๊ฐ€ ์ด๋ฏธ์ง€์˜ 1์œ„์น˜์— ๊ฑธ์น  ์ˆ˜๋งŒ ์žˆ์œผ๋ฉด ๊ทธ ์ž๋ฆฌ๋ฅผ 1๋กœ ๋ฐ”๊ฟˆ
    • โ†’ ํ˜•ํƒœ๊ฐ€ ๊ตต์–ด์ง€๊ณ  ์—ฐ๊ฒฐ์„ฑ์ด ๋†’์•„์ง
  • Erosion
    • ๊ตฌ์กฐ ์š”์†Œ๊ฐ€ ์ด๋ฏธ์ง€์˜ 1์œ„์น˜ ์ „๋ถ€์— ์™„์ „ํžˆ ๋งž์•„์•ผ ๊ทธ ์ž๋ฆฌ๋ฅผ 1๋กœ ์œ ์ง€
    • โ†’ ํ˜•ํƒœ๊ฐ€ ๊นŽ์ด๊ณ  ์–‡์•„์ง

alt text

์›๋ž˜ ์ด๋ฏธ์ง€์— structuring element๊ฐ€ ๋‹ฟ๋Š” ๊ณณ์ด ์žˆ์œผ๋ฉด โ†’ 1๋กœ ์ฑ„์›€

alt text

structuring element ์ „๋ถ€๊ฐ€ ํ•ด๋‹น ์ด๋ฏธ์ง€ ์˜์—ญ์— ๋‹ค ๋งž์ง€ ์•Š์œผ๋ฉด โ†’ 0์œผ๋กœ ๋งŒ๋“ฆ

Opening & Closing

  • Opening(์นจ์‹ ํ›„ ํŒฝ์ฐฝ)
    • ์ž‘์€ ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ์— ํšจ๊ณผ์ 
    • ๋ชจ์–‘ ์œ ์ง€ + ์ž‘์€ ์  ์‚ฌ๋ผ์ง
  • Closing(ํŒฝ์ฐฝ ํ›„ ์นจ์‹)
    • ์ž‘์€ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ์— ํšจ๊ณผ์ 
    • ๋‚ด๋ถ€์˜ ํ‹ˆ์ด๋‚˜ ๊นจ์ง„ ๋ถ€๋ถ„์„ ๋ฉ”์›Œ์คŒ

alt text

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)

alt text

alt text

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);
  }
}

alt text

์žฌ๊ท€ ๋Œ€์‹  ๋ฐ˜๋ณต๋ฌธ ๊ธฐ๋ฐ˜ โ†’ Stack overflow๋ฐœ์ƒ์„ ๋ฐฉ์ง€ ํ(Queue)๋ฅผ ์ด์šฉํ•ด ํ•œ ์ค„(๋ผ์ธ)์„ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ

This post is licensed under CC BY 4.0 by the author.