잡동사니 블로그
OpenCV로 이미지 경계 강조하기 → 침식(Erode)과 팽창(Dilate) 본문
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에서 간단하면서도 자주 활용되는 기법인듯함.
'공부용' 카테고리의 다른 글
현재 디렉토리에서 하위 디렉토리까지 ipynb_checkpoints 지우기 (0) | 2025.02.05 |
---|---|
Gradient Accumulation (0) | 2025.01.01 |
[논문 읽기] SAM 2: Segment Anything in Images and Videos (3) | 2024.10.29 |
Kmedoids clustering (3) | 2024.10.16 |
Dice Coefficient(Dice Score) -> Dice Loss Function (1) | 2024.10.09 |