Introduction
The binary image has only two pixel values 0 and 1, whereas a grayscale image can have pixel values ranging from 0 to 255. Thresholding is used to create a binary image from a grayscaled image. It is one of the simplest techniques of doing image segmentation.
Types of Image Thresholding
Thresholding is of two types:
- Simple Thresholding
- Adaptive Thresholding
Importing opencv library
import cv2
Simple Thresholding : cv2.threshold()
“The simplest thresholding methods replace each pixel in an image with a black pixel if the image intensity I(i,j) is less than some fixed constant T (that is, I (i,j) < T , or a white pixel if the image intensity is greater than that constant.”
Syntax
retval, ThreshImg = cv2.threshold( src, dst,thresh, maxval, type)
src image that we want to perform thresholding on. This image should be grayscale.
dst output array of the same size and type and the same number of channels as src.
thresh thresh is the threshold value which is used to classify the pixel intensities in the grayscale image
maxval maximum value which is assigned to pixel values exceeding the threshold
type Thresholding type. This parameter is used to do different types of simple thresholding.
Opencv provides different types of simple thresholding.
- cv2.THRESH_BINARY
- cv2.THRESH_BINARY_INV
- cv2.THRESH_TRUNC
- cv2.THRESH_TOZERO
- cv2.THRESH_TOZERO_INV
Example 1: cv2.threshold() with cv2.THRESH_BINARY
#reading an image
img=cv2.imread("lena.jpg")
#Display image
window_name='imagefirst'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#here the first value ret,is the value that was used for the thresholding i.e 127 and thresh1 is the actual threshold image
ret,thresh1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
#Display image
window_name='Threshold1'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 2: cv2.threshold() with cv2.THRESH_BINARY_INV
#here the first value ret2,is the value that was used for the thresholding i.e 100 and thresh2 is the actual threshold image
ret2,thresh2 = cv2.threshold(img,100,255,cv2.THRESH_BINARY_INV)
#Display image
window_name='Threshold2'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 3: cv2.threshold() with cv2.THRESH_TRUNC
#here the first value ret3,is the value that was used for the thresholding i.e 100 and thresh3 is the actual threshold image
ret3,thresh3 = cv2.threshold(img,100,255,cv2.THRESH_TRUNC)
#Display image
window_name='thresh3'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh3)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 4: cv2.threshold() with cv2.THRESH_TOZERO
#here the first value ret4,is the value that was used for the thresholding i.e 100 and thresh4 is the actual threshold image
ret4,thresh4 = cv2.threshold(img,100,255,cv2.THRESH_TOZERO)
#Display image
window_name='thresh4'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh4)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 5: cv2.threshold() with cv2.THRESH_TOZERO_INV
#here the first value ret5,is the value that was used for the thresholding i.e 100 and thresh5 is the actual threshold image
ret5,thresh5 = cv2.threshold(img,100,255,cv2.THRESH_TOZERO_INV)
#Display image
window_name='thresh5'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,thresh5)
cv2.waitKey(0)
cv2.destroyAllWindows()
Adaptive Thresholding:cv2.adaptiveThreshold()
OpenCV provides cv2.adaptiveThreshold() which can perform Adaptive threshold operation on an image.
Syntax
cv2.adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C)
src image that we want to perform thresholding on. This image should be grayscale.
dst output array of the same size and type and the same number of channels as src.
maxval maximum value which is assigned to pixel values exceeding the threshold
adaptiveMethod: adaptiveMethod decides how the threshold value is calculated:
- cv2.ADAPTIVE_THRESH_MEAN_C: The threshold value is the mean of the neighbourhood area minus the constant C.
- cv2.ADAPTIVE_THRESH_GAUSSIAN_C: The threshold value is a gaussian-weighted sum of the neighbourhood values minus the constant C.
thresholdType: Representing the type of threshold to be used.
blockSize: It determines the size of the neighbourhood area.
C: C is a constant that is subtracted from the mean or weighted sum of the neighborhood pixels.
Example 1: cv2.adaptiveThreshold() with cv2.ADAPTIVE_THRESH_MEAN_C
#reading an image
img_second=cv2.imread("lena.jpg",0)
#Display image
window_name='imagesecond'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img_second)
cv2.waitKey(0)
cv2.destroyAllWindows()
threshold1 = cv2.adaptiveThreshold(img_second,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,5)
#Display image
window_name='threshold1'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,threshold1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 2: cv2.adaptiveThreshold() with cv2.ADAPTIVE_THRESH_GAUSSIAN_C
threshold2 = cv2.adaptiveThreshold(img_second,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,5)
#Display image
window_name='threshold2'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,threshold2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion
In this tutorial, we saw various techniques for thresholding and saw its practical implementation by using the two OpenCV functions cv2.adaptiveThreshold() and cv2.threshold().
- Also Read – OpenCV Tutorial – Reading, Displaying and Writing Image using imread() , imshow() and imwrite()
- Also Read – 12 Amazing Computer Vision Datasets You Should Know
- Also Read – Python OpenCV – Image Smoothing using Averaging, Gaussian Blur and Median Filter
- Also Read – OpenCV Tutorial – Image Colorspace Conversion using cv2.cvtColor()
Reference – https://docs.opencv.org/master/d6/d00/tutorial_py_root.html
-
My name is Sachin Mohan, an undergraduate student of Computer Science and Engineering. My area of interest is ‘Artificial intelligence’ specifically Deep learning and Machine learning. I have attended various online and offline courses on Machine learning and Deep Learning from different national and international institutes My interest toward Machine Learning and deep Learning made me intern at ISRO and also I become the 1st Runner up in TCS EngiNX 2019 contest. I always love to share my knowledge and experience and my philosophy toward learning is "Learning by doing". So thats why I believe in education which have include both theoretical as well as practical knowledge.
View all posts