Bird
Raised Fist0
Computer Visionml~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of image thresholding in computer vision?
easy
A. To convert an image into black and white for easier analysis
B. To increase the color depth of an image
C. To blur the image for noise reduction
D. To resize the image to smaller dimensions

Solution

  1. Step 1: Understand image thresholding

    Image thresholding simplifies images by turning pixels into black or white based on a cutoff value.
  2. Step 2: Identify the purpose

    This simplification helps in easier analysis like object detection or segmentation.
  3. Final Answer:

    To convert an image into black and white for easier analysis -> Option A
  4. Quick Check:

    Image thresholding = black and white conversion [OK]
Hint: Thresholding means black and white conversion [OK]
Common Mistakes:
  • Confusing thresholding with image resizing
  • Thinking thresholding increases color depth
  • Mixing thresholding with blurring
2. Which of the following is the correct syntax to apply binary thresholding using OpenCV in Python?
easy
A. ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
B. ret, thresh = cv2.adaptiveThreshold(image, 127, 255, cv2.THRESH_BINARY)
C. thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
D. ret, thresh = cv2.threshold(image, 255, 127, cv2.THRESH_BINARY)

Solution

  1. Step 1: Recall OpenCV binary threshold syntax

    The function cv2.threshold returns two values: the threshold used and the thresholded image.
  2. Step 2: Check parameter order and function call

    Correct call is cv2.threshold(image, threshold_value, max_value, threshold_type).
  3. Final Answer:

    ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) -> Option A
  4. Quick Check:

    cv2.threshold returns two values [OK]
Hint: cv2.threshold returns two values: ret and image [OK]
Common Mistakes:
  • Using adaptiveThreshold instead of threshold for binary
  • Not unpacking two return values
  • Swapping threshold and max values
3. Given the following code snippet, what will be the value of ret after applying Otsu's thresholding?
import cv2
image = cv2.imread('image.jpg', 0)
ret, thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
print(ret)
medium
A. The fixed threshold value 0
B. Always 255
C. The optimal threshold value found by Otsu's method
D. The maximum pixel value in the image

Solution

  1. Step 1: Understand Otsu's thresholding output

    When using cv2.THRESH_OTSU, the function ignores the input threshold (0 here) and calculates an optimal threshold automatically.
  2. Step 2: Identify what ret holds

    The variable ret stores the threshold value found by Otsu's method, not the input or max pixel value.
  3. Final Answer:

    The optimal threshold value found by Otsu's method -> Option C
  4. Quick Check:

    Otsu returns optimal threshold in ret [OK]
Hint: Otsu's ret is the best threshold found [OK]
Common Mistakes:
  • Assuming ret is always 0 or max pixel value
  • Confusing input threshold with output
  • Thinking ret is the thresholded image
4. Identify the error in this adaptive thresholding code snippet and select the correct fix:
import cv2
image = cv2.imread('image.jpg', 0)
thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 6, 2)
medium
A. Image must be read in color mode, not grayscale
B. Max value should be 127 instead of 255
C. Use cv2.THRESH_OTSU instead of cv2.THRESH_BINARY
D. Block size must be an odd number greater than 1; change 6 to 7

Solution

  1. Step 1: Check adaptiveThreshold parameters

    The block size parameter must be an odd number greater than 1 to define the neighborhood size.
  2. Step 2: Identify the error in block size

    The block size is 6, which is even and will cause a runtime error. It must be changed to an odd number greater than 1, such as 7.
  3. Final Answer:

    Block size must be an odd number greater than 1; change 6 to 7 -> Option D
  4. Quick Check:

    Block size odd and >1 [OK]
Hint: Block size in adaptiveThreshold must be odd > 1 [OK]
Common Mistakes:
  • Using even block size causing runtime error
  • Confusing max value with threshold value
  • Reading image in color instead of grayscale
5. You have an image with uneven lighting. Which thresholding method should you choose to get the best binary segmentation, and why?
hard
A. Binary thresholding with a fixed value, because it is simple and fast
B. Adaptive thresholding, because it calculates thresholds locally for different regions
C. Otsu's thresholding, because it finds a global optimal threshold automatically
D. No thresholding, just use the original image

Solution

  1. Step 1: Understand the problem of uneven lighting

    Uneven lighting means different parts of the image have different brightness levels, making a single global threshold ineffective.
  2. Step 2: Compare thresholding methods

    Binary thresholding uses one fixed value, which fails with uneven lighting. Otsu's method finds one global threshold, also insufficient. Adaptive thresholding calculates thresholds for small regions, handling uneven lighting well.
  3. Final Answer:

    Adaptive thresholding, because it calculates thresholds locally for different regions -> Option B
  4. Quick Check:

    Uneven lighting = adaptive thresholding best [OK]
Hint: Uneven light? Use adaptive thresholding [OK]
Common Mistakes:
  • Choosing global threshold methods for uneven lighting
  • Ignoring lighting variation in images
  • Skipping thresholding and using raw image