THRESHOLDING

Original image

Thresholded image


Thresholding is a popular technique in digital image processing that is used to separate objects from their background. Basically in thresholding methods, either replace the pixels in an image with the black pixels, which is lower than that pre-defined constant T, or replaces with the white pixels, which is greater than that pre-defined constant T in the grayscale image. 

A color image can be thresholded as well but we have to threshold separately for each RGB component, combine them with AND operation.



as you may know, 0 intensity is black and 255 intensity is white, so in this tutorial, I use an image whose intensity is o to 255. Ok, let's move on to the content

In this blog, we talk about several thresholding techniques

1 - Binary Thresholding



import cv2 as cv
import numpy as np

img = cv.imread('gradient.png',0)
#in following methods if you confused about the (_,th1) varilable defined , the underscore 
#is used for ret value(ret is a boolean variable that returns true if the frame is 
#available) and th1 stnads for frame
_,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)

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

cv.waitKey(0)
cv.destroyAllWindows()

cv. threshold() takes several arguments and the first one is the source which we want to the threshold, the second one is threshold value (constant T), the third one is the maximum value of the pixel range (nowhere I'm using 0-255 value range), the last one is threshold method (technique)

in the binary method, as you know we use Yes/No method. if the threshold value this less than constant it whole pixels are turned into 0 value others are 255(max value). so as you can see the constant T become the boundary of the new image




2 - Binary Inverse Thresholding



import cv2 as cv
import numpy as np

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

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

cv.waitKey(0)
cv.destroyAllWindows()

is this method, we use same methods in above techniques but it vice versa. the pixels less than the threshold value put into 255(max value) and greater ones are 0



3 - THRESH_TRUNC method








import cv2 as cv
import numpy as np

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

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

cv.waitKey(0)
cv.destroyAllWindows()

in this method, the value of intensity still remains unchanged until the constant T and after that it all pixels are converted to the constant value T




4 - TOZERO method / TOZERO_INV method








import cv2 as cv
import numpy as np

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

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

cv.waitKey(0)
cv.destroyAllWindows()

as in the name, all the less than values are converted to 0 and other values remain unchanged. likewise the binary inverse, we have an inverse for TOZERO which is done the other way round




TOZERO

TOZEROINV

Adaptive thresholding

adaptive thresholding has calculated the threshold in a small region. Why is this essential, using primary thresholding is not a good idea in every condition because there might be different lighting conditions in different regions in those cases the illumination of the pixel does not vary in a linear way, it may differ from point to point, so the adaptive thresholding comes in here











import cv2 as cv
import numpy as np

img = cv.imread('sudoku.png',0)

_,th4 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
_,th5 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)

cv.imshow('image',img)
cv.imshow('th1',th4 )
cv.imshow('th1',th5 )

cv.waitKey(0)
cv.destroyAllWindows()

we use adaptive threshold() function in this adaptation, the first argument is the source, the second argument is the maximum value, the third one is adaptive methods. there are 2 methods for adaptation,

cv.ADAPTIVE_THRESH_MEAN_C - the threshold T(x,y) is a mean of the blocksize x blocksize neighborhood of (x,y) minus C

cv.ADAPTIVE_THRESH_GAUSSIAN_C - the threshold T(x,y) is a weighted sum of the blocksize x blocksize neighborhood of (x,y) minus C

and the fourth argument is blocksize and the last argument is c



Mean.jpg

Gaussian.jpg


Comments

Popular Posts