0
0
Computer Visionml~20 mins

Histogram computation in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Histogram computation
Problem:You want to compute the color histogram of images to understand the distribution of pixel intensities. Currently, the histogram is computed but it does not correctly represent the image colors, leading to poor feature extraction for further tasks.
Current Metrics:Histogram bins do not sum up to the total number of pixels; color channels are mixed; histogram shape is inconsistent with expected color distribution.
Issue:The histogram computation mixes color channels and does not normalize counts, causing inaccurate representation of image color distribution.
Your Task
Compute separate histograms for each color channel (Red, Green, Blue) of an image, normalize them so that the sum of bins equals 1, and verify the histogram sums and shapes are correct.
Use only OpenCV and NumPy libraries.
Do not change the input image format (assume BGR as loaded by OpenCV).
Histogram bins must be 256 for each channel.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np

# Load image in BGR format
image = cv2.imread('sample.jpg')

# Check if image is loaded
if image is None:
    raise ValueError('Image not found or path is incorrect')

# Number of bins
bins = 256

# Calculate histogram for each channel separately
hist_b = cv2.calcHist([image], [0], None, [bins], [0, 256])
hist_g = cv2.calcHist([image], [1], None, [bins], [0, 256])
hist_r = cv2.calcHist([image], [2], None, [bins], [0, 256])

# Normalize histograms by total number of pixels
num_pixels = image.shape[0] * image.shape[1]
hist_b_norm = hist_b / num_pixels
hist_g_norm = hist_g / num_pixels
hist_r_norm = hist_r / num_pixels

# Verify sums
sum_b = hist_b_norm.sum()
sum_g = hist_g_norm.sum()
sum_r = hist_r_norm.sum()

print(f'Sum of normalized Blue histogram bins: {sum_b:.4f}')
print(f'Sum of normalized Green histogram bins: {sum_g:.4f}')
print(f'Sum of normalized Red histogram bins: {sum_r:.4f}')
Computed histograms separately for each color channel (B, G, R) instead of mixing channels.
Normalized each histogram by dividing by total pixel count to get probability distribution.
Added checks to verify that the sum of each normalized histogram is 1.
Results Interpretation

Before: Histograms mixed color channels and were not normalized, sums were not 1, leading to inaccurate color distribution representation.

After: Separate histograms per channel normalized to sum to 1, accurately representing the color intensity distribution in the image.

Computing separate normalized histograms per color channel provides a clear and accurate representation of image color distribution, which is essential for tasks like image classification or enhancement.
Bonus Experiment
Try computing histograms for grayscale images and compare the histogram shape with color channel histograms.
💡 Hint
Convert the image to grayscale using cv2.cvtColor and compute a single histogram with 256 bins. Observe how the distribution differs from color histograms.