% Leslie Watkins
function output = hist_eq(window)


bin_size = 16;
img1 = double(window);

img1=round((img1-min(min(img1)))./(max(max(img1-min(min(img1))))).*255);

if sum(sum(img1-img1))~=0
    output = img1;
    return;
end

img1_vect = [];
img1 = floor(img1.*(bin_size-1)./255);
for k=1:size(img1, 1)
    img1_vect=[img1_vect img1(k,:)];
end
  

[n,x]=hist(img1_vect, bin_size);



% figure; hist(img1_vect);


total=0;

new_values=zeros(1,bin_size);



for k=1:bin_size
    total=total+n(k);
    
    
    new_values(k)=total.*255./(size(img1_vect, 2));
end

% save 'test' 'img1'

for n=1:size(img1, 1)
    for m=1:size(img1, 2)
        img2(n,m)=round(new_values(img1(n,m)+1));
    end
end



% For viewing new image histogram
img2_vect=[];
for k=1:size(img2, 1)
    img2_vect=[img2_vect img2(k,:)];
end

% figure; hist(img1_vect, bin_size);
% figure;
% hist(img2_vect, bin_size);
% pause; close all;
% 
% imshow(uint8(img2));

output = double(img2);



end





