-2

According to this Quora answer, Gabor filter is a frequency domain filter. And, here is an implementation of Gabor filter which uses imfilter() to achieve the filtering, which means that

imfilter() works in the frequency domain.

Now, let us look at Source Code #1.

If I replace

I_ffted_shifted_filtered = I_ffted_shifted.*Kernel;

with

I_filtered = imfilter(I, Kernel);

as the following

function [out1, out2] = butterworth_lpf_imfilter(I, Dl, n)

    Kernel = butter_lp_kernel(I, Dl, n);

    I_filtered = imfilter(I, Kernel);

    out1 = ifftshow(ifft2(I_filtered));

    out2 = ifft2(ifftshift(I_filtered));
end

we don't obtain the expected output,

enter image description here

Why doesn't this work? What is the problem in my code?


Source Code #1

main.m

clear_all();
I = gray_imread('cameraman.png');
Dl = 10;
n = 1;
[J, K] = butterworth_lpf(I, Dl, n);    
imshowpair(I, J, 'montage');

butterworth_lpf.m

function [out1, out2] = butterworth_lpf(I, Dl, n)    
    Kernel = butter_lp_kernel(I, Dl, n);        
    I_ffted_shifted = fftshift(fft2(I));

    I_ffted_shifted_filtered = I_ffted_shifted.*Kernel;

    out1 = ifftshow(ifft2(I_ffted_shifted_filtered));        
    out2 = ifft2(ifftshift(I_ffted_shifted_filtered));
end

butter_lp_kernel.m

function k = butter_lp_kernel(I, Dl, n) 
    Height = size(I,1); 
    Width = size(I,2);                
    [u, v] = meshgrid( ...
                    -floor(Width/2) :floor(Width-1)/2, ...
                    -floor(Height/2): floor(Height-1)/2 ...
                 );         
    k = butter_lp_f(u, v, Dl, n);    

function f = butter_lp_f(u, v, Dl, n)
    uv = u.^2+v.^2;
    Duv = sqrt(uv);
    frac = Duv./Dl;
    denom = frac.^(2*n);
    f = 1./(1.+denom);

Output

enter image description here

Alex Taylor
  • 1,402
  • 1
  • 9
  • 15
user366312
  • 16,949
  • 65
  • 235
  • 452
  • 3
    No? You replace the whole code by imfilter. The whole idea of imfilter is that you dont need to think about frequency domain. It basically does what you wrote there, plainly and simply. Also, why would you want an aternative? the code you have works – Ander Biguri Jul 25 '17 at 14:48
  • 2
    Note that your proposition of changing line 8 by `imfilter` would mean that the only thing that `imfilter` does is elmentwise multiplication. – Ander Biguri Jul 25 '17 at 14:55
  • 2
    I love it that you're asking "can I change A to B" instead of actually trying it to see what happens. If it doesn't work, you can try to find an alternative and ask a more appropriate question. – Andras Deak -- Слава Україні Jul 25 '17 at 17:01
  • `imfilter` efficiently performs filtering **in the spatial domain**. You cannot fundamentally use `imfilter` in the frequency domain because all you have to do is perform element-wise multiplication. You filter in the frequency domain to avoid having to do the operations in spatial domain. What you are proposing is trying to filter in the frequency domain with spatial domain techniques which makes no sense. It's like saying you want to replace the engine of your Ferrari with one from a Pinto. Though the intent is to save on gas, it screws everything else up. – rayryeng Jul 26 '17 at 03:53
  • Your edit is misleading. The Gabor filter definitions that were specified in the code you linked are **spatial-domain** filters, hence the use of `imfilter`. You may take the FFT for it to become a frequency-domain version of the filter, and then you can actually frequency domain filtering. https://en.wikipedia.org/wiki/Gabor_filter. The filter by itself without applying any transformations to it absolutely does not function as filtering in the frequency domain. Please review your signal processing concepts before making other assertions. – rayryeng Jul 26 '17 at 13:17
  • @rayryeng, what is my fault here? That Quora answer says that Gabor Filter is a frequency domain filter. – user366312 Jul 26 '17 at 13:20
  • You're not at fault. I'm just a bit aggravated that you are implying that one point is true just because another point is. They are both unrelated. Even if the Gabor filter is a frequency domain filter, the fact that `imfilter` is or can be a frequency-domain filtering mechanism is not correct. It implements spatial filtering. When you open up the source of `imfilter`, you will see more or less a decomposition of pixel neighbourhoods and applying the filtering on each neighbourhood. This is not how frequency domain filtering is performed. – rayryeng Jul 26 '17 at 13:29
  • A gabor filter is LTI and can be presented in either the spatial domain or the frequency domain. I have no idea what it means to state that a Gabor filter is a "frequency domain filter". Specific implementations of the Gabor filter may be frequency domain implementations. – Alex Taylor Jul 27 '17 at 16:23
  • However, because the kernel widths of Gabor filters can get quite large depending on the frequency you are trying to isolate, it is common to implement Gabor filters in the frequency domain for speed purposes. This is what the implementation of imgaborfilt does in MATLAB. – Alex Taylor Jul 27 '17 at 16:24

1 Answers1

2

imfilter takes a spatial domain representation of an image A and a spatial domain kernel h, and returns a spatial domain image, B. Whatever domain or algorithm is used to compute B is an implementation detail. That said, imfilter uses spatial convolution to compute B.

As you can see in the comments above, a Gabor filter is not a "frequency domain filter". A Gabor filter is LTI and as such the filtering operation can be implemented in either domain. This should make sense, given that the "gaussian modulated with a sinusoid" waveforms that are frequently shown in the literature when discussing Gabor filter banks are in the spatial domain.

Example of spatial domain Gabor filter kernels

It happens that in the spatial domain, Gabor filter kernels can get large and are non-separable in the general case unless theta is a multiple of 90 degrees unless you want to use approximate techniques. So, it is common to use a frequency domain implementation of Gabor filtering for speed purposes. This is what imgaborfilt does in IPT.

If you have IPT, I recommend looking at the code in gabor and imgaborfilt for more information.

https://www.mathworks.com/help/images/ref/imgaborfilt.html

https://www.mathworks.com/help/images/ref/gabor.html

In the implementation you are using, they are using a frequency domain implementation. If you want to use imfilter, you would have to pass in the equivalent spatial domain representation of the Gabor filter. It does not make sense to pass a frequency domain representation of the Gabor filter to imfilter as you are currently doing. That is not a Gabor filtering operation.

Alex Taylor
  • 1,402
  • 1
  • 9
  • 15
  • Great answer. We tried to tell the OP this in our comments above but it requires a comprehensive answer like this for it to sink in. – rayryeng Jul 27 '17 at 23:03
  • Thanks. There was a lot packed into this question so thought it might be worth an answer even though all the comments were accurate and helpful. – Alex Taylor Jul 28 '17 at 13:51