Isolating and Modifying Color Channels
OpenCV loads images in the Blue-Green-Red (BGR) format by default. Direct manipulation of individual channels allows for specific color corrections or feature extraction.
import cv2
import numpy as np
def manipulate_channel_demo():
# Load image from local path
source_path = "../data/opencv2.png"
img = cv2.imread(source_path)
if img is None:
raise FileNotFoundError(f"Image not found at {source_path}")
# Display full original image
cv2.imshow("Full Original", img)
# Extract the Blue channel (index 0)
# Shape indices: [height, width, channel]
blue_channel = img[:, :, 0]
# Display extracted channel (interpreted as grayscale)
cv2.imshow("Blue Channel Only", blue_channel)
# Suppress Blue channel influence by setting to zero
modified_img = img.copy()
modified_img[:, :, 0] = 0
cv2.imshow("No Blue Component", modified_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
manipulate_channel_demo()
Enhancing Contrast with Hisotgram Equalization
Histogram equalization redistirbutes pixel intensity values to improve image contrast. This is critical for images with low dynamic range.
Grayscale Equalizatino
For single-channel inputs, the operation maps input cumulative distribution functions directly.
import cv2
from matplotlib import pyplot as plt
def grayscale_histogram_equalization():
image_path = "../data/sunrise.jpg"
# Read as grayscale matrix (0 means no color channels)
gray_img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if gray_img is None:
return
# Apply histogram equalization algorithm
eq_img = cv2.equalizeHist(gray_img)
# Compare visual outputs
cv2.imshow("Original Gray", gray_img)
cv2.imshow("Equalized Gray", eq_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Plot statistical distribution for analysis
fig, ax = plt.subplots(2, 1, figsize=(10, 8))
ax[0].hist(gray_img.ravel(), 256, [0, 256], label='Original')
ax[0].set_title('Original Histogram Distribution')
ax[0].legend()
ax[1].hist(eq_img.ravel(), 256, [0, 256], color='r', label='Equalized')
ax[1].set_title('Post-EQ Histogram Distribution')
ax[1].legend()
plt.tight_layout()
plt.show()
Color Space Luminance Manipulation
In color images, modifying RGB channels directly alters hue and saturation artifacts. The recommended approach converts data to a luminance-aware space (like YUV) before processing.
import cv2
def color_luminance_enhancement():
color_path = "../data/sunrise.jpg"
original_color = cv2.imread(color_path)
if original_color is None:
return
# Convert BGR to YUV
yuv_image = cv2.cvtColor(original_color, cv2.COLOR_BGR2YUV)
# Separate Y channel (Luminance)
y_channel = yuv_image[:,:,0]
# Equalize only the luminance component
eq_y_channel = cv2.equalizeHist(y_channel)
# Reconstruct image
yuv_image[:,:,0] = eq_y_channel
enhanced_bgr = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2BGR)
cv2.imshow("Enhanced Color Image", enhanced_bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()
Pixel-Level Segmentation via Thresholding
Thresholding transforms continuous intensity values into binary masks, separating foreground objects from background based on a cut-off value.
import cv2
def thresholding_operations():
mask_path = "../data/lena.jpg"
# Load as single channel
src_img = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
if src_img is None:
return
# Define threshold parameters
threshold_val = 127
max_val = 255
# Standard Binary Thresholding
# Pixels > threshold become max_val, others become 0
_, binary_mask = cv2.threshold(src_img, threshold_val, max_val, cv2.THRESH_BINARY)
# Inverse Binary Thresholding
# Pixels > threshold become 0, others become max_val
_, inverse_mask = cv2.threshold(src_img, threshold_val, max_val, cv2.THRESH_BINARY_INV)
cv2.imshow("Binary Mask", binary_mask)
cv2.imshow("Inverse Binary Mask", inverse_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()