%% Basic intensity transformations
% Example script for teaching Signal and Image Processing
%
% *Course homepage:* http://cmp.felk.cvut.cz/cmp/courses/ZSO

%%

%%
% clear everything
clear all
%%
% read input image

% orig.im = imread('wom.jpg');
% bigger image to emphasize the effect
orig.im = imread('wom.jpg');

%% histogram of the original image
orig.h=hist(double(orig.im(:)),0:255)./prod(size(orig.im));

%% cumulative distributive function
orig.cumh = cumsum(orig.h);
showdata(orig,1,'original image');

%% making negative: computation with and without loops, effect of memory pre-allocation 
% Check the elapsed time for computation.
% The Matlab is very effective in matrix computation
% and *very* ineffective when looping without memory pre-allocation
lut = 255:-1:0;
t = cputime;
neg.im = uint8(lut(double(orig.im)+1));
elapsed.lut = cputime-t;

%%
% looping *with*  memory pre-allocation
neg.im = zeros(size(orig.im));
t = cputime;
for i=1:size(orig.im,1),
  for j=1:size(orig.im,2),
	neg.im(i,j) = lut(double(orig.im(i,j))+1);
  end
end
elapsed.allocated = cputime-t;

%%
% looping *without* memory pre-allocation
t = cputime;
for i=1:size(orig.im,1),
  for j=1:size(orig.im,2),
	negative(i,j) = lut(double(orig.im(i,j))+1);
  end
end
elapsed.nonallocated = cputime-t;

%%
% display elapsed times
types = fieldnames(elapsed);
for i=1:size(types,1),
    eval(['elapsed_time = elapsed.',types{i},';']); 
    disp(sprintf('Elapsed time for %s: %f secs',types{i},elapsed_time));
end

%%
showdata(neg,2,'negative');

%% histogram equalization by the function from ipt
ekv2.im = histeq(orig.im);
showdata(ekv2,3,'equalized image by histeq');

