Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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 28 29 30
Archives
Today
Total
관리 메뉴

잡동사니 블로그

OpenCV로 이미지 경계 강조하기 → 침식(Erode)과 팽창(Dilate) 본문

공부용

OpenCV로 이미지 경계 강조하기 → 침식(Erode)과 팽창(Dilate)

코딩부대찌개 2025. 2. 7. 17:14

OpenCV에서 erode(침식)와 dilate(팽창)는 모폴로지 연산(morphological operations) 중 하나로, 주로 노이즈 제거, 객체 크기 조정 등에 사용됨.

1️⃣ Erode (침식, Erosion)

어두운(검은색) 영역을 확장하고, 밝은(흰색) 영역을 줄이는 연산

작은 노이즈 제거 및 개체 크기 축소

2️⃣ Dilate (팽창, Dilation)

밝은(흰색) 영역을 확장하고, 어두운(검은색) 영역을 줄이는 연산

작은 구멍(검은색) 채우기 가능 및 객체 크기 증가

 

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = np.zeros((200, 400), dtype=np.uint8)
cv2.putText(img, 'Hello world!', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.5, 255, 5)  
cv2.circle(img, (300, 100), 10, 255, -1)  
cv2.circle(img, (320, 120), 5, 255, -1)  

# 커널 생성 (3x3 정사각형)
# 합성곱 연산
kernel = np.ones((3,3), np.uint8)

# 침식(Erosion) 
eroded = cv2.erode(img, kernel, iterations=2)

# 팽창(Dilation)
dilated = cv2.dilate(img, kernel, iterations=2)

fig, axes = plt.subplots(1, 3, figsize=(15, 5))
titles = ['Original', 'Erosion', 'Dilation']
images = [img, eroded, dilated]

for ax, image, title in zip(axes, images, titles):
    ax.imshow(image, cmap='gray')
    ax.set_title(title)
    ax.axis('off')

plt.show()

 

Segmentation에서 간단하면서도 자주 활용되는 기법인듯함.

https://github.com/facebookresearch/sam2/blob/2b90b9f5ceec907a1c18123530e92e794ad901a4/sav_dataset/utils/sav_benchmark.py#L273