0
0
Computer Visionml~20 mins

Image thresholding (binary, adaptive, Otsu) in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Image thresholding (binary, adaptive, Otsu)
Problem:You want to separate objects from the background in a grayscale image using thresholding. The current method uses a simple binary threshold with a fixed value, but it does not work well for images with uneven lighting.
Current Metrics:Visual quality: Objects are not clearly separated in shadowed or bright areas; some parts are lost or merged with background.
Issue:Fixed binary thresholding fails on images with varying illumination, causing poor segmentation.
Your Task
Improve image segmentation by applying adaptive thresholding and Otsu's thresholding methods to handle uneven lighting and find the best threshold automatically.
Use OpenCV functions for thresholding.
Do not change the input image or convert it to color.
Compare results visually and with simple pixel count metrics.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np

# Load grayscale image
image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Fixed binary threshold
_, binary_thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

# Adaptive mean threshold
adaptive_mean = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                      cv2.THRESH_BINARY, 11, 2)

# Adaptive gaussian threshold
adaptive_gauss = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                       cv2.THRESH_BINARY, 11, 2)

# Otsu's thresholding
_, otsu_thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Calculate pixel counts for white pixels (objects) in each method
binary_count = np.sum(binary_thresh == 255)
adaptive_mean_count = np.sum(adaptive_mean == 255)
adaptive_gauss_count = np.sum(adaptive_gauss == 255)
otsu_count = np.sum(otsu_thresh == 255)

print(f'White pixels count - Fixed binary: {binary_count}')
print(f'White pixels count - Adaptive mean: {adaptive_mean_count}')
print(f'White pixels count - Adaptive gaussian: {adaptive_gauss_count}')
print(f'White pixels count - Otsu: {otsu_count}')

# Save results to files
cv2.imwrite('binary_thresh.jpg', binary_thresh)
cv2.imwrite('adaptive_mean.jpg', adaptive_mean)
cv2.imwrite('adaptive_gauss.jpg', adaptive_gauss)
cv2.imwrite('otsu_thresh.jpg', otsu_thresh)
Added adaptive thresholding methods (mean and gaussian) to handle uneven lighting.
Added Otsu's thresholding to automatically find the best global threshold.
Compared pixel counts of segmented objects to evaluate thresholding effects.
Results Interpretation

Fixed binary threshold: Poor segmentation in shadows and bright spots.

Adaptive thresholding: Better local segmentation, objects clearer in all areas.

Otsu's thresholding: Automatically finds a good global threshold, improving overall segmentation.

Adaptive and Otsu thresholding methods improve image segmentation by adjusting thresholds based on local or global image properties, overcoming limitations of fixed thresholding.
Bonus Experiment
Try combining adaptive thresholding with morphological operations (like opening or closing) to remove noise and improve object shapes.
💡 Hint
Use cv2.morphologyEx with cv2.MORPH_OPEN or cv2.MORPH_CLOSE after thresholding.