๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming/Computer Vision

[OpenCV] Morphological Operations ๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ | ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ฑ„์šฐ๊ธฐ | ๊ฐ์ฒด์˜ ๊ฒฝ๊ณ„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ | ๊ฐ์ฒด์˜ ํฌ๊ธฐ ์ค„์ด๊ธฐ | ๊ฐ์ฒด ์—ฐ๊ฒฐํ•˜๊ธฐ | ๊ฒฝ๊ณ„ ๊ฐ•์กฐํ•˜๊ธฐ

by ๋ญ…์ฆค 2023. 3. 29.
๋ฐ˜์‘ํ˜•
Morphological Operations (๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ)

 

๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ์€ ์ด๋ฏธ์ง€ ๋‚ด ๊ฐ์ฒด์˜ ํ˜•ํƒœ, ํฌ๊ธฐ, ๊ตฌ์กฐ ๋“ฑ์„ ๋ถ„์„ํ•˜๊ฑฐ๋‚˜ ๋ณ€ํ˜•ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค. ์ฃผ๋กœ ๋ฐ”์ด๋„ˆ๋ฆฌ ์ด๋ฏธ์ง€(ํ‘๋ฐฑ ์ด๋ฏธ์ง€)์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋‚ด์˜ ๊ตฌ๋ฉ์„ ์ฑ„์šฐ๊ฑฐ๋‚˜, ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•˜๊ฑฐ๋‚˜, ๊ฐ์ฒด ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์กฐ์ ˆํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌ์กฐ ์š”์†Œ(structuring element)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰๋˜๋Š”๋ฐ ๊ตฌ์กฐ ์š”์†Œ๋Š” ์ด๋ฏธ์ง€ ๋‚ด์—์„œ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ธฐ์ค€์ด ๋œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๊ตฌ์กฐ ์š”์†Œ๋Š” ์‚ฌ๊ฐํ˜•, ์›, ์‹ญ์ž๊ฐ€ ๋“ฑ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๋ชจ์–‘๊ณผ ํฌ๊ธฐ๋Š” ์ž‘์—…์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์„ ํƒ๋œ๋‹ค.

 

๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ์—๋Š” ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜๊ฐ€ ์žˆ์ง€๋งŒ, ๋Œ€ํ‘œ์ ์œผ๋กœ ํŒฝ์ฐฝ(dilation), ์นจ์‹(erosion), ์—ด๊ธฐ(opening), ๋‹ซ๊ธฐ(closing) ๋“ฑ์ด ์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ํ™•์žฅ์‹œํ‚ค๋Š” dilation ์—ฐ์‚ฐ๊ณผ ๊ฐ์ฒด๋ฅผ ์ถ•์†Œ์‹œํ‚ค๋Š” erosion ์—ฐ์‚ฐ์˜ ์—ฌ๋Ÿฌ ์กฐํ•ฉ์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

OpenCV ๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ

