I want to implement gabor filter in java. I got gabor filter code in Matlab. i tried to convert it into java but it had some mistakes. pls check the code and fix the errors. i post both the matlab code (which i got) and java code which i had tried.
MATLAB code
function [op]= gabor1(I)
I=imread('C:\Documents and Settings\Administrator\My Documents\MATLAB\cat.jpg');
im=rgb2gray(I);
imshow(I);
alpha=0.707;
count=1;
omega=2;
t=(7*pi)/4;
double h;
gab=cell(6,6);
op=[];
for x=1:1:4
for y=1:1:6
inter1=(x^2+y^2)/2;
inter2=power(((-1)*alpha),(2*omega));
inter3=exp(inter1*inter2);
inter4=(x*cos(t))+(y*sin(t));
inter5=omega*pi*(alpha^omega);
inter6=exp(inter4*inter5);
gab{x,y}=inter6*inter3;
disp(gab{x,y});
count=count+1;
end;
end;
m=[];
u=[];
del=[];
k=0;
for x=1:1:4
for y=1:1:6
m{x,y}=conv2(gab{x,y},im);
[r,c]=size(m{x,y});
H = fspecial('gaussian',[r,c],1);
glpf{x,y} = imfilter(m{x,y},H,'replicate');
% MEAN VALUE
u{x,y}=mean2(glpf{x,y});
%STADNDARD DEVIATION
del{x,y} = std2(glpf{x,y});
%op=[op u{x,y} del{x,y}];
op=[op u{x,y}];
disp(op);
end
end
JAVA code
package texturefeature;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ColorModel;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GaborFilter extends Panel{
BufferedImage destImage=null;
public BufferedImage GetImage()
{
BufferedImage image=null;
try
{
System.out.println("enter image name");
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String imageName;
imageName = bf.readLine();
File input=new File(imageName);
image=ImageIO.read(input);
System.out.println("image loaded successfully");
}
catch(Exception e)
{
System.out.println(e);
}
return image;
}
public void texture(double teta) throws IOException
{
BufferedImage sourceImage=null;
ColorModel srcCM=null;
sourceImage=GetImage();
srcCM=sourceImage.getColorModel();
//dstCM=srcCM;
double t;
t=teta;
destImage= new BufferedImage(sourceImage.getWidth(),sourceImage.getHeight(),BufferedImage.TYPE_3BYTE_BGR);
System.out.println(srcCM.getColorSpace().getType());
//srcCM.createCompatibleWritableRaster(sourceImage.getWidth(),sourceImage.getHeight());
double a=0.7071;
int i=0;
float[] sharpen=new float[24];
double w=2;
double inter1=0,inter2=0,inter3=0;
double inter4=0,inter5=0,inter6=0;
double finalAns=0;
inter2=Math.pow(((-1)*a), (2*w));
// System.out.println(inter2);
inter5=w*3.14*Math.pow(a, w);
for(int x=1;x<5;x++)
{
for(int y=1;y<7;y++)
{
inter1= (Math.pow(x, 2) + Math.pow(y, 2))/2;
// System.out.println(inter1);
inter3=Math.exp(inter1*inter2);
// System.out.println(inter3);
inter4 = (x*Math.cos(t))+(y*Math.sin(t));
inter6=Math.exp(inter4*inter5);
finalAns=inter6*inter3;
System.out.println(finalAns);
sharpen[i]=(float)finalAns;
i++;
}
}
//System.out.print("kernel is");
Kernel kernel = new Kernel(4,6,sharpen);
BufferedImageOp sharpenFilter = new ConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
destImage=sharpenFilter.filter(sourceImage,destImage );
System.out.println("convolution finished");
}
public GaborFilter() throws IOException
{
texture(5.495);
}
@Override
public void paint(Graphics g)
{
g.drawImage(destImage, 0, 0, null);
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
//GaborFilter gf =new GaborFilter();
JFrame frame=new JFrame("display image");
Panel panel = new GaborFilter();
frame.getContentPane().add(panel);
frame.setSize(500,500);
frame.setVisible(true);
}
}