1 Simple Cells
Ia = imread ('elephant.png');
Ia = im2double(Ia);
v1 = gabor2 (3,0.1,90,0.75,90);
Ia_gabor2 = conv2 (Ia, v1, 'valid');
figure(1), clf,imagesc(Ia_gabor2); axis('equal','tight'), colormap('gray'); colorbar

2 Complex Cells
gA=gabor2(3,0.1,90,0.75,90);
gB=gabor2(3,0.1,90,0.75,0);
IgA=conv2(Ia,gA,'valid');
IgB=conv2(Ia,gB,'valid');
Ia_AB=sqrt((IgA.^2)+(IgB.^2));
figure(2), clf, imagesc(Ia_AB); axis('equal','tight'), colormap('gray'); colorbar;

Code
%% Code
%% Simple cells
Ia = imread ('elephant.png');
Ia = im2double(Ia);
v1 = gabor2 (3,0.1,90,0.75,90);
Ia_gabor2 = conv2 (Ia, v1, 'valid');
figure(1), clf,imagesc(Ia_gabor2); axis('equal','tight'), colormap('gray'); colorbar
Ia_gabor2 (360, 414)
Ia_gabor2 (276, 208)
%% Complex cells
gA=gabor2(3,0.1,90,0.75,90);
gB=gabor2(3,0.1,90,0.75,0);
IgA=conv2(Ia,gA,'valid');
IgB=conv2(Ia,gB,'valid');
Ia_AB=sqrt((IgA.^2)+(IgB.^2));
figure(2), clf, imagesc(Ia_AB); axis('equal','tight'), colormap('gray'); colorbar;
Ia_AB (223, 192)
Ia_AB (395, 240)
%% gabor2 function
function gb=gabor2(sigma,freq,orient,aspect,phase)
%function gb=gabor2(sigma,freq,orient,aspect,phase)
%
% This function produces a numerical approximation to 2D Gabor function.
% Parameters:
% sigma = standard deviation of Gaussian envelope, this in-turn controls the
% size of the result (pixels)
% freq = the frequency of the sin wave (1/pixels)
% orient = orientation of the Gabor from the horizontal (degrees)
% aspect = aspect ratio of Gaussian envelope (1 = circular symmetric envelope,
% lower values produce longer functions)
% phase = the phase of the sin wave (degrees)
sz=fix(7*sigma./max(0.2,aspect));
if mod(sz,2)==0, sz=sz+1; end
[x y]=meshgrid(-fix(sz/2):fix(sz/2),fix(-sz/2):fix(sz/2));
% Rotation
orient=(orient-90)*pi/180;
xDash=x*cos(orient)+y*sin(orient);
yDash=-x*sin(orient)+y*cos(orient);
phase=phase*pi/180;
gb=exp(-.5*((xDash.^2/sigma^2)+(aspect^2*yDash.^2/sigma^2))).*(cos(2*pi*xDash*freq+phase));
%ensure gabor sums to zero (so it is a valid differencing mask)
gb(gb>0)=gb(gb>0)./sum(sum(max(0,gb)));
gb(gb<0)=gb(gb<0)./sum(sum(max(0,-gb)));