MATLAB CODES

Contents

1. gab2d.m

2. postf2d.m

3. featcal.m

4. maxoccur.m

5. ptrnclas.m

6. gabtune4.m

7. gabtune5.m

8. texseg31.m

9. texseg32.m

10. texseg33.m

11. texseg4.m

12. gabor.c


1. gab2d.m

% Function for 2D Gabor Filtering with
% The Parameters as Centre Frequencies (u,v) and
% The Scale (sigmax,sigmay) of the Gabor Filter
% I = Input Image (In Double)
% Io = Output Image (In Double)

function Io = gab2d(I,sigmax,sigmay,u,v)

% Creating the 2D Gabor Filter
m=fix(sigmax);
n=fix(sigmay);
for x=-m:m
for y=-n:n
t=((x*x)/(sigmax*sigmax))+((y*y)/(sigmay*sigmay));
t=(exp(-0.5*t))/(2*pi*sigmax*sigmay);
gfr(m+x+1,n+y+1)=t*cos(2*pi*(u*x+v*y));
gfi(m+x+1,n+y+1)=t*sin(2*pi*(u*x+v*y));
end
end

% Computing the Output Image
Ir=conv2(I,double(gfr),'same');
Ii=conv2(I,double(gfi),'same');
Io=sqrt(Ir.*Ir+Ii.*Ii);

Back to Top


2. postf2d.m

% Function for Post-Filtering of Gabor Filter Output
% I = Input Image (In Double)
% Io = Output Image (In Double)
% sigmax,sigmay : Scales of Post Filter

function Io = postf2d(I,sigmax,sigmay)

% Creating the 2D Post Filters
m=fix(sigmax);
n=fix(sigmay);
for x=-m:m
for y=-n:n
t=((x*x)/(sigmax*sigmax))+((y*y)/(sigmay*sigmay));
t=(exp(-0.5*t))/(2*pi*sigmax*sigmay);
pf(m+x+1,n+y+1)=t;
end
end

% Computing the Output Image
Io=conv2(I,pf,'same');

Back to Top


3. featcal.m

% Function for Calculating the Features of an Image
% I = Image whose Feature is to be Extracted
% (scalec,scaler) = Scales of Sampling Window

function [fm,fs] = featcal(I,scaler,scalec)

[m,n]=size(I);
N=(m-scaler+1)*(n-scalec+1);
fm=0;
fs=0;

for i=1:(m-scaler+1)
for j=1:(n-scalec+1)
tmp=I(i:(i+scaler-1),j:(j+scalec-1));
fm=fm+mean2(tmp);
fs=fs+std2(tmp);
end
end

fm=fm/N;
fs=fs/N;

Back to Top


4. maxoccur.m

% Function for Returning the Maximum Occuring Value in an Array x as dictated by P
% The Function also returns a Number (u) which designates its Uniqueness

function [p,u]=maxocur(x,P)

n=length(x);
m=length(P);
tst=zeros(1,m);

for j=1:m
for i=1:n
if x(i)==P(j)
tst(j)=tst(j)+1;
end
end
end

t=max(tst);
u=-1;
for j=1:m
if tst(j)==t
p=P(j);
u=u+1;
end
end

Back to Top


5. ptrnclas.m

% Classifying the Patterns Based on Maximum Occurence in Classification

% Ic = 3 - dimensional matrix containing filter outputs

% fms = 3 - dimensional feature space clustering centre of means

% P = Pattern Number Array

% scaler = Scale of Pattern Extraction

% scalec = Scale of Pattern Extraction

% Io = Classified and Segmented Image

% It = Classified Image

function [Io,It] = ptrnclas(Ic,fms,P,scaler,scalec)

[m,n,p]=size(Ic);
for i=1:p
Ic1(1:m,1:n,i)=zeros(m,n);
end
Io=zeros(m,n);

% Classification of the Images by the Classifier Blocks
for imnum=1:p
F=fms(1:p,1:2,imnum);
for i=1:(m-scaler(imnum)+1)
for j=1:(n-scalec(imnum)+1)
tmp=squeeze(Ic(i:(i+scaler(imnum)-1),j:(j+scalec(imnum)-1),imnum));
fm=mean2(tmp);
fs=std2(tmp);
for k=1:p
D(k)=sqrt((fm-F(k,1))*(fm-F(k,1))+(fs-F(k,2))*(fs-F(k,2)));
end
for k=1:p
if D(k)==min(D)
break;
end
end
Ic1(i:(i+scaler(imnum)-1),j:(j+scalec(imnum)-1),imnum)=P(k)*ones(scaler(imnum),scalec(imnum));
end
end
end

