📘
Image Histogram Equalization
MediumImage Processing
Implement histogram equalization to enhance image contrast using NumPy.
Histogram equalization redistributes pixel intensities to achieve a more uniform histogram, improving contrast in images with concentrated intensity ranges.
Algorithm:
- Compute histogram h[i] = count of pixels with intensity i
- Compute cumulative distribution function: CDF[i]=∑j=0ih[j]
- Normalize CDF to range [0, 255]: CDFnorm[i]=(M×N)−CDFminCDF[i]−CDFmin×255
- Map each pixel: output[x,y]=CDFnorm[input[x,y]]
Where M×N is the total number of pixels and CDFmin is the minimum non-zero CDF value.
Example:
Input:
image = [[50, 50], [50, 50]]
Output:
[[255, 255], [255, 255]]
Reasoning:
Step 1: Histogram Only intensity 50 appears, count = 4
Step 2: CDF CDF[50] = 4, all others = 0 or 4
Step 3: Normalize CDF_min = 4, total pixels = 4 CDF_norm[50] = (4-4)/(4-4) × 255 = undefined → use 255 when all same
Step 4: Map All pixels map to 255 (maximum) since they all have the same value.
When all pixels are identical, equalization maps them to maximum intensity.
Constraints:
- image: 2D numpy array with values in [0, 255]
- Return: Equalized image with same shape, values in [0, 255]
- Output values should be integers
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.