【问题标题】:Gabor Filter implementation in Frequency domain频域中的 Gabor 滤波器实现
【发布时间】:2019-02-03 19:35:25
【问题描述】:

Here 我们有 Gabor 滤波器的空间域实现。但是,我需要在频域中实现一个 Gabor 滤波器for performance reasons.

我找到了Frequency Domain equation of Gabor Filter

我实际上怀疑这个公式的正确性和/或适用性。

源代码

所以,我实现了以下内容:

public partial class GaborFfftForm : Form
{
    private double Gabor(double u, double v, double f0, double theta, double a, double b)
    {
        double rad = Math.PI / 180 * theta;
        double uDash = u * Math.Cos(rad) + v * Math.Sin(rad);
        double vDash = (-1) * u * Math.Sin(rad) + v * Math.Cos(rad);

        return Math.Exp((-1) * Math.PI * Math.PI * ((uDash - f0) / (a * a)) + (vDash / (b * b)));
    }

    public Array2d<Complex> GaborKernelFft(int sizeX, int sizeY, double f0, double theta, double a, double b)
    {
        int halfX = sizeX / 2;
        int halfY = sizeY / 2;

        Array2d<Complex> kernel = new Array2d<Complex>(sizeX, sizeY);

        for (int u = -halfX; u < halfX; u++)
        {
            for (int v = -halfY; v < halfY; v++)
            {
                double g = Gabor(u, v, f0, theta, a, b);

                kernel[u + halfX, v + halfY] = new Complex(g, 0);
            }
        }

        return kernel;
    }

    public GaborFfftForm()
    {
        InitializeComponent();

        Bitmap image = DataConverter2d.ReadGray(StandardImage.LenaGray);
        Array2d<double> dImage = DataConverter2d.ToDouble(image);

        int newWidth = Tools.ToNextPowerOfTwo(dImage.Width) * 2;
        int newHeight = Tools.ToNextPowerOfTwo(dImage.Height) * 2;

        double u0 = 0.2;
        double v0 = 0.2;
        double alpha = 10;//1.5;
        double beta = alpha;

        Array2d<Complex> kernel2d = GaborKernelFft(newWidth, newHeight, u0, v0, alpha, beta);

        dImage.PadTo(newWidth, newHeight);
        Array2d<Complex> cImage = DataConverter2d.ToComplex(dImage);
        Array2d<Complex> fImage = FourierTransform.ForwardFft(cImage);

        // FFT convolution .................................................
        Array2d<Complex> fOutput = new Array2d<Complex>(newWidth, newHeight);
        for (int x = 0; x < newWidth; x++)
        {
            for (int y = 0; y < newHeight; y++)
            {
                fOutput[x, y] = fImage[x, y] * kernel2d[x, y];
            }
        }

        Array2d<Complex> cOutput = FourierTransform.InverseFft(fOutput);
        Array2d<double> dOutput = Rescale2d.Rescale(DataConverter2d.ToDouble(cOutput));

        //dOutput.CropBy((newWidth-image.Width)/2, (newHeight - image.Height)/2);

        Bitmap output = DataConverter2d.ToBitmap(dOutput, image.PixelFormat);

        Array2d<Complex> cKernel = FourierTransform.InverseFft(kernel2d);
        cKernel = FourierTransform.RemoveFFTShift(cKernel);
        Array2d<double> dKernel = DataConverter2d.ToDouble(cKernel);
        Array2d<double> dRescaledKernel = Rescale2d.Rescale(dKernel);
        Bitmap kernel = DataConverter2d.ToBitmap(dRescaledKernel, image.PixelFormat);

        pictureBox1.Image = image;
        pictureBox2.Image = kernel;
        pictureBox3.Image = output;
    }
}

此时只关注算法步骤。

我在频域中生成了一个 Gabor 核。由于内核已经在频域中,因此我没有对其应用 FFT,而图像是 FFT-ed。然后,我将内核和图像相乘以实现 FFT-Convolution。然后像往常一样对它们进行逆 FFT 并转换回位图。

输出

  1. 内核看起来没问题。但是,过滤器输出看起来不是很有希望(或者,是吗?)。
  2. 方向 (theta) 对内核没有任何影响。
  3. 计算/公式在值变化时经常出现被零除异常。

我该如何解决这些问题?

哦,还有,

  • 参数αβ代表什么?
  • f0的适当值应该是多少?

更新:

我根据@Cris Luoengo的回答修改了我的代码。

    private double Gabor(double u, double v, double u0, double v0, double a, double b)
    {
        double p = (-2) * Math.PI * Math.PI;
        double q = (u-u0)/(a*a);
        double r = (v - v0) / (b * b);

        return Math.Exp(p * (q + r));
    }

    public Array2d<Complex> GaborKernelFft(int sizeX, int sizeY, double u0, double v0, double a, double b)
    {
        double xx = sizeX;
        double yy = sizeY;
        double halfX = (xx - 1) / xx;
        double halfY = (yy - 1) / yy;

        Array2d<Complex> kernel = new Array2d<Complex>(sizeX, sizeY);

        for (double u = 0; u <= halfX; u += 0.1)
        {
            for (double v = 0; v <= halfY; v += 0.1)
            {
                double g = Gabor(u, v, u0, v0, a, b);

                int x = (int)(u * 10);
                int y = (int)(v * 10);

                kernel[x,y] = new Complex(g, 0);
            }
        }

        return kernel;
    }

