함수 설명
Sharpening이란 주변 pixel과의 차이를 극대화시켜 경계 부분의 명암 비를 증가시키는 작업을 뜻합니다.
이러한 처리를 통해 이미지의 구조적 정보 및 edge를 강조시켜 보다 선명한 이미지를 얻을 수 있습니다.
해당 픽셀만 강조하기 위해 주변부의 픽셀의 값을 감소시켜야합니다. 위처럼 강조될 강도를 maks 중앙에 지정하고(9), 주변부의 값을 줄여 총합이 1이 되도록 mask를 생성합니다.
생성된 mask를 가지고 이미지를 필터링하면 강조된 이미지를 얻을 수 있습니다.
코드 설명
python3 기반 OpenCV 라이브러리를 사용했습니다.
kernel = np.array([
[(1 - strength) / 8, (1 - strength) / 8, (1 - strength) / 8],
[(1 - strength) / 8, strength, (1 - strength) / 8],
[(1 - strength) / 8, (1 - strength) / 8, (1 - strength) / 8]
])
cv2.filter2D(src, ddepth, kernel)
1) src : 바꾸고 싶은 source 이미지
2) ddepth : 출력 영상의 데이터타입. default는 -1로 source 이미지와 같은 타입으로 출력
3) kernel : filtering에 사용할 mask 행렬
예제
흑백 이미지를 통해 sharpening 효과를 확인하는 코드입니다.
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
# Image Load
image = io.imread('Lenna.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Sharpening function
def sharpening(image, strength):
b = (1 - strength) / 8
sharpening_kernel = np.array([[b, b, b],
[b, strength, b],
[b, b, b]])
output = cv2.filter2D(image, -1, sharpening_kernel)
return output
# Result
output1 = sharpening(gray_image, strength=7)
output2 = sharpening(gray_image, strength=17)
# view
plt.subplot(1, 3, 1)
plt.imshow(gray_image, cmap='gray')
plt.title('Original')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(output1, cmap='gray')
plt.title('strength=7')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(output2, cmap='gray')
plt.title('strength=17')
plt.axis('off')
plt.show()
s
Sharpening 강도가 커질수록 선명해지지만 원본 픽셀값을 많이 변경시키는 것이므로 부자연스러워지는 것을 확인 할 수 있다.
'Image Processing (Python)' 카테고리의 다른 글
Smoothing (0) | 2021.11.17 |
---|---|
Flip (대칭) (0) | 2021.11.16 |
Image Read (이미지 불러오기) (0) | 2021.11.11 |
Invert (색반전) (0) | 2021.11.10 |
Zero Padding (0) | 2021.11.09 |
댓글