3

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.

roni
  • 1,443
  • 3
  • 28
  • 49
  • 1
    So my help is to use different kind of texture features, e.g. Haralick texture features: http://stackoverflow.com/questions/22113684/texture-recognition-within-a-specific-area-in-the-pic/22129769#22129769. 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: https://github.com/scikit-image/scikit-image/blob/master/skimage/filter/_gabor.py – marol Mar 15 '14 at 08:34
  • thanks for your reply. coincidentally I had already used the Harlick features to measure degree of homogeneity in images. However for this application I intend to use the Gabor filters for texture segmentation. Perhaps u could give me a few pointers regarding this ? – roni Mar 15 '14 at 09:36
  • 1
    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 Mar 15 '14 at 10:48
  • 1
    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 Mar 15 '14 at 23:37
  • 1
    @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 Mar 17 '14 at 14:47
  • Thanks for your concern @teng. Your code is absolutely correct but as I already pointed out, the paper I am referring too considers the value of sigma as = {.0075,.005,.0025}, F={60,90,120} and theta={0,pi/6,pi/4,pi/3,pi/2} and the gabor filter is supposed to work on a 256x256 image. All parameters are working fine except that of sigma. It is so small. Could you help on this matter. If required I can send you the article too. – roni Mar 18 '14 at 07:22
  • @teng kindly could you please provide me some relevant values of the sigma parameter ? I see that in you code you have used sigma=3. Please also provide some other values – roni Mar 18 '14 at 07:42
  • The relevent value/ranges for sigma will be dependent on what your application will be with the filter. You do not need to follow the values in the paper for the sigma, F and theta, unless you are trying to duplicate their results. If that is the case, I would contact the authors directly . If that is not the case, perhaps you should let us know what your application is and the images you are dealing with. – pangyuteng Mar 18 '14 at 13:52
  • I am actually trying to replicate the results of that paper. If you are interested I can send the paper to you. I tried to contact the authors but they are not responding. Basically it uses the gabor transformed image for texture image segmentation by the common Chan Vese image segmentation model. Could you possibly help me in this regard ? – roni Mar 19 '14 at 06:26
  • Active contour is another topic itself. :D I suggest you asking another question here on SO. Namaste... – pangyuteng Mar 19 '14 at 14:04
  • @roni: I am working in active contour and texure image. We are same interesting topic. Please contact to me via email mjohn1282@gmail.com .We can discuss more information above that method. – John Aug 04 '14 at 08:07

1 Answers1

2

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');
pangyuteng
  • 1,749
  • 14
  • 29
  • +1 for clear answer. BTW, I read paper of chan-vese. And I truy to use your code. But the chan-vese paper, he setup F=60:30:120 and sigma=[0.0075 0.005 0.0025]. It is very small compare with your code. Hence, I confuse the value of F and sigma. Could you read the chan-vese paper and talk to me what is value of F and sigma that equivalent with your code. Thanks http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.3145&rep=rep1&type=pdf . – John Aug 05 '14 at 18:08