0% found this document useful (0 votes)
4 views6 pages

Image Histogram and Equalization Guide

The document outlines the process of generating pixel intensities, histograms, and performing histogram equalization on images without using library functions. It explains how to calculate pixel intensity for grayscale and RGB images, generate a histogram to represent pixel intensity distribution, and enhance image contrast through histogram equalization. The document includes code segments for each step of the process, detailing how to compute histograms and apply intensity mapping.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Image Histogram and Equalization Guide

The document outlines the process of generating pixel intensities, histograms, and performing histogram equalization on images without using library functions. It explains how to calculate pixel intensity for grayscale and RGB images, generate a histogram to represent pixel intensity distribution, and enhance image contrast through histogram equalization. The document includes code segments for each step of the process, detailing how to compute histograms and apply intensity mapping.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Task name: Generating pixel intensities, histogram, histogram

equalization of an image without using any library Functions.


Input image:

Pixel Intensity Calculation

Pixel intensity: Pixel intensity refers to the brightness value of a pixel.

• For grayscale images, intensity is directly the stored value (0–255).


• For RGB images, intensity is calculated using a luminance formula to represent
brightness as a single number.

For RGB images:

I = 0.299 × R + 0.587 × G + 0.114 × B

where

R = Red channel value

G = Green channel value


B= Blue channel value

Code segment:

if color_type == 0: # Grayscale

intensity = line[x] # directly pixel intensity

elif color_type in (2,6): # RGB/RGBA

r, g, b = line[x:x+3]

intensity = int(0.299*r + 0.587*g + 0.114*b)

The output is a 2D matrix of intensities (pixels[row][col]) ranging from 0 (black) to 255


(white).

Output:

Histogram Generation
A histogram of an image shows how many pixels have each intensity value between 0
and 255.

• X-axis: intensity values (0–255)


• Y-axis: frequency (number of pixels)

It describes the contrast and brightness distribution of the image.

Process

1. Initialize an array of 256 zeros (hist).


2. Loop through all pixels.
3. For each pixel intensity value, increment the corresponding bin in hist.

Code Example
def compute_histogram(pixels):
hist = [0]*256
for row in pixels:
for val in row:
hist[val] += 1
return hist

Output

• → number of pixels with intensity i.


hist[i]
• Can be visualized as a bar graph.

Histogram Equalization
Histogram equalization enhances contrast by redistributing intensities so they cover the
full 0–255 range.
This makes dark regions darker, bright regions brighter, and improves visibility of details.

For performing histogram equalization

Step 1: Compute Histogram

Count pixel intensities .

Step 2: Compute CDF (Cumulative Distribution Function)

𝑖
cdf[i] =∑𝑗=0 ℎ𝑖𝑠𝑡[𝑗]

This tells how many pixels are ≤ intensity i.

Step 3: Normalize CDF

Scale CDF values into 0–255 to create a mapping:

𝑐𝑑𝑓[𝑖]−𝑐𝑑𝑓𝑚𝑖𝑛
new(i) = 𝑡𝑜𝑡𝑎𝑙 * 255
− 𝑐𝑑𝑓𝑚𝑖𝑛
𝑝𝑖𝑥𝑒𝑙𝑠

Step 4: Apply Mapping

Replace every pixel with its new intensity from the mapping.

Code segment:

def histogram_equalization(pixels):
hist = compute_histogram(pixels)
total_pixels = sum(hist)

# Step 2: cumulative distribution function


cdf = []
cumsum = 0
for h in hist:
cumsum += h
[Link](cumsum)

# Step 3: normalize CDF


cdf_min = min(x for x in cdf if x > 0)
mapping = [
round((c - cdf_min)/(total_pixels - cdf_min)*255) if c > 0 else 0
for c in cdf
]
# Step 4: apply mapping
new_pixels = [[mapping[val] for val in row] for row in pixels]
return new_pixels, hist, mapping

Output

You might also like