You are on page 1of 1

%% Histogram Equalization Matlab Implementation

clc;
clear all;
close all;
img = imread('cameraman.tif');
[m, n] = size(img);
img = double(img);
grayLevel = length(unique(img));
ni = zeros(256,2);
for i = 1 : 255
ni(i,1) = i;
ni(i,2) = length(find(img == i));
end
sumni = sum(ni(1:end,2));
cumSumni = zeros(256,1);
for j = 1 : 256
cumSumni(j,1) = sum(sum(ni(1:j,2)));
end
final = zeros(256,1);
high = max(max(img));
for k = 1 : 256
final(k,1) = round(high * cumSumni(k,1)/sumni);
end
out = zeros(size(img));
for m = 1 : 255
idx = (img == m);
out(idx) = final(m,1);
idx = [];
end
figure;imshow(uint8(img));title('Input Image');
figure;imshow(uint8(out));title('Histogram Equalized Image');

You might also like