Adaptive Thresholding in Python OpenCV
In the previous article, we mentioned the simple thresholding of images, and in this article, we mentioned the adaptive thresholding of images using Python OpenCV.
Adaptive thresholding has some advantages over simple thresholding. In simple thresholding, the threshold value is global, i.e., it is the same for all the pixels in the image. So, a constant threshold value may not be good in all the conditions where an image has different lighting conditions in different areas.
In adaptive thresholding, the threshold value is calculated for smaller regions, and, therefore, there will be different threshold values for different regions. So, we can get different thresholds for different regions of the same image, which gives us better results for images with varying illumination.
In Python, OpenCV provides the cv2.adaptiveThreshold() method to perform adaptive image thresholding. It has the following syntax-
cv2.adaptiveThreshold(source, max_val, adaptive_method, threshold_type, blocksize, constant)
source- It is the source image, which should be a grayscale image.
max_val- It specifies the maximum value that is assigned to pixel values exceeding the threshold.
adaptive_method- It determines how the threshold value is calculated.
threshold_type- It is type of thresholding technique.
blocksize- It is the size of a pixel neighbourhood that is used to calculate a threshold value.
constant- A constant value that is subtracted from the mean or weighted sum of the neighbourhood pixels.
These are the adaptive thresholding types-
- cv.ADAPTIVE_THRESH_MEAN_C- The mean of the neighbourhood area minus the constant C.
- cv.ADAPTIVE_THRESH_GAUSSIAN_C- A gaussian-weighted sum of the neighbourhood values minus the constant C.
Example1 of Adaptive Thresholding
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('nature.jpg',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
thresh2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,11,2)
thresh3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,11,2)
plt.subplot(221), plt.imshow(img,'gray')
plt.title('Original Image')
plt.subplot(222), plt.imshow(thresh1, 'gray')
plt.title('THRESH_BINARY')
plt.subplot(223), plt.imshow(thresh2, 'gray')
plt.title('ADAPTIVE_THRESH_MEAN_C')
plt.subplot(224), plt.imshow(thresh3, 'gray')
plt.title('ADAPTIVE_THRESH_GAUSSIAN_C')
plt.show()
The above code returns the following output-
Example2 of Adaptive Thresholding
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('salad.jpg',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
The above code returns the following output-
Related Articles
Python multiline stringglob in Python
Python heap implementation
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human detection OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples