---------Making Gabor-----Matlab------
source:http://faculty.washington.edu/ionefine/html/MakingAGabor.html
-------function summary-----------
LINSPACE Linearly spaced vector.
LINSPACE(X1, X2) generates a row vector of 100 linearly
equally spaced points between X1 and X2.
LINSPACE(X1, X2, N) generates N points between X1 and X2.
For N < 2, LINSPACE returns X2.
Now to create a Gabor (a sinusoid windowed by a Gaussian) becomes a piece of cake. We simply multiply the two-dimensional Gaussian window by the two-dimensional Gabor.
------------------------------------
Contents
Making a Gabor
Here we are going to use some of the things we have learned to make a Gabor filter. These are regularly used for image processing. They are also popular as stimuli among vision scientists.
sd=0.3; % standard deviation
x=linspace(-1,1,100);
y=(1/sqrt(2*pi*sd)).*exp(-.5*((x/sd).^2)); % create a normal distribution
y=y./max(y); % scale it so it goes from 0-1
So y is a 1-d Gaussian with a standard deviation of sd=0.3. It has a minimum value of 0 and a maximum value of 1. We can plot this and see what it looks like.
plotting 1D Gaussian
plot(x, y);
create 2D Gaussian
Now we use the outer product to create a two-dimensional Gaussian filter, with a maximum value of 1 and a minimum of 0
filt=(y'*y);
disp(min(filt(:)));
disp(max(filt(:)));
1.4962e-005
1
view 2D Gaussian
And then we can look what that two dimensional Gaussian looks like. We are going to allow the colormap to take 256 possible values of gray. That means we want filt to vary between 1 and 256.
colormap(gray(256));
newmax=256;
newmin=1;
delta = (newmax-newmin);
scaled_filt = delta*filt + newmin;
image(scaled_filt);
Using the Gaussian filter to filter other images
We can then use this Gaussian window to filter any image we want. Let’s say we want to filter a sinusoidal grating. We actually use much the same set of tricks.
create 1-D sinusoid
sf=6; % spatial freq in cycles per image
y2=sin(x*pi*sf);
y2=scaleif(y2, 0, 1);
plot(x, y2);
ans =
2
Create 1-D sinusoidal grating
This now gives us a 1-dimensional sinusoidal grating with a frequency of 6 cycles per image. We will scale it so it has a minimum value of 0 and a maximum value of 1. If we calculate the outer product of this sinusoid with itself we get a weird checkerboard.
img=(y2'*y2);
colormap(gray(256));
scaled_img = delta*img + newmin;
image(scaled_img);
Create 2-D sinusoid
What we want to do is calculate the outer product of the one-dimensional sinusoid with a vector of ones.
y3=ones(size(y2));
img=(y3'*y2);
colormap(gray(256))
scaled_img = delta*img + newmin;
image(scaled_img)
% Once again, img has a minimum of 0 and a maximum of 1
disp(max(img(:)))
disp(min(img(:)))
1
0
Create a Gabor
Now to create a Gabor (a sinusoid windowed by a Gaussian) becomes a piece of cake. We simply multiply the two-dimensional Gaussian window by the two-dimensional Gabor.
gabor=img.*filt;
scaled_gabor=delta.*gabor+1;
colormap(gray(256))
image(scaled_gabor);
Note that throughout this process we have worked with two versions of the Gaussian and the Gabor. The original versions (filt, img) were scaled between 0 and 1. We did this to keep the nice property that when the Gaussian filter was at its maximum, the brightness of the grating didn’t change, and when the Gaussian filter was at 0 the Gabor was also at 0. But when we used image to look at these matrices we converted them to range between 1-256 so as to match the colormap.
No comments:
Post a Comment