3
votes

I am trying to implement a gabor filter for use in textured image segmentation. I am doing this in MATLAB and consulting the concepts from paper - A level set and Gabor-based Active Contour Algorithm for Segmenting Textured Images

I am highlighing the relevant parts below: The 2D gabor function is given as

enter image description here

where

enter image description here

The frequency of the span-limited sinusoidal grating is given by F and its orientation is specified by Theta. Sigma is the scale parameter. This filter is to be used on an image and as the gabor filter consists of an imaginary component, the Gabor transform is obtained as shown below.

enter image description here

where, GR and GI are the real and imaginary parts obtained by convoluting it with an image u0. I need to code this part in MATLAB and generate the Gabor transformed image for the different values of theta, F and sigma

My code

clc;clear all;close all;
sigma=.0075;
m_size=7;
theta=pi/4;
F=60;
[real_g,im_g]=gabor(m_size,sigma,F,theta);

//My Gabor function
function [real_g,im_g] = gabor(m_size,sigma,F,theta)
[x,y]=meshgrid(1:m_size,1:m_size);
real_g = zeros(m_size);
im_g = zeros(m_size);
g_sigma = zeros(m_size);
for i=1:size(x,1)
    for j=1:size(y,1)
        g_sigma(i,j) = (1./(2*pi*sigma^2)).*exp(((-1).*(i^2+j^2))./(2*sigma^2));
        real_g(i,j) = g_sigma(i,j).*cos((2*pi*F).*(i.*cos(theta)+j.*sin(theta)));
        im_g(i,j) = g_sigma(i,j).*sin((2*pi*F).*(i.*cos(theta)+j.*sin(theta)));
    end
end

My output

>> real_g

real_g =

     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0

>> im_g

im_g =

     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0
     0     0     0     0     0     0     0

My gabor filter is completely wrong. Please could you guys help me to construct the correct gabor filter? Please note that the data for the parameters and the formula is taken from the paper I already referred.

Any help will be greatly appreciated. PS If anybody needs the paper I can mail it too. Thanks.

1
So my help is to use different kind of texture features, e.g. Haralick texture features: stackoverflow.com/questions/22113684/…. I have used Gabor features in scikit-image in python and it of course it worked but it turned out it is not straight-to-use kind of feature. If you having problems with implementation you can look here: github.com/scikit-image/scikit-image/blob/master/skimage/filter/…marol
I've used Gabor features in determining orientation of wooden straws and they seemed to be not useful features. If you have problem with implementation in matlab, check scikit-image source I've referenced.marol
the gaussian kernel usually would span from -x:x instead of 1:x. plus the sigma is rather smaller, thus the 'real_g' and 'im_g' are mostly 0s.pangyuteng
@teng thanks for the info broroni
@roni, as you have not accepted the solution provided, I'm guessing you are still having trouble with this problem. Please let us know more info so that we can help you answer your question. :)pangyuteng

1 Answers

2
votes

Hopefully the below codes would be of some use to what you are working on. It demonstrate how to transform an image using your Gabor filter with varying thetas (as shown in the images). Cheers.

images with varying theta

% get image
u0=double(imread('cameraman.tif'));

% initialize parameters
sigma = 3;
m_size = 7;
F = 1;
m_size_halfed = round((m_size-1)/2);

% make up some thetas
thetas=0:pi/5:pi;

% loop through all thetas
for i = 1:numel(thetas)    
theta = thetas(i);

% setup the "gabor transform"
[x,y]=meshgrid(-m_size_halfed:m_size_halfed,-m_size_halfed:m_size_halfed);
g_sigma = (1./(2*pi*sigma^2)).*exp(((-1).*(x.^2+y.^2))./(2*sigma.^2));
real_g = g_sigma.*cos((2*pi*F).*(x.*cos(theta)+y.*sin(theta)));
im_g = g_sigma.*sin((2*pi*F).*(x.*cos(theta)+y.*sin(theta)));

% perform Gabor transform
u_0sft=sqrt(conv2(u0,real_g,'same').^2+conv2(u0,im_g,'same').^2);

subplot(1,numel(thetas)+1,i+1)
imagesc(u_0sft);
colormap('gray'); axis image; axis off;
title(sprintf('theta:%1.1f',theta));

end

% visualize image
subplot(1,numel(thetas)+1,1)
imagesc(u0);
colormap('gray'); axis image; axis off;
title('original');