DIP EXP- 8 [ HISTOGRAM EQUALIZATION ]
clc;
clear all;
close all;
a=imread('sample.jpg');
a=double(rgb2gray(a));
big=max(max(a));
h=zeros(1,256);
z=zeros(1,256);
[r, c]=size(a);
C=r*c;
h=zeros(1,256); % changed from 300 to 256, as grayscale images have a range of 0-255
for n=1:r
for m=1:c
if a(n,m)==0
a(n,m)=1;
end
t=a(n,m)+1; % added +1 to adjust for 1-based indexing in MATLAB
h(t)=h(t)+1;
end
end
pdf=h/C;
cdf(1)=pdf(1);
for x=2:256 % changed from big to 256
cdf(x)=pdf(x)+cdf(x-1);
end
new=round(cdf*255); % changed from big to 255 and removed +1
for p=1:r
for q=1:c
temp=a(p,q)+1; % added +1 to adjust for 1-based indexing in MATLAB
b(p,q)=new(temp);
t=b(p,q)+1; % added +1 to adjust for 1-based indexing in MATLAB
z(t)=z(t)+1;
end
end
b=b-1;
subplot(221); imshow(uint8(a)); title('Original Image')
subplot(222); bar(h);title('Histogram of Original Image');
subplot(223); imshow(uint8(b)); title('Histogram Equalized Image')
subplot(224); bar(z);title('Histogram of Equalized Image');