%% Dynamixel RX-64 Ekvalizace histogramu
% Martin Zeman, 2009

function [img2] = histogram(obrazek)
img = obrazek;
[sY sX] = size(img);
hgram = zeros(1,256);
for i = 1:sY
    for j = 1:sX
        hgram(img(i,j)+1) = hgram(img(i,j)+1) + 1;
    end
end

for k = 256:-1:0
    if hgram(k) ~= 0
        break;
    end
end
pomer = (255 - k) / k;
hgram = zeros(1,256);
img2 = zeros(sY,sX,'uint8');
for i = 1:sY
    for j = 1:sX
        img2(i,j) = uint8(img(i,j) + img(i,j) * pomer);
        hgram(img2(i,j)+1) = hgram(img2(i,j)+1) + 1;
    end
end
figure(3), imshow(img2);