Gaussian and Laplacian Pyramid Representation in Images

Keywords: Gaussian Pyramid, Laplacian Pyramid

In this tutorial I am going to describe an image encoding technique. Here, the local operators of many scales (but identical shape) serve as the basis functions.

In this tutorial, I will describe a practical way to reduce the image dimensions from a Digital Signal Processing point of view. I will also give downloadable MAtlab code for explanations.

The Gaussian Pyramid

The first step in Pyramid Coding is to low-pass filter the original image go to obtain g1. But, first of all lets display the image to see how it looks like (a tutorial on how to use functions in Matlab can be seen here). To do this, download the image from here. So, in Matlab we write the function "readDisplayData.m":

function ourImage2=readDisplayData(filename, width, height)

%open the raw data file
fileData=fopen(filename, 'r');

%read the imange
[matrix1D,count]=fread(fileData);

%define one matrix of needed dimensions
ourImage1=[width, height];

%convert 1D data matrinx into a 2D matrix
for r=1:height
for c=1:width
ourImage1(r,c)=matrix1D((r-1)*width+c);
end
end

ourImage2=flipud(ourImage1);
%display the flipped matrix using graylevels
figure;
colormap(gray(256));
image(ourImage2);
title('the initial image');


save this file as "readDisplayData.m". Now, lets write the Matlab file that calles this function:

%file: read.m
%date: 041604
%action: calls readDisplayData.m, imagePad.m convolver2D.m, gausspyr.m and laplacepyr.m to read,
% pad, display, flter, subsample, compute gaussian and laplacian pyramids for an image


clear all;
close all
name='q1i1_576x432';
width = 576;
height = 432;
border = 2;
level = 6;

x=[1, 4, 6, 4, 1]/16;
y=[1, 4, 6, 4, 1]/16;

%read image q1i1_576x432 (name)
firstImage=readDisplayData(name, width, height);


Save this file as "read.m". Put these two files in the same folder. In Matlab editor, click on "read.m" tag. Press F5 to run the program. What you should see is an image that looks like below.

It can be seen that the image dimensions are 576x432. As we said before, the first step is to low-pass filter this image. To do this we are going to use the filter given by:

x=[1, 4, 6, 4, 1]/16;
y=[1, 4, 6, 4, 1]/16;


for both x and y axes. So, we are going to convolve the image with this filter. To see why are we doing this, lets take a look on a image line in frequency domain. In frequency domain, a line might look like below:

By convolving the image with the filter, the high frequencies are removed. The result is a blurry image. to filter the image, we are going to pad the image first. So, we are going to write another function. Open a new file in Matlab editor and type there:

% DATE: 04/15/04
% FILE: imagePad.m
% DESC: Pad the image with a border of "0" pixels and display the image

function ourImage3=imagePad(ourImage, border, width, height)

%create a (2*border+width,2*border+height) matrix with zero elements
height3=height+2*border;
width3=width+2*border;
ourImage3=zeros(height3, width3);

%create bordered image
for r=1:height3
for c=1:width3
if ((r>border)&(r<(height3-border))&(c>border)&(c<=(width3-border)))
ourImage3(r,c)=ourImage(r-border,c-border);
end
end
end

figure;
colormap(gray(256));
image(ourImage3)
title('the padded image');


Save this file as "imagePad.m". So, now, the "read.m" file has to call this function as well. To do this add the following lines:

%pad this image and display it
secondImage=imagePad(firstImage, border, width, height); title('the padded image');


save this file and press F5 again. The program will display the first image as well as the padded one.

We have to convolve this image with the filter. To do this, we are going to write another function (titled "convolve2D.m"). Open a new file in Matlab editor and type there:

% DATE: 04/15/04
% FILE: convolve2D.m
% DESC: convolves the image (first with xfilter and then with yfilter)

function ourImage5=convolve2D(ourImage, border, xfilter, yfilter, width, height)

% convolve image with 1D xfilter mask
for r=1:height+2*border
for c=1:width
SUM1=0;
%for I=1:5
for J=1:5
%r1=r+I-1;
c1=c+J-1;
SUM1 = SUM1 + (ourImage(r, c1))*(xfilter(1,J));
end
%end

ourImage4(r,c+2)=SUM1;
end
end

[M,N]=size(ourImage4);
% convolve image with 1D yfilter mask
for r=1:M-2*border
for c=1:N
SUM2=0;
for I=1:5
r1=r+I-1;
SUM2 = SUM2 + (ourImage4(r1, c))*(yfilter(1,I));
end

ourImage5(r+2,c)=SUM2;
end
end
display the bordered image figure; colormap(gray(256)); image(ourImage5); title('the result of convolution');


Now, the "read.m" has to call this function. To do this, add the following two lines:

%smoothing the image
thirdImage = convolve2D(secondImage, border, x, y, width, height);


Save "read.m" file and press F5 again. You should see that the program displays three pictures now. the last one is:

It can be seen that the image dimension is reduced (actually is half of the dimension of the initial image). This is level g1 we mentioned about. By calling again these two functions we can reduce the dimensions even more. The following pictures shows this quite clearly:

Now, if the computer use this image to search for some pattern, the amount of computations is tremendously reduced if we reduce the dimmensions (which we actually did). Of course you can rewrite the Matlab functions to call each other (I already did this).

The Laplacian Pyramid

Lets have a look again on the frequency domain figure:

Now, if we subtract the filtered image from the initial one, we end up having the high frequencies ( the edges of the initial image). Still, be carefull because you need to subtract same level images.
I modified a little bit the Matlab such way that the functions call themselves and for the last level, the laplacian pyramid image is computed and displayed.

To see better what we have got, lets reduce the dimensions. It can be seen that we have the edges.

Download the Matlab code from here rather that copy and paste.

Home