민자의 지식창고

Otsu' Binarization 본문

개발노트/OpenCV

Otsu' Binarization

전지적민자시점 2020. 7. 24. 17:01

Golobal threshholding 방법에서 기준값으로 우리가 정한 임의 값을 사용 하였으나, 결국은 시행착오로 거쳐서 값을 지정하는 방안 뿐입니다.

 

이미지 히스토그램이 두개의 봉우리를 가지는 bimodal 이미지라고 했을때 두 기준의 사이값을 취하면 가장 좋은 결과를 얻을 수 있습니다.

 

Otsu Binarization은 이미지 히스토그램을 분석 한 후 중간값을 취하여, Thresholding 합니다

 

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 바이너리제이션
    #적용하는 함수가 있는것이 아닌, cv2.threshold() 함수에 cv2.THRESH_OTSU 플래그
    #값을 thresholding 더하고, 기준값을 0으로 전달해 준다.
    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()

Gaussian blur를 적용하여, 확실한 봉우리를 만들고, 여기에 Otsu 알고리즘을 적용하여, 기준값을 구한 후 Thresholding을 적용하면 보다 나은 노이즈 제거를 보임을 알수 있습니다.

 

 

참고자료 - 문제가 되었을시에 글을 삭제 하도록 하겠습니다.

https://blog.naver.com/samsjang/220504782549

 

[10편] 이미지 Thresholding

이미지 프로세싱 & 컴퓨터 비전OpenCV-Python 강좌 10편 : 이미지 Thresholding 배우기 필요환경:...

blog.naver.com

728x90

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

CNN모델을 이용한 숫자 인식하기  (0) 2020.07.27
이미지 필터링 -blur  (1) 2020.07.27
Adative Thresholding  (0) 2020.07.24
OpenCV Thresholding  (0) 2020.07.24
Opencv 색 바꾸기  (0) 2020.07.23