What are the different image thresholding techniques and how to implement them?
Image thresholding is a sub-module of image segmentation where certain pixel values are altered according to a particular threshold value where the pixel value of 0 is considered black and the pixel value of  255 is considered white. So the OpenCV module is used to accordingly validate the threshold value the lower pixel value of 0 and the upper pixel value of 255 and correspondingly facilitate image segmentation as required. This article briefs on the different types of image thresholding techniques with implementation using the OpenCV module. Table of Contents Introduction to Image ThresholdingSimple thresholding using OpenCVAdaptive thresholding using OpenCVOstu’s thresholding using OpenCVSummary Introduction to Image Thresholding Image thresholding is a technique employed to facilitate easy image segmentation for various image preprocessing tasks. When considering image processing, the OpenCV module has various inbuilt functions that can be used for relevant tasks. So here the OpenCV module is utilized for image thresholding wherein the different image thresholding techniques offered by OpenCV are listed below:- Simple ThresholdingAdaptive ThresholdingOstu’s Thresholding So here OpenCV package is used as a middleman to enforce the required threshold for comparison between the pixel values and segment any required boundary from an image. So in the next section of the article let’s see how to employ each of the types of thresholding possible using OpenCV. Are you looking for a complete repository of Python libraries used in data science, check out here. Simple Thresholding using OpenCV The simple thresholding technique in OpenCV is also termed Binary Thresholding. In simple thresholding technique, a standard threshold value is set and each pixel value is compared with the threshold value. If the pixel value is less than the mentioned threshold value then the value is set to 0 or else it is set to the maximum value. So let’s explore different simple thresholding techniques. Loading the image to the working environment The images can be loaded to the working environment by using the imread() function of the OpenCV package as shown below. img=cv2.imread('/content/drive/MyDrive/Colab notebooks/Image thresholding techniques in opencv/img_thresh.jpeg') Now the loaded image can be visualized using the imshow() function of matplotlib as shown below. plt.imshow(img) plt.show() But the imshow() function by default loads a grayscale and the original image can be obtained by using the function to convert BGR image to RGB image named cv2.COLOR_BGR2RGB() as shown below. orig_img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(orig_img) plt.show() General Syntax of simple image thresholding cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique) cv2.THRESH_BINARY This is one of the simple thresholding techniques wherein if the pixel intensity is greater than the mentioned threshold then the image will be segmented to a white region that is to the upper limit of 255 or else if the intensity is lower than the image will be segmented to the lower limit 0 or the image will be segmented to the blacker region. The steps included to employ the threshold binary function of the OpenCV module arse shown below. rect,thresh=cv2.threshold(orig_img,127,255,cv2.THRESH_BINARY) Later the obtained threshold value can be used to obtain plots and to visualize the functionality as shown below. plt.imshow(thresh) plt.show() So here we can compare the original image and the binary threshold segmented image by the OpenCV module. So now let’s proceed to the next technique. cv2.THRESH_BINARY_INV It is just the inverse of the threshold binary function where the pixels greater than the threshold will be segmented to the black region or to the 0 value and for the pixels lower than the threshold value will be allocated the higher pixel values or the value of 255 pixels. Let’s see how to use the function to visualize the function for inverse threshold binary image segmentation. rect,thresh1=cv2.threshold(orig_img,127,255,cv2.THRESH_BINARY_INV) So now let’s use the threshold value to obtain plots and to visualize how the inverse threshold function has segmented the pixels. plt.imshow(thresh1) plt.show() So now we can clearly compare how the binary and the inverse binary threshold function of OpenCV segment the image according to the threshold value. For better understanding, a subplot is obtained to clearly understand the segmentation differences as shown below. plt.figure(figsize=(20,5)) plt_title=['Original Image','Binary thresholding of original image','Inverse Binary thresholding of original image'] image_list=[orig_img,thresh,thresh1] for i in range(3): plt.subplot(1,3,i+1) plt.imshow(image_list[i]) plt.title(plt_title[i]) #plt.tight_layout() plt.show() cv2.THRESH_TRUNC This function of the OpenCV module operates in such a way that if the intensity value of each of the pixels is greater than the threshold value it is truncated to the threshold or else it is truncated to the lowest value of the pixel or segmented towards the black region. The steps involved to utilize the function are shown below. rect,thresh2=cv2.threshold(orig_img,127,255,cv2.THRESH_TRUNC) Now we can use the threshold value to obtain plots to visualize the segmented image. plt.imshow(thresh2) plt.show() cv2.THRESH_TOZERO This function of OpenCV functions in such a way that if the intensity of the pixels is less than the threshold values then the pixel values get set towards 0 or the image gets segmented on the darker side or towards the Black Region. We can use this function by following the steps mentioned below. rect,thresh3 = cv2.threshold(orig_img,127,255,cv2.THRESH_TOZERO) Now the threshold values can be used to obtain plots as shown below to visualize how the function has segmented the image based on the pixel intensities. plt.imshow(thresh3) plt.show() cv2.THRESH_TOZERO_INV This function is just the vice versa of the above function where in if the intensity of the pixels is less than the pixels value will be set to the highest value or will be segmented towards the whiter region whereas for the pixels value are higher than the threshold it will be segmented to the lower pixel value and the image would be segmented towards the darker region. rect,thresh4 = cv2.threshold(orig_img,127,255,cv2.THRESH_TOZERO_INV) Now the threshold value can be used to visualize how the original images have been segmented the image based on pixel intensities as shown below. plt.imshow(thresh4) plt.show() For a better understanding of the image, segmentation lets obtain a subplot to compare the individual thresholding technique on the original image. plt.figure(figsize=(20,5)) plt_title=['Original Image','Zero thresholding of original image','Inverse Zero thresholding of original image'] image_list=[orig_img,thresh3,thresh4] for i in range(3): plt.subplot(1,3,i+1) plt.imshow(image_list[i]) plt.title(plt_title[i]) #plt.tight_layout() plt.show() plt.figure(figsize=(20,5)) So now let’s look into the next technique of image thresholding named Adaptive Thresholding. Adaptive thresholding using OpenCV Adaptive thresholding is more or less similar to simple thresholding but if closely observed in simple thresholding a global value of the threshold is used to compare the pixel intensities, but here in the adaptive thresholding technique, the threshold value gets calculated for smaller regions which account for various threshold values for different regions and tend to yield phenomenal performance for poor lighting conditions. So let’s look at the different image thresholding techniques adaptive thresholding techniques offer. But first, let’s look into the general syntax of the adaptive thresholding technique. The syntax for adaptive thresholding cv2.adaptiveThreshold(source, maxVal, adaptiveMethod, thresholdType, blocksize, constant) So the adaptive thresholding technique has two types of inbuilt functions. They are Mean Adaptive thresholdingGaussian thresholding But before applying the adaptive thresholding technique the image has to be converted to grayscale for implementation of the adaptive thresholding techniques. The steps included in converting the original image to a grayscale image are shown below. img_grey = cv2.cvtColor(orig_img, cv2.COLOR_BGR2GRAY) Now let’s use the grayscale converted image to apply the adaptive thresholding techniques. cv2.ADAPTIVE_THRESH_MEAN_C This technique of thresholding computes the means of the blocks of the pixel values with respect to a constant, wherein the constant value is subtracted from the mean of pixels. The steps to follow to implement this function are as follows. thresh5 = cv2.adaptiveThreshold(img_grey,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2) So the threshold value obtained can be visualized through a plot as shown below. plt.imshow(thresh5) plt.show() plt.imshow(thresh5) plt.show() cv2.ADAPTIVE_THRESH_GAUSSIAN_C Similar to the mean thresholding technique this thresholding technique computes the gaussian weights of the neighboring pixels with respect to a constant value wherein the constant value will be subtracted from the gaussian weights for image segmentation. The steps to be followed for implementing this technique are shown below. thresh6 = cv2.adaptiveThreshold(img_grey,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) The threshold values obtained can be visualized using plots as shown below for a better understanding of how the gaussian weights have segmented each of the pixel values. plt.imshow(thresh6) plt.show() For better understanding, a subplot was obtained to understand how the image segmentation happens on the original image with respect to adaptive image thresholding. plt.figure(figsize=(20,5)) plt_titles=['Original Image','Adaptive Mean Thresholding Image','Gaussian Thresholding Image'] imgs=[orig_img,thresh5,thresh6] for i in range(3): plt.subplot(1,3,i+1) plt.imshow(imgs[i]) plt.title(plt_titles[i]) plt.tight_layout() plt.show() Ostu’s Thresholding using OpenCV Ostu’s thresholding technique is an image thresholding technique wherein the threshold value is determined automatically for the image being passed for segmentation. This evacuates the problems associated with wrong image segmentation during various lighting conditions. Ostu’s Binary technique is best suitable and interpretable for noisy images as for noisy images manual selection of threshold values may sometimes go wrong. So firstly, for implementing Ostu’s thresholding technique has to be converted into a grayscale image as shown below. noise_img_grey = cv2.cvtColor(orig_nosie_img, cv2.COLOR_BGR2GRAY) Normal Ostu’s Thresholding The normal Ostu’s thresholding technique works on the grayscale converted image and automatically selects the threshold for the grayscale converted noisy image. The steps involved in implementing this technique are shown below. rect1,thresh7=cv2.threshold(noise_img_grey,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) Later these threshold values can be used to visualize the segmentation of the noisy image based on the automatically selected threshold value. plt.imshow(thresh7) plt.show() Gaussian Filter based Ostu’s thresholding For implementing this technique the grayscale image has to be primarily blurred using the Gaussian filter as shown below. blur_noise_img=cv2.GaussianBlur(noise_img_grey,ksize=(7,7),sigmaX=0) Note:- The ksize parameter should always be odd as it specifies the height and width of the image and sigmaX which is the standard deviation along the x-axis and can be declared as 0. So now let’s see how to obtain the threshold value using the Gaussian filter for Ostu’s thresholding. rect,thresh8 = cv2.threshold(blur_noise_img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) Now let’s use this threshold value obtained and obtain a plot to visualize the same. plt.imshow(thresh8) plt.show() Let us obtain a  subplot for a better understanding of how Ostu’s thresholding technique works for image thresholding. plt.figure(figsize=(20,5)) plt_titles=['Original Noisy Image','Grayscale image','Ostus threshold image','Guassian blurred image','Ostus thresholding image with gaussian filtering'] img_list=[orig_nosie_img,noise_img_grey,thresh7,blur_noise_img,thresh8] for i in range(5): plt.subplot(1,5,i+1) plt.imshow(img_list[i]) plt.title(plt_titles[i]) plt.tight_layout() plt.show() image','Ostus thresholding image with gaussian filtering'] img_list=[orig_nosie_img,noise_img_grey,thresh7,blur_noise_img,thresh8] for i in range(5): plt.subplot(1,5,i+1) plt.imshow(img_list[i]) plt.title(plt_titles[i]) plt.tight_layout() plt.show() Summary In short image thresholding techniques is one of the integral parts of image segmentation where the image gets segmented based on pixel intensity values and the considered threshold values. The threshold value selection varies from technique to technique and the corresponding image segmentation technique will be responsible for appropriate image segmentation. So this article provides us a complete overview of how different image thresholding techniques can be applied using OpenCV and how the images get segmented based on the pixel intensity values and the threshold values being considered. References OpenCV Official DocumentationOpenCV official guide for image thresholdingColab notebook
This article briefs on the different image thresholding techniques with implementation in Opencv.
Different image thresholding techniques with their detailed explanation and implementation in OpenCV is covered here
["image processing", "OpenCV", "Python"]
["data science", "Go", "AI", "image processing", "OpenCV", "Python", "Ray", "Colab", "programming_languages:Python", "Matplotlib", "R"]
["AI", "data science", "Ray", "Colab", "OpenCV", "Matplotlib", "Python", "R", "Go", "programming_languages:Python"]
Darshan is a Master's degree holder in Data Science and Machine Learning and an everyday learner of the latest trends in Data Science and Machine Learning. He is always interested to learn new things with keen interest and implementing the same and curating rich content for Data Science, Machine Learning,NLP and AI