본문 바로가기

카테고리 없음

opencv- 11일차

Simple Image Thresholding

오늘과 내일은 Thresholding 에 대해 공부하겠다. Thresholding 은 딥러닝, 머신러닝에서 매우 중요한 개념이다.

 

그리고 또한 매우 유명한 segmentation 기법인데, 배경과 그 물체를 분리하는데 사용하는 기법이다.

 

thresholding의 과정

 

각 픽셀을 threshold (임계값) 값과 비교한다.

이렇게 비교하면 두가지 그룹으로 나뉘게 된다.

 

1. intensity value greater than threshold value

2. intensity value less than threshold value

 

 

import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
_, th1= cv.threshold(img,127, 255, cv.THRESH_BINARY)


cv.imshow('Image', img)
cv.imshow('th1', th1)

cv.waitKey(0)
cv.destroyAllWindows()

 

 

thresholding 함수의 인자로는 thresholding을 처리할 이미지, threshold 값, pixel의 maximum 값, 그리고 thresholding을 처리할 함수 종류이다.

 

우선 THRESH_BINARY를 적용하면 두번째 인자로 전달한 값보다 작은 것은 0으로, 큰 것은 1로 이미지가 생성된다. 

 

import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
_, th1= cv.threshold(img,127, 255, cv.THRESH_BINARY)
_, th2= cv.threshold(img,127, 255, cv.THRESH_BINARY_INV)


cv.imshow('Image', img)
cv.imshow('th1', th1)
cv.imshow('th2', th2)


cv.waitKey(0)
cv.destroyAllWindows()

 

반대로 THRESH_BINARY_INV를 수행하게 된다면 임계값보다 큰 것은 0이 되고 작은 것은 1이 된다.

 

 

 

import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
_, th1= cv.threshold(img,50, 255, cv.THRESH_BINARY)
_, th2= cv.threshold(img, 200, 255, cv.THRESH_BINARY_INV)
_, th3= cv.threshold(img,200, 255, cv.THRESH_TRUNC)

cv.imshow('Image', img)
cv.imshow('th1', th1)
cv.imshow('th2', th2)
cv.imshow('th3', th3)


cv.waitKey(0)
cv.destroyAllWindows()

THRESH_BINARY 와 THRESH_BINARY_INV 의 임계값을 바꿔보았고,

 

THRESH_TRUNC 함수의 기능은 임계값 보다 작은 값의 픽셀은 원본 이미지와 같게 출력하고 임계값보다 큰 값의 픽셀부터는 임계값으로 동일하게 채운다.

 

 

import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
_, th1= cv.threshold(img,50, 255, cv.THRESH_BINARY)
_, th2= cv.threshold(img,200, 255, cv.THRESH_BINARY_INV)
_, th3= cv.threshold(img,127, 255, cv.THRESH_TRUNC)

cv.imshow('Image', img)
cv.imshow('th1', th1)
cv.imshow('th2', th2)
cv.imshow('th3', th3)
)

cv.waitKey(0)
cv.destroyAllWindows()

 

 

import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
_, th1= cv.threshold(img,50, 255, cv.THRESH_BINARY)
_, th2= cv.threshold(img,200, 255, cv.THRESH_BINARY_INV)
_, th3= cv.threshold(img,127, 255, cv.THRESH_TRUNC)
_, th4= cv.threshold(img,127, 255, cv.THRESH_TOZERO)

cv.imshow('Image', img)
cv.imshow('th1', th1)
cv.imshow('th2', th2)
cv.imshow('th3', th3)
cv.imshow('th4', th4)


cv.waitKey(0)
cv.destroyAllWindows()

THRESH_TOZERO 함수의 기능은 

 

임계값보다 작은 픽셀은 0으로, 임계값보다 큰 픽셀은 원본 이미지 그대로 출력하는 것이다. 

 

import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
_, th1= cv.threshold(img,50, 255, cv.THRESH_BINARY)
_, th2= cv.threshold(img,200, 255, cv.THRESH_BINARY_INV)
_, th3= cv.threshold(img,127, 255, cv.THRESH_TRUNC)
_, th4= cv.threshold(img,127, 255, cv.THRESH_TOZERO)
_, th5= cv.threshold(img,127, 255, cv.THRESH_TOZERO_INV)

cv.imshow('Image', img)
cv.imshow('th1', th1)
cv.imshow('th2', th2)
cv.imshow('th3', th3)
cv.imshow('th4', th4)
cv.imshow('th5', th5)

cv.waitKey(0)
cv.destroyAllWindows()

 

THRESH_TOZERO_INV 함수의 기능은

반대로 임계값보다 큰 픽셀은 0으로, 임계값보다 작은 픽셀은 원본 이미지 그대로 출력한다는 것이다.