소스 뷰어
import cv2, numpy as np, matplotlib.pyplot as plt
# 이미지 읽기
image = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE)
# 1. 가우시안 블러링 적용
size= 21
sigma= size/3
kernel = np.fromfunction(
lambda x, y :
(1/ (2 * np.pi * sigma ** 2)) \
* np.exp(- ((x - (size - 1) / 2) ** 2 + (y - (size - 1) / 2) ** 2) / (2 * sigma ** 2)),
(size, size)
)
kernel = kernel / np.sum(kernel)
blurred_image = cv2.filter2D(image, -1, kernel)
# 결과 출력
plt.figure(figsize=(12, 12))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title("Gaussian Blur")
plt.imshow(blurred_image, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()