clear;
clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                   %%
%% Skrypt implementuje wyrównanie histogramu.                         %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

A = imread('Lena.gif','gif');
[m,n] = size(A);
A = double(A);
B = zeros(m,n);
his = zeros(256,1);
t = [0:255];
for i = 1 : m
    for j = 1 : n
        k = A(i,j);
        his(k + 1) = his(k+1) + 1;
    end;
end;

% Generujemy histogram skumulowany

dist = zeros(256,1);
dist(1) = his(1);
for i = 2 : 256
    dist(i) = dist(i-1) + his(i);
end;
dist = dist/(m*n);
plot(t,his/2700);
hold on;
plot(t,dist,'LineWidth',2.5);
hold off;
for i = 1 : m
    for j = 1 : n
        B(i,j) = dist(A(i,j)+1);
    end;
end;
B = 255 * B;
B = uint8(B);
imwrite(B,'histogram_1.gif','gif');


    