在哪里,

        double u0 = 0.2;
        double v0 = 0.2;
        double alpha = 10;//1.5;
        double beta = alpha;

我不确定这是否是一个好的输出。

【问题讨论】:

  • 中间图是您在频域中的 Gabor 滤波器吗?它应该看起来像一个移位的高斯(即远离原点,即左上角的像素)。在空间域中,它将具有复数值。过滤后的结果也是如此。你显示震级了吗?
  • 另外,出于性能原因,您应该在空间域中实现递归算法:researchgate.net/publication/3318442_Recursive_Gabor_filtering——这比 FFT 快得多。
  • @CrisLuengo,Is the middle plot your Gabor filter in the frequency domain? --- Did you display the magnitude? ---
  • f0= sizeX/4 左右开头。这应该会给你一个介于 0 和奈奎斯特频率之间的高斯中间值。 Alpha 和 beta 是高斯的 sigmas,它们越小,您对频率的选择性就越高,而在空间上的选择性越低。如果 sizeXsizeY 为 256,则您的 10 和 15 相当大。没关系,但如果结果仍然不佳,请尝试使用较小的值。
  • Gabor 是一种自适应滤波器,就像高斯、FFT 和许多其他滤波器一样。您将其应用于每个图像行,然后在结果上,在每个图像列上。在这种情况下,separable 意味着您可以将空间域滤波器写为两个 1D 滤波器的外积。

标签: c# image-processing gabor-filter


【解决方案1】:

您找到的 Gabor 过滤器的方程式中似乎有错字。 Gabor 滤波器是频域中的平移高斯滤波器。因此,它需要在指数中有

链接中的公式(2)似乎更合理,but still misses a 2

exp( -2(πσ)² (u-f₀)² )

这是一维情况,这是我们要在θ方向上使用的滤波器。我们现在在垂直方向v 上乘以非移位高斯。我将αβ 设置为两个sigma 的倒数:

exp( -2(π/α)² (u-f₀)² ) exp( -2(π/β)² v² ) = exp( -2π²((u-f₀)/α)² + -2π²(v/β)² )

你应该用uv在θ上旋转来实现上述方程,就像你已经做的那样。

另外,uv 应该从 -0.5 到 0.5,而不是从 -sizeX/2sizeX/2。这是假设您的 FFT 将原点设置在图像中间,这并不常见。通常,FFT 算法将原点设置在图像的一角。所以你可能应该让你的uv 从0 运行到(sizeX-1)/sizeX

使用上述更正的实现,您应该将 f₀ 设置为介于 0 和 0.5 之间(尝试从 0.2 开始),并且 αβ 应该足够小,以至于高斯无法达到0 频率(您希望过滤器在此处为 0)

在频域中,您的滤波器看起来像远离原点的旋转高斯。

在空间域中,您的过滤器的幅度应该再次看起来像高斯。虚构的组件应如下所示(图片链接到我在其上找到的 Wikipedia 页面):

(即它在 θ 方向是反对称的(奇数)),可能有更多的波瓣,具体取决于 αβf₀。实部应该相似但对称(偶数),最大值在中间。请注意,在 IFFT 之后,您可能需要将原点从左上角移动到图像的中间(谷歌“fftshift”)。


请注意,如果您将αβ 设置为相等,则u-v 平面的旋转无关紧要。在这种情况下,您可以使用笛卡尔坐标而不是极坐标来定义频率。也就是说,不是将 f₀ 和 θ 定义为参数,而是定义 u₀ 和 v₀。然后在指数中将 u-f₀ 替换为 u-u₀,将 v 替换为 v-v₀。


问题编辑后的代码又错过了方块。我会编写如下代码:

private double Gabor(double u, double v, double u0, double v0, double a, double b)
{
    double p = (-2) * Math.PI * Math.PI;
    double q = (u-u0)/a;
    double r = (v - v0)/b;

    return Math.Exp(p * (q*q + r*r));
}

public Array2d<Complex> GaborKernelFft(int sizeX, int sizeY, double u0, double v0, double a, double b)
{
    double halfX = sizeX / 2;
    double halfY = sizeY / 2;

    Array2d<Complex> kernel = new Array2d<Complex>(sizeX, sizeY);

    for (double y = 0; y < sizeY; y++)
    {
        double v = y / sizeY;
        // v -= HalfY;  // whether this is necessary or not depends on your FFT
        for (double x = 0; x < sizeX; x++)
        {
            double u = x / sizeX;
            // u -= HalfX;  // whether this is necessary or not depends on your FFT
            double g = Gabor(u, v, u0, v0, a, b);

            kernel[(int)x, (int)y] = new Complex(g, 0);
        }
    }

    return kernel;
}

【讨论】:

  • 你已经完全删除了 sin(θ) 和 cos(θ) ?你的公式中没有三角函数?
  • @anonymous:我简化了。将此答案中的uv 视为代码中的uDashvDash。也就是说,在使用我提供的方程式之前先旋转 u-v 轴。
  • 也就是说,我一直使用α==β,这样就不需要轮换了,一切都变得更简单了。然后,不是将f₀θ 定义为参数,而是定义u₀v₀。然后在指数中将u-f₀ 替换为u-u₀,将v 替换为v-v₀。也就是说,频率是在笛卡尔坐标而不是极坐标中定义的。
  • 这个内核似乎无法旋转。
  • 旋转来自选择u₀v₀
猜你喜欢
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 2023-03-17
相关资源
最近更新 更多