You are on page 1of 3

VEERMATA JIJABAI TECHNOLOGICAL

INSTITUTE (VJTI)

NAME: Hema Bharsat BRANCH: IT COURSE: DIP


REG NO: 211081015 ASSIGNMENT NO: - 01 DATE OF SUBMISSION:
08 MAR 2024

Histogram Equalization in Image Processing


Introduction
Histogram equalization is a method in image processing used to enhance the contrast of an
image by redistributing the intensity values. This technique improves the visibility of details in an
image, especially in images with low contrast. Histogram equalization works by stretching the
histogram of an image to cover the entire intensity range.

Histogram
A histogram represents the distribution of pixel intensities in an image. It plots the frequency of
occurrence of each intensity level on the x-axis against the number of pixels having that
intensity level on the y-axis. Histograms are useful for analyzing the brightness and contrast
characteristics of an image.

Steps Involved in Histogram Equalization


Histogram equalization can be performed in three steps:
● Compute the Histogram of the Image: Calculate the histogram of the image to
understand the distribution of pixel intensities.
● Calculate the Normalized Cumulative Sum of Histogram: Compute the cumulative sum of
the histogram values and normalize it to obtain the mapping function.
● Transform the Input Image: Use the mapping function obtained in step 2 to transform the
intensity values of the input image, resulting in an output image with improved contrast.

Code Explanation

import cv2
from google.colab.patches import cv2_imshow
import numpy as np

# Function to compute the histogram of an image


def compute_histogram(image):
flattened_image = image.flatten()
hist, _ = np.histogram(flattened_image, bins=256, range=(0, 255))
return hist

# Function to calculate the normalized cumulative sum of histogram


def normalized_histogram(hist):
total_pixels = np.sum(hist)
cumulative_sum = np.cumsum(hist)
normalized_cumulative_sum = cumulative_sum / total_pixels
return normalized_cumulative_sum

# Function to perform histogram equalization


def histogram_equalization(image, normalized_cumulative_sum):
output_image = np.zeros_like(image)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
intensity = image[i, j]
new_intensity = int(normalized_cumulative_sum[int(intensity)] * 255)
output_image[i, j] = new_intensity
return output_image

# Load the image using cv2


image = cv2.imread('a.jpg', cv2.IMREAD_GRAYSCALE)

# Display the original image


print("Original Image:")
cv2_imshow(image)

# Step 1: Compute the histogram of the image


hist = compute_histogram(image)

# Step 2: Calculate the normalized sum of histogram


normalized_cumulative_sum = normalized_histogram(hist)

# Step 3: Transform the input image to an output image


output_image = histogram_equalization(image, normalized_cumulative_sum)

# Display the equalized image


print("Equalized Image:")
cv2_imshow(output_image)

Explanation:
● Compute Histogram: The compute_histogram function calculates the histogram of the
input image using NumPy's histogram function.
● Calculate Normalized Cumulative Sum: The normalized_histogram function calculates
the normalized cumulative sum of the histogram values, which represents the mapping
function for histogram equalization.
● Histogram Equalization: The histogram_equalization function applies histogram
equalization to the input image using the mapping function obtained in the previous step.
● Loading Image and Displaying: The code loads the input image using OpenCV
(cv2.imread) and displays it using cv2_imshow.
● Step-wise Execution: The code executes histogram equalization in three steps:
computing the histogram, calculating the normalized cumulative sum, and transforming
the input image.

You might also like