[go: up one dir, main page]

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

Histogram Equalization Code and Explanation

Uploaded by

rpragnya6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Histogram Equalization Code and Explanation

Uploaded by

rpragnya6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Manual Histogram Equalization in

MATLAB
This document provides the MATLAB code for manual histogram equalization and includes
detailed explanations for each step.

Step 1: Load Image


Load the grayscale image using 'imread' and display it using 'imshow'. Only these two built-
in functions are allowed.

img = imread('low_contrast_image.png');
imshow(img); title('Original Image');

Step 2: Set Image Size Manually


Specify the image size manually instead of using the 'size' function.

rows = 256; % <-- change this to your image height


cols = 256; % <-- change this to your image width

Step 3: Compute Histogram


Initialize a histogram array with 256 bins and count the frequency of each intensity value in
the image.

histogram = zeros(1, 256);


for i = 1:rows
for j = 1:cols
pixel = img(i, j);
histogram(pixel + 1) = histogram(pixel + 1) + 1;
end
end

Step 4: Compute PDF


Calculate the probability of each intensity value by dividing its count by the total number of
pixels.

total_pixels = rows * cols;


pdf = zeros(1, 256);
for i = 1:256
pdf(i) = histogram(i) / total_pixels;
end
Step 5: Compute CDF
Calculate the cumulative distribution function (CDF) to be used for remapping pixel values.

cdf = zeros(1, 256);


cdf(1) = pdf(1);
for i = 2:256
cdf(i) = cdf(i - 1) + pdf(i);
end

Step 6: Create Mapping Using CDF


Map old intensity values to new ones using the CDF, scaling to [0, 255] and rounding
manually.

mapping = zeros(1, 256);


for i = 1:256
x = cdf(i) * 255;
dec = x - fix(x);
if dec >= 0.5
x = fix(x) + 1;
else
x = fix(x);
end
mapping(i) = x;
end

Step 7: Apply Mapping to Image


Create a new image by replacing each pixel with its mapped value from the mapping array.

equalized_img = img;
for i = 1:rows
for j = 1:cols
val = img(i, j);
equalized_img(i, j) = mapping(val + 1);
end
end
imshow(equalized_img); title('Equalized Image');

Step 8: Compute Mean and Std Dev of Original Image


Calculate the mean and standard deviation manually. Use Newton-Raphson method to
approximate square root.

sum_orig = 0;
for i = 1:rows
for j = 1:cols
sum_orig = sum_orig + img(i,j);
end
end
mean_orig = sum_orig / total_pixels;

sum_sq_orig = 0;
for i = 1:rows
for j = 1:cols
diff = img(i,j) - mean_orig;
sum_sq_orig = sum_sq_orig + diff * diff;
end
end

std_orig = sum_sq_orig / total_pixels;


x = std_orig;
for k = 1:10
x = 0.5 * (x + std_orig / x);
end
std_orig = x;

Step 9: Compute Std Dev of Equalized Image


Repeat the mean and standard deviation calculation for the equalized image.

sum_eq = 0;
for i = 1:rows
for j = 1:cols
sum_eq = sum_eq + equalized_img(i,j);
end
end
mean_eq = sum_eq / total_pixels;

sum_sq_eq = 0;
for i = 1:rows
for j = 1:cols
diff = equalized_img(i,j) - mean_eq;
sum_sq_eq = sum_sq_eq + diff * diff;
end
end

x = sum_sq_eq / total_pixels;
for k = 1:10
x = 0.5 * (x + sum_sq_eq / total_pixels / x);
end
std_eq = x;

Step 10: Report Result


Print and compare standard deviations of original and equalized images to assess contrast
improvement.

fprintf('Original Contrast (Std Dev): %.2f\n', std_orig);


fprintf('Equalized Contrast (Std Dev): %.2f\n', std_eq);

if std_eq > std_orig


disp('Contrast improved after histogram equalization.');
else
disp('No contrast improvement.');
end

You might also like