a = imread("C:\Users\C010\Documents\zaamin\1.jpg");


if size(a, 3) == 3
    a_gray = rgb2gray(a);
else
    a_gray = a;
end

ori_his = imhist(a_gray,[],1);
[rows,cols] = size(a_gray);
total_pixels = rows*cols;

hist = zeros(1,256);
for i = 0:255
    hist(i+1) = sum(a_gray == i);
end

cdf = cumsum(hist) / total_pixels;

equilized_img = uint8(zeros(rows,cols));
for i = 1:rows
    for j = 1:cols
        equilized_img(i,j) = round(cdf(a_gray(i,j)+1)*255);
    end
end


subplot(2,3,1);
imshow(a);
title('Original Image');

subplot(2,3,2);
imshow(a_gray);
title('Gray Image');


subplot(2,3,3);
imshow(equilized_img);
title('Histogram Equilized Image');