% Intermediate Classification by using Maximum Occurance Criteria
for i=1:m
for j=1:n
for k=1:p
tmp1(k)=Ic1(i,j,k);
end
[s,u]=maxocur(tmp1,P);
if u==0
Io(i,j)=s;
end
end
end

% Final Classification of the Image Considering Neighbourhood Statistics
for i=2:(m-1)
for j=2:(n-1)
if Io(i,j)==0
tmp1=[Io(i-1,j-1),Io(i-1,j),Io(i-1,j+1),Io(i,j-1),Io(i,j+1),Io(i+1,j-1),Io(i+1,j),Io(i+1,j+1)];
[s,u]=maxocur(tmp1,P);
Io(i,j)=s;
end
end
end
It=Io;

% Creating the Final Segmentation
edg=[-1 0 1];
I1=conv2(Io,edg,'same');
I2=conv2(Io,edg','same');
Iedg=sqrt(I1.*I1+I2.*I2);
Iedg=255*sign(Iedg);
for i=1:m
for j=1:n
if Iedg(i,j)==255
Io(i,j)=255;
end
end
end

Back to Top


6. gabtune4.m

% Programme for Tuning the Gabor Filter Parameters for Maximum Discrimination
% Here we consider the Case of Multiple Gabor Filters. That is, each Gabor Filter
% to be Tuned for each Texture and that to satisfying Maximum Discrimination
% k = Filter Number to be Tuned
close all;
clear;
clc;

% Initialising the parameters
sigmax=[10,7];
sigmay=[10,7];
u=[-0.005660,-0.005182];
v=[-0.005660,-0.005182];
etau=0.0001;
etav=0.0001;

% Reading the Image Files
for i=1:2
s=strcat('tex',int2str(i+1),'.jpg');
Itmp=double(imread(s));
[m(i),n(i)]=size(Itmp);
I(1:m(i),1:n(i),i)=Itmp;
end
Itmp=double(imread('tex5.jpg'));
[m(3),n(3)]=size(Itmp);
I(1:m(3),1:n(3),3)=Itmp;

% Initialising Iteration Parameters
itnum=0;
itstop=5;
itcrit=0;
yprev=0;
uprev=zeros(1,2);
vprev=zeros(1,2);

% Starting the Iteration
while itcrit<itstop

itnum=itnum+1;

% Iterating for Filter Number k
k=1;

% Evaluating the Filtered Images and their Feature Points
for i=1:3
Itmp=squeeze(I(1:m(i),1:n(i),i));
Itmp=gab2d(Itmp,sigmax(k),sigmay(k),u(k),v(k));
Itmp=postf2d(Itmp,sigmax(k),sigmay(k));
[fm(i),fs(i)]=featcal(Itmp,2*sigmax(k)+1,2*sigmay(k)+1);
end

% Computing the Distances of the Feature Points from Each Other
for i=1:3
for j=1:3
D(i,j)=sqrt(((fm(i)-fm(j))*(fm(i)-fm(j)))+((fs(i)-fs(j))*(fs(i)-fs(j))));
end
end

% Evaluating the Objective Function
for i=1:3
D(i,i)=max(max(D));
end
y=-min(min(D));

% Adaptively Adjusting u and v
tmp=u(k);
u(k)=u(k)-etau*((y-yprev)/(u(k)-uprev(k)));
uprev(k)=tmp;
tmp=v(k);
v(k)=v(k)-etav*((y-yprev)/(v(k)-vprev(k)));
vprev(k)=tmp;
yprev=y;

% Evaluating Iteration Criteria
itcrit=abs(yprev);

end

% Displaying the Results
fprintf('\n\n');
fprintf('\nFilter Number %d : u=%f, v=%f\n',k,uprev(k),vprev(k));

Back to Top


7. gabtune5.m

% Proframme for Tuning One Gabor Filter to Distinguish a Bi-Textured Image
close all;
clear;
clc;

% Reading the Image files
I1=imread('tex3.jpg');
I1=double(I1);
I2=imread('tex4.jpg');
I2=double(I2);

% Initialising Gabor Filter Parameters
sigmax=7;
sigmay=7;
u=-1/(4*sigmax+2);
v=1/(4*sigmay+2);
scaler=2*sigmax+1;
scalec=2*sigmay+1;

% Initialising iteration parameters
itcrit=0;
itstop=10;
etau=0.0001;
etav=0.0001;
uprev=0;
vprev=0;
yprev=0;
itnum=0;

% Starting the itearation
while itcrit<=itstop

itnum=itnum+1;
Itmp=gab2d(I1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[fm1,fs1]=featcal(Itmp,scaler,scalec);
Itmp=gab2d(I2,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[fm2,fs2]=featcal(Itmp,scaler,scalec);
y=-norm([(fm1-fm2),(fs1-fs2)]);
tmp=u;
u=u-etau*((y-yprev)/(u-uprev));
uprev=tmp;
tmp=v;
v=v-etav*((y-yprev)/(v-vprev));
vprev=tmp;
yprev=y;
itcrit=abs(y);

end

% Displaying Results
fprintf('\n\nu=%f and v=%f\n\n',uprev,vprev);

Back to Top


8. texseg31.m

% Programme for Segmentation and Classification of a Bi-textured image
% using a single Gabor Filter.
close all;
clear;
clc;

% Reading the Image and the Groundtruth
t1=imread('tex3.jpg');
t1=double(t1);
[m,n]=size(t1);
t2=imread('tex4.jpg');
t2=double(t2);
I1=zeros(m,2*n);
I2=I1;
Io=I1;
P=[50,150];
for i=1:m
for j=1:n
I1(i,j)=t1(i,j);
I2(i,j)=P(1);
end
end
for i=1:m
for j=n+1:2*n
I1(i,j)=t2(i,j-n);
I2(i,j)=P(2);
end
end

% Initialising Gabor Filterbank and Classifier Parameters
sigmax=7;
sigmay=7;
u=6.8663e-010;
v=-6.8663e-010;
scaler=2*sigmax+1;
scalec=2*sigmay+1;
F=zeros(2,2);
Itmp=gab2d(t1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[F(1,1),F(1,2)]=featcal(Itmp,scaler,scalec);
Itmp=gab2d(t2,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[F(2,1),F(2,2)]=featcal(Itmp,scaler,scalec);
clear t1;
clear t2;

% Passing the image through the Filterbank and Classifiers
Itmp=gab2d(I1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[m,n]=size(I1);
for i=1:(m-scaler+1)
for j=1:(n-scalec+1)
tmp=Itmp(i:(i+scaler-1),j:(j+scalec-1));
fm=mean2(tmp);
fs=std2(tmp);
D(1)=norm([(fm-F(1,1)),(fs-F(1,2))]);
D(2)=norm([(fm-F(2,1)),(fs-F(2,2))]);
if D(1)<D(2)
Io(i:(i+scaler-1),j:(j+scalec-1))=P(1)*ones(scaler,scalec);
else
Io(i:(i+scaler-1),j:(j+scalec-1))=P(2)*ones(scaler,scalec);
end
end
end

% Measuring the Classification Accuracy
s=0;
for i=1:m
for j=1:n
if Io(i,j)==I2(i,j)
s=s+1;
end
end
end
s=(100*s)/(m*n);

% Edge Detection and Segmentation of the Classified Image
edg=[-1,0,1];
Ie1=conv2(Io,edg,'same');
Ie2=conv2(Io,edg','same');
Ie=sqrt(Ie1.*Ie1+Ie2.*Ie2);
Ie=255*sign(Ie);
for i=1:m
for j=1:n
if Ie(i,j)==255
Io(i,j)=255;
end
end
end

% Displaying the Input and the Output Image
fprintf('\n\n Classification Accuracy = %f\n\n',s);
I1=uint8(I1);
imshow(I1);
figure;
I2=uint8(I2);
imshow(I2);
figure;
Io=uint8(Io);
imshow(Io);

% Writing the Image Files
imwrite(I1,'im341.jpg');
imwrite(I2,'im341g.jpg');
imwrite(Io,'im341cs.jpg');

Back to Top


9. texseg32.m

% Programme for Segmentation and Classification of a Bi-textured image
% using a single Gabor Filter.
close all;
clear;
clc;

% Reading the Image and the Groundtruth
t1=imread('tex3.jpg');
t1=double(t1);
[m,n]=size(t1);
t2=imread('tex4.jpg');
t2=double(t2);
I1=zeros(m,2*n);
I2=I1;
Io=I1;
P=[50,150];
for i=1:m
for j=1:(n/2)
I1(i,j)=t1(i,j);
I2(i,j)=P(1);
end
end
for i=1:m
for j=(n/2+1):(n/2+n)
I1(i,j)=t2(i,j-(n/2));
I2(i,j)=P(2);
end
end
for i=1:m
for j=(n/2+n+1):(2*n)
I1(i,j)=t1(i,j-n);
I2(i,j)=P(1);
end
end

% Initialising Gabor Filterbank and Classifier Parameters
sigmax=7;
sigmay=7;
u=6.8663e-010;
v=-6.8663e-010;
scaler=2*sigmax+1;
scalec=2*sigmay+1;
F=zeros(2,2);
Itmp=gab2d(t1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[F(1,1),F(1,2)]=featcal(Itmp,scaler,scalec);
Itmp=gab2d(t2,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[F(2,1),F(2,2)]=featcal(Itmp,scaler,scalec);
clear t1;
clear t2;

% Passing the image through the Filterbank and Classifiers
Itmp=gab2d(I1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[m,n]=size(I1);
for i=1:(m-scaler+1)
for j=1:(n-scalec+1)
tmp=Itmp(i:(i+scaler-1),j:(j+scalec-1));
fm=mean2(tmp);
fs=std2(tmp);
D(1)=norm([(fm-F(1,1)),(fs-F(1,2))]);
D(2)=norm([(fm-F(2,1)),(fs-F(2,2))]);
if D(1)<D(2)
Io(i:(i+scaler-1),j:(j+scalec-1))=P(1)*ones(scaler,scalec);
else
Io(i:(i+scaler-1),j:(j+scalec-1))=P(2)*ones(scaler,scalec);
end
end
end

% Measuring the Classification Accuracy
s=0;
for i=1:m
for j=1:n
if Io(i,j)==I2(i,j)
s=s+1;
end
end
end
s=(100*s)/(m*n);

% Edge Detection and Segmentation of the Classified Image
edg=[-1,0,1];
Ie1=conv2(Io,edg,'same');
Ie2=conv2(Io,edg','same');
Ie=sqrt(Ie1.*Ie1+Ie2.*Ie2);
Ie=255*sign(Ie);
for i=1:m
for j=1:n
if Ie(i,j)==255
Io(i,j)=255;
end
end
end

% Displaying the Input and the Output Image
fprintf('\n\n Classification Accuracy = %f\n\n',s);
I1=uint8(I1);
imshow(I1);
figure;
I2=uint8(I2);
imshow(I2);
figure;
Io=uint8(Io);
imshow(Io);

% Writing the Image Files
imwrite(I1,'im342.jpg');
imwrite(I2,'im342g.jpg');
imwrite(Io,'im342cs.jpg');

Back to Top


10. texseg33.m

% Programme for Segmentation and Classification of a Bi-textured image
% using a single Gabor Filter.
close all;
clear;
clc;

% Reading the Image and the Groundtruth
t1=imread('tex2.jpg');
t1=double(t1);
t2=imread('tex3.jpg');
t2=double(t2);
I1=double(imread('im233.jpg'));
I2=double(imread('im233g.jpg'));
P=[50,150];
[m,n]=size(I2);
for i=1:m
for j=1:n
if I2(i,j)<200
I2(i,j)=P(1);
else
I2(i,j)=P(2);
end
end
end
imwrite(uint8(I2),'im233g1.jpg');
Io=zeros(m,n);

% Initialising Gabor Filterbank and Classifier Parameters
sigmax=7;
sigmay=7;
u=-0.005182;
v=-0.005182;
scaler=2*sigmax+1;
scalec=2*sigmay+1;
F=zeros(2,2);
Itmp=gab2d(t1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[F(1,1),F(1,2)]=featcal(Itmp,scaler,scalec);
Itmp=gab2d(t2,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[F(2,1),F(2,2)]=featcal(Itmp,scaler,scalec);
clear t1;
clear t2;

% Passing the image through the Filterbank and Classifiers
Itmp=gab2d(I1,sigmax,sigmay,u,v);
Itmp=postf2d(Itmp,sigmax,sigmay);
[m,n]=size(I1);
for i=1:(m-scaler+1)
for j=1:(n-scalec+1)
tmp=Itmp(i:(i+scaler-1),j:(j+scalec-1));
fm=mean2(tmp);
fs=std2(tmp);
D(1)=norm([(fm-F(1,1)),(fs-F(1,2))]);
D(2)=norm([(fm-F(2,1)),(fs-F(2,2))]);
if D(1)<D(2)
Io(i:(i+scaler-1),j:(j+scalec-1))=P(1)*ones(scaler,scalec);
else
Io(i:(i+scaler-1),j:(j+scalec-1))=P(2)*ones(scaler,scalec);
end
end
end

% Measuring the Classification Accuracy
s=0;
for i=1:m
for j=1:n
if Io(i,j)==I2(i,j)
s=s+1;
end
end
end
s=(100*s)/(m*n);

% Edge Detection and Segmentation of the Classified Image
edg=[-1,0,1];
Ie1=conv2(Io,edg,'same');
Ie2=conv2(Io,edg','same');
Ie=sqrt(Ie1.*Ie1+Ie2.*Ie2);
Ie=255*sign(Ie);
for i=1:m
for j=1:n
if Ie(i,j)==255
Io(i,j)=255;
end
end
end

% Displaying the Input and the Output Image
fprintf('\n\n Classification Accuracy = %f\n\n',s);
I1=uint8(I1);
imshow(I1);
figure;
I2=uint8(I2);
imshow(I2);
figure;
Io=uint8(Io);
imshow(Io);

% Writing the Image Files
imwrite(Io,'im233cs.jpg');

Back to Top


11. texseg4.m

% Programme for Classifying and Segmenting a Tri-Textured Image
clear;
close all;
clc;

% Reading the Image Files
for i=1:2
s=strcat('tex',int2str(i+1),'.jpg');
T(:,:,i)=double(imread(s));
end
T(:,:,3)=double(imread('tex5.jpg'));

[m,n,p]=size(T);
I1=zeros(m,3*n);
I2=I1;
P=[70,140,210];
for i=1:m
for j=1:n
I1(i,j)=T(i,j,1);
I2(i,j)=P(1);
end
end
for i=1:m
for j=1:n
I1(i,j+n)=T(i,j,2);
I2(i,j+n)=P(2);
end
end
for i=1:m
for j=1:n
I1(i,j+2*n)=T(i,j,3);
I2(i,j+2*n)=P(3);
end
end
[m,n]=size(I1);
Io=zeros(m,n);
It=Io;

% Initialising the Filter Bank and Classifier Parameters
sigmax=[10,7];
sigmay=[10,7];
u=[-0.005660,-0.005182];
v=[-0.005660,-0.005182];
scaler=2*sigmax+1;
scalec=2*sigmay+1;
for i=1:2
for imnum=1:3
Itmp=squeeze(T(:,:,imnum));
Itmp=gab2d(Itmp,sigmax(i),sigmay(i),u(i),v(i));
Itmp=postf2d(Itmp,sigmax(i),sigmay(i));
[fms(imnum,1,i),fms(imnum,2,i)]=featcal(Itmp,scaler(i),scalec(i));
end
end
clear T;

% Processing the Images by Filter Bank and Classifier Stages
for i=1:2
Itmp=gab2d(I1,sigmax(i),sigmay(i),u(i),v(i));
Itmp=postf2d(Itmp,sigmax(i),sigmay(i));
Ic(1:m,1:n,i)=Itmp;
end
[Io,It]=ptrnclas(Ic,fms,P,scaler,scalec);

% Measuring the Classification Accuracy
s=0;
for i=1:m
for j=1:n
if It(i,j)==I2(i,j)
s=s+1;
end
end
end
clear It;
s=(100*s)/(m*n);

% Displaying the Results
fprintf('\n\n Classification Accuracy = %f\n\n',s);
imshow(uint8(I1));
figure;
imshow(uint8(I2));
figure;
imshow(uint8(Io));

Back to Top


12. Gabor.c

/* it computes the optimal parameters for gabor filters which are to be used for segmentation of the image. it does thie optimization using genetic algorithm. the GA code used here has been proposed by Dr. Goldberg*/

Click here to view the program

Back to Top