Why Color Conversion is important?
In some CV applications, it is necessary to convert color images to grayscale, since only edges and shapes are important
Some applications only require a binary image showing general shapes
The goal of thresholding is to create a simple representation of the image that highlights objects of interest by distinguishing them from the background
Idea of Thresholding
- A threshold “T” is selected
- Any point (x,y) in the image at which f(x,y) > T is called an object point
- The segmented image, denoted by g(x,y) is given by

Thresholding

Syntax
retval, thresholded_image = cv2.threshold(src, thresh, maxval, type)
- src: The source image. This should be a grayscale image.
- thresh: The threshold value. Pixel values above or below this value are processed depending on the thresholding type.
- maxval: The maximum value to use with binary thresholding types. This value is assigned to pixels that meet the threshold condition.
- type: The type of thresholding to be applied. Some common types include:
- cv2.THRESH_BINARY
- cv2.THRESH_BINARY_INV
- cv2.THRESH_TRUNC
- cv2.THRESH_TOZERO
- cv2.THRESH_TOZERO_INV
- Returns:
- retval: The threshold value used (often ignored in simple use cases).
- thresholded_image: The output image after thresholding.
Types

Binary Thresholding

Condition: If the pixel value is greater than 127 (threshold) set it to (maxVal) 255
cv2.threshold(img,127,255,cv2.THRESH_BINARY)

Binary Thresholding – Inverted

Condition: If the pixel value is greater than 127 (threshold) set it to 0
cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)

Truncate Thresholding

Condition: If the pixel value is greater than 127 (threshold) set it to 127 (threshold
cv2.threshold(img,127,255,cv2.THRESH_TRUNC)

Threshold to zero

Condition: If the pixel value is greater than 127 (threshold) keep it unchanged
cv2.threshold(img,127,255,cv2.THRESH_TOZERO)

Threshold to zero Inverted

Condition: If the pixel value is greater than 127 (threshold) set it to 0
cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)

Ways to choose Threshold
- Global Thresholding
- Adaptive Thresholding
Global Thresholding – iterative way
- Global Thresholding
- A single threshold value is chosen for the entire image.
Global Thresholding
- Initial Threshold Selection
- A common choice is the mean intensity value of the image
- Segment the image
- Divide the image into two groups using the current threshold T
- Group 1: Pixels with Intensity > T (Foreground)
- Group 2: Pixels with intensity <= T (Background)
- Calculate the mean intensities
- Compute new Threshold
- Repeat step 2 to 4 until convergence

Once convergence is achieved the final value of Tnew is used as global Threshold to binarize the image
Solved problem: Refer handwritten notes
Adaptive Mean Thresholding
thresh_mean = cv2.adaptiveThreshold (
src=img, # Input image (grayscale)
maxValue=255, # Maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types
adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C, # Adaptive method (mean calculation)
thresholdType=cv2.THRESH_BINARY, # Thresholding type (binary or inverse binary)
blockSize=3, # Size of the neighborhood area (must be odd and greater than 1)
C=5 # Constant subtracted from the mean or weighted mean
)
Adaptive Gaussian Thresholding
thresh_gaussian = cv2.adaptiveThreshold(
src=img, # Input image (grayscale)
maxValue=255, # Maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types
adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C, # Adaptive method (Gaussian-weighted mean)
thresholdType=cv2.THRESH_BINARY, # Thresholding type (binary or inverse binary)
blockSize=3, # Size of the neighborhood area (must be odd and greater than 1)
C=5 # Constant subtracted from the mean or weighted mean
)
Views: 1