% Example of downsampling an image by 2x2 blocks
%
% Samuli Siltanen November 2012

% Create example image of even dimensions
im = phantom(128);

figure(1)
clf
imagesc(im)
colormap gray
axis equal
axis off

% Record the size of the image
[row,col] = size(im);

% Drop the image to vertical vector
im2 = im(:);

% Reshape
im2 = reshape(im2,2,row*col/2);

% Sum 
im2 = sum(im2);

% Reshape to situation, where rows have been downsampled
im2 = im2(:);
im2 = reshape(im2,row/2,col);

% Now it remains to repeat the above for the transpose
im3 = im2.';

% Record the size of the image
[row,col] = size(im3);

% Drop the image to vertical vector
im3 = im3(:);

% Reshape
im3 = reshape(im3,2,row*col/2);

% Sum 
im3 = sum(im3);

% Reshape to situation, where rows have been downsampled
im3 = im3(:);
im3 = reshape(im3,row/2,col);

% Transpose back
im4 = im3.';
im4 = im4/4;



figure(2)
clf
imagesc(im4)
colormap gray
axis equal
axis off