OpenCV์—์„œ๋Š” ํŒฝ์ฐฝ(dilation), ์นจ์‹(erosion), ์—ด๊ธฐ(opening), ๋‹ซ๊ธฐ(closing), ๊ทธ๋ผ๋””์–ธํŠธ(gradient) ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

 

  1. ํŒฝ์ฐฝ(dilation)
    1. kernel์„ ์ด๋ฏธ์ง€ ์ƒ์˜ ๋ชจ๋“  ํ”ฝ์…€์— ์ ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ํ™•์žฅ
    2. ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ์ž‘์€ ๊ตฌ๋ฉ์„ ์ฑ„์šฐ๋Š” ๋ฐ์— ์œ ์šฉ
    3. ์˜ˆ์‹œ) ํ˜ˆ๊ด€ ์ด๋ฏธ์ง€์—์„œ ํ˜ˆ๊ด€ ๋‚ด๋ถ€๋ฅผ ์ฑ„์šฐ๋Š” ๋ฐ ์‚ฌ์šฉ
  2. ์นจ์‹ (erosion)
    1. kernel์„ ์ด๋ฏธ์ง€ ์ƒ์˜ ๋ชจ๋“  ํ”ฝ์…€์— ์ ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ถ•์†Œ
    2. ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ฑฐ๋‚˜ ๊ฒฝ๊ณ„๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ์— ์œ ์šฉ
    3. ์˜ˆ์‹œ) ๊ธธ์ญ‰ํ•œ ๊ฐ์ฒด์˜ ๋ ๋ถ€๋ถ„์„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ
  3. ์—ด๊ธฐ (opening)
    1. erosion ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ํ›„์— dilation ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰
    2. ์ด๋Š” ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ์ž‘์€ ๊ตฌ๋ฉ์„ ์ฑ„์šฐ๋Š” ๋ฐ์— ์œ ์šฉ
    3. ์˜ˆ์‹œ) ๋ฐ”๋‹ฅ์— ๋ฌผ์ฒด๊ฐ€ ์žˆ๋Š” ์ด๋ฏธ์ง€์—์„œ ๋ฌผ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ณ  ์ž‘์€ ๊ตฌ๋ฉ์„ ์ฑ„์šฐ๋Š” ๋ฐ ์‚ฌ์šฉ
  4. ๋‹ซ๊ธฐ (closing)
    1. dilation ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ํ›„์— erosion ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰
    2. ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๊ฑฐ๋‚˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๋Š” ๋ฐ์— ์œ ์šฉ
    3. ์˜ˆ์‹œ) ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌผ์ฒด๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ์ด๋ฏธ์ง€์—์„œ ๋ฌผ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๋Š” ๋ฐ ์‚ฌ์šฉ
  5. ๊ทธ๋ผ๋””์–ธํŠธ (gradient)
    1. dilation ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์™€ ์นจ์‹ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•ด์„œ ๊ฐ์ฒด์˜ ๊ฒฝ๊ณ„๋ฅผ ๊ฐ•์กฐ
  6. ํƒ‘ํ–‡ (top hat)
    1. opening ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์™€ ์›๋ณธ ์ด๋ฏธ์ง€์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•ด์„œ ๊ฐ์ฒด์˜ ๊ฒฝ๊ณ„๋ฅผ ๊ฐ•์กฐ
  7. ๋ธ”๋ž™ํ–‡ (black hat)
    1. closing ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์™€ ์›๋ณธ ์ด๋ฏธ์ง€์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•ด์„œ ๊ฐ์ฒด์˜ ๊ฒฝ๊ณ„๋ฅผ ๊ฐ•์กฐ

 

 

๋ชจํด๋กœ์ง€ ์—ฐ์‚ฐ ์˜ˆ์‹œ

 

  • dilation & erosion : iteration ์œผ๋กœ ํŒฝ์ฐฝ/์นจ์‹ ์˜ ์ •๋„ ์กฐ์ ˆ
  • opening & closing & gradient : kernel ์‚ฌ์ด์ฆˆ๋กœ ์กฐ์ ˆ
import cv2

image = cv2.imread('sampl.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

eroded = cv2.erode(gray_image, None, iterations = 1)
dilated = cv2.dilate(gray_image, None, iterations = 1)

kernel_size = (3, 3)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)

opening = cv2.morphologyEx(gray_image, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(gray_image, cv2.MORPH_CLOSE, kernel)
gradient = cv2.morphologyEx(gray_image, cv2.MORPH_GRADIENT, kernel)โ€‹

 

ํŒฝ์ฐฝ(dilation) & ์นจ์‹(erosion)

 

 

 

์—ด๊ธฐ(opening) & ๋‹ซ๊ธฐ(closing)

 

๊ทธ๋ผ๋””์–ธํŠธ (gradient)

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://pyimagesearch.com/2021/04/28/opencv-morphological-operations/

 

 

๋‹ค์–‘ํ•œ ์˜ˆ์‹œ
  • ์ด๋ฏธ์ง€์—์„œ ๋…ธ์ด์ฆˆ๋ฅผ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜ ํ…์ŠคํŠธ๋ฅผ ์„ ๋ช…ํ•˜๊ฒŒ ์ „์ฒ˜๋ฆฌํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์— ์‚ฌ์šฉ
  • ์˜๋ฃŒ ์˜์ƒ์—์„œ ํ˜ˆ๊ด€ ๋ถ„๋ฆฌ, ์ข…์–‘ ๊ฒ€์ถœ, ๋ผˆ ๊ตฌ๋ถ„ ๋“ฑ์—๋„ ์‚ฌ์šฉ

 

 

๋ฐ˜์‘ํ˜•