0% found this document useful (0 votes)
14 views5 pages

IP Experiment 6 An

The document outlines an experiment to implement histogram equalization using Python, which enhances image contrast by redistributing pixel intensity values. It explains the theory behind histograms, categorizes images based on pixel intensity distribution, and provides a step-by-step algorithm along with sample code. The conclusion emphasizes the effectiveness of histogram equalization in improving image quality, while noting that advanced techniques may be preferred in specific applications.
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)
14 views5 pages

IP Experiment 6 An

The document outlines an experiment to implement histogram equalization using Python, which enhances image contrast by redistributing pixel intensity values. It explains the theory behind histograms, categorizes images based on pixel intensity distribution, and provides a step-by-step algorithm along with sample code. The conclusion emphasizes the effectiveness of histogram equalization in improving image quality, while noting that advanced techniques may be preferred in specific applications.
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
You are on page 1/ 5

EXPERIMENT - 6

AIM:- To implement histogram equalisation.

TOOL:- Python

THEORY:-

A histogram of a digital image represents the distribution of pixel intensity values


across the entire image. It is a discrete function, denoted as ℎ( ) = , where is
the k-th gray level, and represents the number of pixels in the image having that
gray level. The histogram provides a visual representation of how the pixel intensities
are distributed, which helps in analyzing the characteristics of the image.

Based on the histogram distribution, an image can be categorized into three types:

1. Dark Image: If most of the pixel intensities are concentrated towards the
lower gray levels (close to 0), the image appears dark.
2. Bright Image: If the pixel intensities are mostly towards the higher gray levels
(close to L-1), the image appears bright.
3. Low Contrast Image: If the pixel intensities are mostly concentrated in the
middle gray level range, the image has low contrast, making it appear dull and
lacking in details.

Histogram equalization is a technique used to enhance the contrast of an image by


redistributing the pixel intensity values more uniformly across the entire grayscale
range. This is achieved by transforming the pixel values using a cumulative
distribution function (CDF). The transformation function is given by:

where:

Sk is the new pixel intensity after transformation.

hi is the histogram count for intensity level i.

n is the total number of pixels in the image.

Each pixel intensity level rk in the original image is mapped to a new intensity level
Sk in the output image, thereby spreading the intensity values over a wider range.
𝑛
𝑘
𝑟
𝑘
𝑛
𝑘
𝑟
𝑘
ALGORITHM:-

1. Read the i/p image & its size.


2. Obtain the gray level values of each pixel & divide them by total number of gray
level values.
3. Implement the function
4. Plot the equalized histogram and original histogram.
5. Display the original and the new image.

CODE:-

import numpy as np

import cv2

import matplotlib.pyplot as plt

# Load image in grayscale

img = cv2.imread('Panda.jpeg', cv2.IMREAD_GRAYSCALE)

if img is None:

raise ValueError("Image not found or unable to load.")

img = np.array(img)

flat = img.flatten()

# Display original histogram

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

plt.subplot(2, 2, 1)

plt.title('ORIGINAL HISTOGRAM')

plt.hist(flat, bins=50, color='blue', alpha=0.7)

# Function to compute histogram

def get_histogram(image, bins=256):


𝑆
𝑘
histogram = np.zeros(bins)

for pixel in image:

histogram[pixel] += 1

return histogram

hist = get_histogram(flat, 256)

# Function to compute cumulative sum

def cumsum(a):

b = np.zeros_like(a)

b[0] = a[0]

for i in range(1, len(a)):

b[i] = b[i - 1] + a[i]

return b

cs = cumsum(hist)

# Normalize cumulative sum

nj = (cs - cs.min()) * 255

N = cs.max() - cs.min()

cs = (nj / N).astype(np.uint8)

# Apply histogram equalization

img_new = cs[flat]

plt.subplot(2, 2, 2)

plt.title('EQUALIZED HISTOGRAM')

plt.hist(img_new, bins=50, color='red', alpha=0.7)


img_new = np.reshape(img_new, img.shape)

# Display images

plt.subplot(2, 2, 3)

plt.title('ORIGINAL IMAGE')

plt.imshow(img, cmap='gray')

plt.axis('off')

plt.subplot(2, 2, 4)

plt.title('EQUALIZED IMAGE')

plt.imshow(img_new, cmap='gray')

plt.axis('off')

plt.tight_layout()

plt.show()
RESULT:-

CONCLUSION:-

Histogram equalization is a fundamental technique in image processing used to


enhance contrast by redistributing pixel intensity values. It provides a simple and
effective way to improve image quality, making it useful in various applications,
including medical imaging, remote sensing, and computer vision. However, advanced
variations such as CLAHE are preferred in cases where preserving local contrast and
reducing noise is crucial.

You might also like