민자의 지식창고

이미지 필터링 -blur 본문

개발노트/OpenCV

이미지 필터링 -blur

전지적민자시점 2020. 7. 27. 15:21

low-pass filter(LPF)를 이용한 이미지 블러(blur)

 

2D Convolution(Image Filterfing)

1차원 신호, 이미지 다양한 low-pass filter(LPF), high-pass filter(HPF)를 적용해 필터링

 

LPF 필터는 이미지 노이즈를 제거 하거나, 이미지를 블러링 하기 위해 사용되며, HPF 필터는 이미지에 Edge를 찾는데 활용이 됩니다.

OpenCV는 필터 커널을 이미지에 convolve하여 적용하는 cv2.filter2D() 함수를 제공 합니다

이미지의 픽셀 값을 해당 픽셀의 이웃과 평균하여, 그 값을 취하도록 할 수 있는데, 이를 averaging filter라고 합니다

 

  • 픽셀 중심의 5*5 영역
  • 모든 픽셀값을 더함
  • 더한값을 /25로 나누고 중심 픽셀 값으로 취함

이 커널이 적용된 averageing filter는 5*5 영역 내의 모든 픽셀 값등ㄹ의 평균값을 취함

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

def thresholding():
    img = cv2.imread('D:/2020/test2.jpg', cv2.IMREAD_GRAYSCALE)
    
    #전역 thresholding 적용
    ret, thr1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
    
    #otsu 바이너리제이션
    ret, thr2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
    #가우시안 블러 적용 후  otsu 바이너리제이션
    blur = cv2.GaussianBlur(img,(5,5),0)
    ret, thr3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    
    titles =['org_noisy', 'historgram', 'G-Thresholding',
             'org_noisy', 'historgram', 'Otsu-Thresholding'
             'Gaussian-filter', 'histogram', 'Otsu-thresholding'
            ]
    images = [img, 0, thr1, img, 0, thr2, blur, 0, thr3]
    
    for i in range(3):
        plt.subplot(3, 3, i*3+1), plt.imshow(images[i*3],'gray')
        plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
        
        plt.subplot(3, 3, i*3+2), plt.hist(images[i*3].ravel(),256)
        plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
        
        plt.subplot(3, 3, i*3+3), plt.imshow(images[i*3+2],'gray')
        plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
    plt.show()

thresholding()

 

이미지 블러링(Image Blurring)

이미지 블러링은 LPF 커널을 이미지에 적용하여, 달성 됨.

이미지 블러링은 노이즈 제거를 수행하는데 유용 함.

LPF은 이미지의 노이즈나 모서리 등과 같은 고주파 부분을 제거함. Edge가 무뎌지게 되고 결과적으로 이미지 블러링 효과가 도출됨.

 

 

728x90

'개발노트 > OpenCV' 카테고리의 다른 글

이미지 Contour  (0) 2020.07.27
CNN모델을 이용한 숫자 인식하기  (0) 2020.07.27
Otsu' Binarization  (0) 2020.07.24
Adative Thresholding  (0) 2020.07.24
OpenCV Thresholding  (0) 2020.07.24