Cv2.threshold
이미지 이진화의 방법인 Simple thresholding, Adaptive thresholding, Otsu’s thresholding에 대해서 알 수 있다.
기본 임계처리
이진화 처리는 간단하지만, 쉽지 않은 문제를 가지고 있다. 이진화란 영상을 흑/백으로 분류하여 처리하는 것을 말합니다.
이때 기준이 되는 임계값을 어떻게 결정할 것인지가 중요한 문제가 됩니다.
임계값보다 크면 백, 작으면 흑이 됩니다.
기본 임계처리는 사용자가 고정된 임계값을 결정하고 그 결과를 보여주는 단순한 형태입니다.
Parameters
-
src
- input image로 single-channel 이미지.(grayscale 이미지) -
thresh
- 임계값 -
maxval
- 임계값을 넘었을 때 적용할 value -
type
- thresholding type
Thresholding type
- cv2.THRESH_BINARY
- cv2.THRESH_BINARY_INV
- cv2.THRESH_TRUNC
- cv2.THRESH_TOZERO
- cv2.THRESH_TOZERO_INV
Cv2.threshold.png
Example
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('gradient.jpg',0)
ret, thresh1 = cv2.threshold(img,127,255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img,127,255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img,127,255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img,127,255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img,127,255, cv2.THRESH_TOZERO_INV)
titles =['Original','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img,thresh1,thresh2,thresh3,thresh4,thresh5]
for i in xrange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
Cv2.threshold_-_sample.jpg
Otsus method
오츠 이진화 알고리즘은 해당 페이지 참고.