Sunday, 28 January 2024

Adaptive thresholding in OpenCV

Adaptive thresholding calculate different threshold values for different regions within the image.

 

There are two approaches in adaptive thresholding.

a.   Mean based adaptive thresholding: This approach calculates the threshold value for each pixel as the mean pixel value in the neighbourhood, minus a constant value.

b.   Gaussian-weighted adaptive thresholding: This approach is similar to mean-based adaptive thresholding, but it uses a Gaussian weighted average of the pixel values in the neighbourhood to calculate the threshold value.

 

Following are the steps involved in Adaptive thresholding.

1.   Image pre-processing: Convert the image to grayscale, blur the image or both.

2.   Select the adaptive thresholding algorithm: Choose tone of the algorithm (mean-based or Gaussian-weighted).

3.   Choose neighbourhood size: neighbourhood size determines how many pixels are used to calculate the threshold value for each pixel.

4.   Calculate the threshold value for each pixel: By applying the adaptive thresholding algorithm to the neighbourhood of each pixel, we can get the pixel threshold value.

5.   Threshold the image

 

How to apply adaptive thresholding in OpenCV?

Using cv2.adaptiveThreshold method, we can apply adaptive thresholding.

 

Signature

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, c[, dst])

 

Following table summarizes the parameters of adaptiveThreshold method.

 

Parameter

Description

src

Source image on which we want to apply adaptive thresholding.

maxValue

The maximum pixel value that can be assigned to a pixel based on the thresholding result. It is set to 255 for binary images.

adaptiveMethod

Specifies the algorithm used to calculate the adaptive threshold for each pixel in the image.

 

cv2.ADAPTIVE_THRESH_MEAN_C, cv2.ADAPTIVE_THRESH_GAUSSIAN_C

thresholdType

Specifies type of thresholding.

 

a.   cv2.THRESH_BINARY: All pixel values above the calculated threshold are set to maxValue, and all pixel values below the threshold are set to 0.

b.   cv2.THRESH_BINARY_INV: The inverse of the cv2.THRESH_BINARY operation is applied. Pixel values above the threshold are set to 0, and those below the threshold are set to maxValue.

blockSize

Size of local neighborhood. In general it is an odd integer like 3, 5, 6

c

Constant value that is subtracted from mean pixel value

dst

It is optional parameter, specifies where the threshold result will be stored.

 

Example

adaptive_mean_thresholded_image = cv.adaptiveThreshold(grayed_image, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 7, 2)

 

Find the below working application.

 

adaptive_thresholding.py

import cv2 as cv

image = cv.imread('wolfs.jpeg')
grayed_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
adaptive_mean_thresholded_image = cv.adaptiveThreshold(grayed_image, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 7, 2)
adaptive_gaussian_thresholded_image = cv.adaptiveThreshold(grayed_image, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 7, 2)
adaptive_mean_thresholded_inverse_image = cv.adaptiveThreshold(grayed_image, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 7, 2)
adaptive_gaussian_thresholded_inverse_image = cv.adaptiveThreshold(grayed_image, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 7, 2)

cv.imshow('Wolfs', image)
cv.imshow('grayed_image', grayed_image)
cv.imshow('adaptive_mean_thresholded_image', adaptive_mean_thresholded_image)
cv.imshow('adaptive_gaussian_thresholded_image', adaptive_gaussian_thresholded_image)
cv.imshow('adaptive_mean_thresholded_inverse_image', adaptive_mean_thresholded_inverse_image)
cv.imshow('adaptive_gaussian_thresholded_inverse_image', adaptive_gaussian_thresholded_inverse_image)

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

 

Original image

 


 

grayed_image

 

 


 

adaptive_mean_thresholded_image

 


 

adaptive_gaussian_thresholded_image

 


 

adaptive_mean_thresholded_inverse_image


 

adaptive_gaussian_thresholded_inverse_image





Previous                                                    Next                                                    Home

No comments:

Post a Comment