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

DIP Lab Assignment 7 - AP21110010912

The document outlines an assignment to write a program for improving image contrast using histogram equalization, with a function prototype provided. It includes code for the function, which takes an input image and the number of bins, and returns the enhanced image. The assignment also involves testing the function on two low-contrast images while varying the number of bins and visualizing the results through histograms.

Uploaded by

nandini.gogineni
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)
15 views6 pages

DIP Lab Assignment 7 - AP21110010912

The document outlines an assignment to write a program for improving image contrast using histogram equalization, with a function prototype provided. It includes code for the function, which takes an input image and the number of bins, and returns the enhanced image. The assignment also involves testing the function on two low-contrast images while varying the number of bins and visualizing the results through histograms.

Uploaded by

nandini.gogineni
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

10/21/24, 10:24 PM Assignment - 7 - Jupyter Notebook

Write a program to improve contrast of an image using histogram equalization. The


prototype of the function is as below: histogram_equalisation(input_Image,
no_of_bins); The function should return the enhanced image. Consider two low
contrast input images. Study the nature of the output image quality in each case by
varying the number of bins.

localhost:8888/notebooks/DIP/Assignment - 7.ipynb 1/6


10/21/24, 10:24 PM Assignment - 7 - Jupyter Notebook

localhost:8888/notebooks/DIP/Assignment - 7.ipynb 2/6


10/21/24, 10:24 PM Assignment - 7 - Jupyter Notebook

In [29]: import cv2


import numpy as np
import matplotlib.pyplot as plt
import os

def histogram_equalisation(input_image, no_of_bins=256):
"""Performs histogram equalization on a grayscale image.

Args:
input_image: The input grayscale image as a NumPy array.
no_of_bins (int, optional): The number of bins to use in the histog

Returns:
numpy.ndarray: The equalized image as a NumPy array.

Raises:
ValueError: If the input image is None.
"""

if input_image is None:
raise ValueError("Input image is None. Check if the image file path

hist, bins = np.histogram(input_image.flatten(), no_of_bins, [0, 256])



cdf = hist.cumsum()

cdf_normalized = cdf * hist.max() / cdf.max()

cdf_m = np.ma.masked_equal(cdf, 0)

cdf_m = (cdf_m - cdf_m.min()) * (no_of_bins - 1) / (cdf_m.max() - cdf_m

cdf = np.ma.filled(cdf_m, 0).astype('uint8')

input_image_normalized = np.interp(input_image.flatten(), bins[:-1], np

enhanced_image = cdf[input_image_normalized.astype('uint8')].reshape(in

return enhanced_image

def plot_histograms(image, title):
"""Plots the histogram and image for a given title.

Args:
image: The image as a NumPy array.
title (str): The title for the plot.
"""

plt.figure(figsize=(10, 4))

plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title(f'Image: {title}')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.hist(image.flatten(), 256, [0, 256], color='r')
plt.title(f'Histogram: {title}')
plt.show()

image1_path = r'C:\Users\DELL\OneDrive\Pictures\7th sem 4-1\DIP\image1.7'
localhost:8888/notebooks/DIP/Assignment - 7.ipynb 3/6
10/21/24, 10:24 PM Assignment - 7 - Jupyter Notebook
image2_path = r'C:\Users\DELL\OneDrive\Pictures\7th sem 4-1\DIP\image2.7'

if not os.path.exists(image1_path):
raise FileNotFoundError(f"File {image1_path} not found. Please check th

if not os.path.exists(image2_path):
raise FileNotFoundError(f"File {image2_path} not found. Please check th

image1 = cv2.imread(image1_path, 0)
image2 = cv2.imread(image2_path, 0)

bins = [64, 128, 256]

for bin_count in bins:
enhanced_image1 = histogram_equalisation(image1.copy(), no_of_bins=bin_
enhanced_image2 = histogram_equalisation(image2.copy(), no_of_bins=bin_

print(f"Histogram Equalization with {bin_count} bins for Image 1")
plot_histograms(enhanced_image1, f"Image 1 (Bins: {bin_count})")

print(f"Histogram Equalization with {bin_count} bins for Image 2")
plot_histograms(enhanced_image2, f"Image 2 (Bins: {bin_count})")

Histogram Equalization with 64 bins for Image 1

Histogram Equalization with 64 bins for Image 2

localhost:8888/notebooks/DIP/Assignment - 7.ipynb 4/6


10/21/24, 10:24 PM Assignment - 7 - Jupyter Notebook

Histogram Equalization with 128 bins for Image 1

Histogram Equalization with 128 bins for Image 2

Histogram Equalization with 256 bins for Image 1

Histogram Equalization with 256 bins for Image 2

localhost:8888/notebooks/DIP/Assignment - 7.ipynb 5/6


10/21/24, 10:24 PM Assignment - 7 - Jupyter Notebook

In [ ]: ​

localhost:8888/notebooks/DIP/Assignment - 7.ipynb 6/6

You might also like