
echo on

% 
%
% most basic intro to matlab for image processing

%
% download einstein.pgm here, and change
% below line to get the path correct.
%
im = pgmread('einstein.pgm');
%
% pgmread is not a standard matlab procedure.  If you don't it, do a
% google search for    pgmread matlab, download the .m file and put it
% in your matlab directory...
%
% matlab DOES include a procedure called imread, which can read a number
% of standard image file types... like jpeg, bmp, gif etc.  To get a list
% of all of them, you can type
%
help imread

pause
%
% The ; at the end of a line tells matlab not to print out the result,
% you can always leave it out, and then you will get, on your screen, a
% stream of numbers, (the print out of the image array).
%
imagesc(im);
%
% imagesc( 2Darray ) displays the image, with the pixel intensities
% linearly scaled to take up the entire range.
%
% This probably made a strangely colored picture... 
%
colormap gray
pause
%
% Tells matlab to use a colormap where array value is encoded by pixel
% brightness 
%
%
%
% Ok, so now lets try some stuff....
%
% What is an image convolution?
% We defined it in class, For each pixel location, (1) Smack the filter
% down on top of the image at that location, and (2) add up the product
% of the stacked image pixel and filter pixel values.
%
% Before we start, we need to define a filter...
%
% Let's do: 
%
F = [0.25 0.5 0.25];
%
%
% size(A) returns and array that contains the size of a matrix.
% zeros([x y]) makes an r x c array of zeros
%
imOut = zeros(size(im));
%

%for ix = 1 : size(im,1) - size(F,1) + 1;
%  for iy = 1 : size(im,2) - size(F,2) + 1;
%    tmp = 0;
%    for ij = 1:size(F,1);
%      for ik = 1:size(F,2);
%	tmp = tmp + im(ix + ij - 1 ,iy + ik - 1) .* F(ij,ik);
%      end
%    end
%    imOut(ix,iy) = tmp;
%  end
%end
echo off
for ix = 1 : size(im,1) - size(F,1) + 1;
  for iy = 1 : size(im,2) - size(F,2) + 1;
    tmp = 0;
    for ij = 1:size(F,1);
      for ik = 1:size(F,2);
	tmp = tmp + im(ix + ij - 1 ,iy + ik - 1) .* F(ij,ik);
      end
    end
    imOut(ix,iy) = tmp;
  end
end
echo on

%
% So what do you think that convolving an image with the filter [0.25 0.5 0.25]
% will do?
imagesc(imOut);

pause
%
% (?) Where is this filter centered? or, if you smack the filter on the
% image, where, relative to that filter, is the result stored in the
% output array?
%
% This is kinda slow.  
%  
% (?) How slow?... as a function of the image size and the filter size?
%
% Happily, matlab already has a routine to perform convolutions, and this
% routine is optimized to be faster than if we use for loops.  In
% general, in matlab, try to get it to do all matrix operations and
% iterate as rarely as possible...

pause
imOut2 = conv2(im,F);

% we can compare the two images:

subplot(1,2,1); imagesc(im);
subplot(1,2,2); imagesc(imOut2);

% and they are almost identical, except that matlab puts the output at
% the center of the filter position, and assumes the image is surrounded
% by zeros, and computes the filter reponse for anywhere that the filter
% is centered on a valid image pixel.
%
% This convolution routine has several options about how to deal with the
% boundary conditions.
%

help conv2

pause
% derivative filter 
F = [-1 0 1];

imOut3 = conv2(im,F);

% we can again compare the two images:

subplot(1,2,1); imagesc(im);
subplot(1,2,2); imagesc(imOut3);
pause

% for more information about the image toolbox look at the help
help images

% help images

pause
% 
edgedemo 

echo off







