SciPy 이미지 필터링과 변형
1. 서론
이미지 처리 분야에서는 다양한 필터링과 변형 기법이 사용됩니다. 이러한 방법을 통해 이미지를 선명하게 하거나, 노이즈를 제거하고, 특정 특징을 강조할 수 있습니다. SciPy는 scipy.ndimage
모듈을 통해 다양한 이미지 필터링과 변형 기능을 제공합니다. 이번 포스팅에서는 SciPy를 이용해 이미지 필터링과 변형을 수행하는 방법에 대해 살펴보겠습니다.
2. 환경 설정
이미지 필터링과 변형을 진행하기 위해 다음과 같은 패키지가 필요합니다. SciPy와 Matplotlib가 설치되어 있어야 합니다.
pip install scipy matplotlib
또한, 예제에서는 NumPy를 사용해 이미지를 배열 형태로 처리하므로 NumPy도 함께 설치해 주세요.
pip install numpy
3. 예제 이미지 준비
이번 예제에서는 간단한 이미지를 불러와 필터링과 변형을 수행해 보겠습니다. 먼저, 이미지 파일을 준비하고 불러오는 방법을 설명합니다.
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage
from PIL import Image
# 예제 이미지 불러오기
image = Image.open('example_image.jpg')
image_gray = image.convert('L') # 흑백 이미지로 변환
image_array = np.array(image_gray)
# 이미지 출력
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("흑백 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.show()
위 코드를 실행하면 원본 이미지와 흑백 이미지가 출력됩니다.
4. 이미지 필터링
4.1. 가우시안 필터
가우시안 필터는 이미지에서 노이즈를 제거하면서 부드럽게 하는 데 사용됩니다.
# 가우시안 필터 적용
filtered_image = ndimage.gaussian_filter(image_array, sigma=2)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("가우시안 필터 적용")
plt.imshow(filtered_image, cmap='gray')
plt.axis("off")
plt.show()
sigma
값이 클수록 더 부드러운 이미지가 생성됩니다.
4.2. 평균 필터
평균 필터는 주변 픽셀의 평균 값을 사용해 이미지를 부드럽게 하는 방법입니다.
# 평균 필터 적용
filtered_image = ndimage.uniform_filter(image_array, size=5)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("평균 필터 적용")
plt.imshow(filtered_image, cmap='gray')
plt.axis("off")
plt.show()
4.3. 미디언 필터
미디언 필터는 노이즈 제거에 효과적이며, 특히 salt-and-pepper 노이즈를 제거하는 데 유용합니다.
# 미디언 필터 적용
filtered_image = ndimage.median_filter(image_array, size=3)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("미디언 필터 적용")
plt.imshow(filtered_image, cmap='gray')
plt.axis("off")
plt.show()
5. 이미지 변형
5.1. 이미지 회전
이미지를 원하는 각도로 회전할 수 있습니다.
# 이미지 45도 회전
rotated_image = ndimage.rotate(image_array, 45, reshape=True)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("45도 회전 이미지")
plt.imshow(rotated_image, cmap='gray')
plt.axis("off")
plt.show()
5.2. 이미지 확대와 축소
이미지를 특정 비율로 확대하거나 축소할 수 있습니다.
# 이미지 확대 (2배)
zoomed_image = ndimage.zoom(image_array, 2.0)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("2배 확대 이미지")
plt.imshow(zoomed_image, cmap='gray')
plt.axis("off")
plt.show()
5.3. 이미지 이동
이미지를 특정 방향으로 이동할 수도 있습니다.
# 이미지 수평 및 수직 이동
shifted_image = ndimage.shift(image_array, shift=[50, 30])
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("원본 이미지")
plt.imshow(image_array, cmap='gray')
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("이미지 이동")
plt.imshow(shifted_image, cmap='gray')
plt.axis("off")
plt.show()
6. 결론
이번 포스팅에서는 SciPy의 ndimage
모듈을 활용하여 이미지 필터링과 변형을 수행하는 방법을 살펴보았습니다. 가우시안 필터, 평균 필터, 미디언 필터와 같은 다양한 필터링 방법과 함께 이미지 회전, 확대, 이동과 같은 변형 기법을 소개했습니다.
이미지 처리 과정에서 이러한 필터와 변형은 데이터 전처리, 특징 추출, 분석 등의 작업에 필수적인 역할을 합니다.
'Python SciPy' 카테고리의 다른 글
SciPy 레이블링과 객체 분석 (SciPy.ndimage) (0) | 2025.03.30 |
---|---|
SciPy 엣지 검출과 히스토그램 분석: SciPy.ndimage를 활용한 이미지 처리 (0) | 2025.03.29 |
SciPy 델로니 삼각 분할과 보로노이 다이어그램 (0) | 2025.03.27 |
SciPy 볼록 껍질(Convex Hull) (0) | 2025.03.26 |
SciPy KD-Tree와 최근접 이웃 탐색 (0) | 2025.03.25 |