What is Image Binarization?¶
Image binarization is a process that classifies pixel values in an image into two categories (typically black and white) based on a threshold, resulting in an image that only retains these two colors. For example, a photograph after binarization may only show high-contrast black-and-white regions (e.g., text vs. background). This simplification drastically reduces the complexity of subsequent image analysis and is commonly used in scenarios like text recognition and shape detection.
Why Binarization?¶
- Simplify the Image: Removes intermediate grayscale values, retaining only black and white information to reduce computational load.
- Highlight Features: Makes target regions (e.g., text, object outlines) stand out against the background, facilitating tasks like edge detection or shape recognition.
Preparation: Install and Import Libraries¶
First, ensure the OpenCV and NumPy libraries are installed:
pip install opencv-python numpy
Import the required libraries:
import cv2
import numpy as np
Core Steps: Implementing Image Binarization¶
The core of binarization is the cv2.threshold() function, with the syntax:
ret, thresh = cv2.threshold(gray, thresh_value, max_value, threshold_type)
gray: Input image (must be grayscale).thresh_value: Manually set threshold (0-255).max_value: Value assigned to pixels exceeding the threshold (usually 255, white).threshold_type: Thresholding rule (e.g., binary, inverse binary).- Returns:
ret(actual threshold used),thresh(processed binarized image).
Common Threshold Types and Effects¶
The threshold_type parameter determines pixel classification rules. Common types include:
-
cv2.THRESH_BINARY:
Pixels > threshold → white (255); else → black (0).
Formula:thresh = 255 if pixel > thresh_value else 0 -
cv2.THRESH_BINARY_INV:
Pixels > threshold → black (0); else → white (255).
Formula:thresh = 0 if pixel > thresh_value else 255 -
cv2.THRESH_OTSU:
Automatically calculates the optimal threshold (no manualthresh_valueneeded). Ideal for images with high contrast between background and foreground. Used in conjunction with other types (e.g.,THRESH_BINARY + THRESH_OTSU).
Code Practice: From Image Reading to Binarization¶
Using a grayscale image (replace "test.jpg" with your image path, e.g., a coin or text image):
# 1. Read image and convert to grayscale
img = cv2.imread("test.jpg") # Replace with your image path
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale (OpenCV uses BGR by default)
# 2. Manual threshold binarization (threshold=127)
ret, thresh_binary = cv2.threshold(
gray,
thresh_value=127, # Adjust manually based on image
max_value=255,
threshold_type=cv2.THRESH_BINARY
)
# 3. Otsu auto-threshold binarization (automatically detect optimal threshold)
ret_otsu, thresh_otsu = cv2.threshold(
gray,
thresh_value=0, # 0 triggers Otsu to auto-calculate
max_value=255,
threshold_type=cv2.THRESH_BINARY + cv2.THRESH_OTSU # Combine Otsu with binary
)
# 4. Display results with matplotlib
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 4))
plt.subplot(131), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), plt.title("Original")
plt.subplot(132), plt.imshow(thresh_binary, cmap="gray"), plt.title("Manual (127)")
plt.subplot(133), plt.imshow(thresh_otsu, cmap="gray"), plt.title("Otsu Auto")
plt.show()
Key Details: Threshold Selection¶
- Manual Threshold: For uniform brightness, try
thresh_value=127(midpoint). Adjust for contrast (e.g., 50 for dark images, 200 for bright images). - Otsu Auto-Threshold: No manual tuning needed; best for high-contrast backgrounds (e.g., text, ID photos).
Extension: Adaptive Thresholding (Handle Uneven Lighting)¶
For images with uneven lighting, global thresholding fails. Use adaptive thresholding:
thresh_adaptive = cv2.adaptiveThreshold(
gray,
max_value=255,
adaptive_method=cv2.ADAPTIVE_THRESH_MEAN_C, # Mean of local neighborhood
threshold_type=cv2.THRESH_BINARY,
block_size=11, # Neighborhood size (must be odd, e.g., 11, 15)
C=2 # Subtract 2 from neighborhood mean for final threshold
)
Summary¶
Image binarization is a foundational technique in image processing, implemented via cv2.threshold(). Key steps:
1. Read and convert to grayscale.
2. Choose threshold type (manual/Otsu/adaptive).
3. Apply binarization and visualize results.
Mastery of binarization enables advanced tasks like edge detection, text recognition, and object segmentation.
Post-Lab Exercise: Test different threshold types on the same image and compare results. Apply Otsu to a low-contrast image and contrast with manual thresholds.