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

Image Histogram Equalization Techniques

The document outlines a practical exercise using OpenCV and NumPy to perform histogram equalization on a grayscale image. It demonstrates both inbuilt and manual methods for calculating histograms and equalizing images, followed by visualizing the results using Matplotlib. The code includes steps for loading an image, calculating histograms, applying equalization, and plotting the original and equalized images along with their histograms.
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 views3 pages

Image Histogram Equalization Techniques

The document outlines a practical exercise using OpenCV and NumPy to perform histogram equalization on a grayscale image. It demonstrates both inbuilt and manual methods for calculating histograms and equalizing images, followed by visualizing the results using Matplotlib. The code includes steps for loading an image, calculating histograms, applying equalization, and plotting the original and equalized images along with their histograms.
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

DIP Practical 5

November 21, 2025

[1]: import cv2


import numpy as np
import [Link] as plt

# Load grayscale image


image_path = r"C:\Users\Vicky\AppData\Downloads\Lenna_test_image.png"
img = [Link](image_path, 0)
if img is None:
raise FileNotFoundError("Check the image path")

# ---A) Using OpenCV inbuilt functions ---

# Calculate histogram using OpenCV


hist_inbuilt = [Link]([img], [0], None, [256], [0, 256])

# Histogram equalization using OpenCV


equalized_inbuilt = [Link](img)
equalized_hist_inbuilt = [Link]([equalized_inbuilt], [0], None, [256],␣
↪[0, 256])

# --- B) Without using OpenCV inbuilt functions ---

# Calculate histogram manually


hist_manual = [Link](256, dtype=int)
for pixel in [Link]():
hist_manual[pixel] += 1

# Normalize histogram
hist_norm = hist_manual / hist_manual.sum()

# Calculate CDF (Cumulative Distribution Function)


cdf = hist_norm.cumsum()

# Create LUT for equalization


lut = [Link](255 * cdf).astype(np.uint8)

# Apply LUT

1
equalized_manual = lut[img]

# Calculate histogram of equalized image manually


equalized_hist_manual = [Link](256, dtype=int)
for pixel in equalized_manual.ravel():
equalized_hist_manual[pixel] += 1

# --- Plot all results ---

[Link](figsize=(14, 10))

# Original Image
[Link](3, 2, 1)
[Link]('Original Image')
[Link](img, cmap='gray')
[Link]('off')

# Original Histogram (Inbuilt)


[Link](3, 2, 2)
[Link]('Original Histogram (Inbuilt)')
[Link](hist_inbuilt)
[Link]([0, 256])

# Equalized Image (Inbuilt)


[Link](3, 2, 3)
[Link]('Equalized Image (Inbuilt)')
[Link](equalized_inbuilt, cmap='gray')
[Link]('off')

# Equalized Histogram (Inbuilt)


[Link](3, 2, 4)
[Link]('Equalized Histogram (Inbuilt)')
[Link](equalized_hist_inbuilt)
[Link]([0, 256])

# Equalized Image (Manual)


[Link](3, 2, 5)
[Link]('Equalized Image (Manual)')
[Link](equalized_manual, cmap='gray')
[Link]('off')

# Equalized Histogram (Manual)


[Link](3, 2, 6)
[Link]('Equalized Histogram (Manual)')
[Link](equalized_hist_manual)
[Link]([0, 256])

2
plt.tight_layout()
[Link]()

[ ]:

You might also like