频域中的 Gabor 滤波器实现

2023-12-11

Here我们有 Gabor 滤波器的空间域实现。但是,我需要在频域中实现 Gabor 滤波器出于性能原因。

我已经找到了Gabor滤波器的频域方程:

enter image description here

I am actually in doubt about the correctness and/or applicability of this formula.

源代码

所以,我实施了以下措施:

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;
    }
}

Just concentrate on the algorithmic steps at this time.

我已经在频域中生成了 Gabor 核。由于内核已经在频域中,因此我没有对其应用 FFT,而图像是经过 FFT 处理的。然后,我将内核与图像相乘以实现 FFT 卷积。然后它们像往常一样被逆 FFT 并转换回位图。

Output

enter image description here

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

我该如何解决这些问题?

哦,还有,

  • 参数是什么α, β, 代表?
  • what should be the appropriate value of f0?

Update:

我已经根据修改了我的代码@克里斯·罗恩戈的回答。

    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;
    }

where,

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

enter image description here

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


您发现的 Gabor 滤波器的方程似乎存在拼写错误。 Gabor 滤波器是频域中的平移高斯滤波器。因此,它需要有 and 在指数中。

您链接中的方程(2)似乎更合理,但还是错过了2:

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

这是一维情况,这是我们要在 θ 方向使用的滤波器。我们现在在垂直方向相乘,v,具有非平移高斯分布。我设置α and β是两个西格玛的倒数:

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

您应该使用以下命令来实现上面的等式u and v旋转 θ,就像你已经做的那样。

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

通过上述更正的实现,您应该设置f₀介于 0 和 0.5 之间(尝试从 0.2 开始),并且α and β应足够小,以便高斯不会达到 0 频率(您希望滤波器在那里为 0)

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

在空间域中,滤波器的幅度应该再次看起来像高斯分布。假想部分应该如下所示(图片链接到我在其中找到的维基百科页面):

Gabor, real part

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


请注意,如果您设置α and β为相等,则旋转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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

频域中的 Gabor 滤波器实现 的相关文章

  • 如何在 Cassandra 中存储无符号整数?

    我通过 Datastax 驱动程序在 Cassandra 中存储一些数据 并且需要存储无符号 16 位和 32 位整数 对于无符号 16 位整数 我可以轻松地将它们存储为有符号 32 位整数 并根据需要进行转换 然而 对于无符号 64 位整
  • C++11 删除重写方法

    Preface 这是一个关于最佳实践的问题 涉及 C 11 中引入的删除运算符的新含义 当应用于覆盖继承父类的虚拟方法的子类时 背景 根据标准 引用的第一个用例是明确禁止调用某些类型的函数 否则转换将是隐式的 例如最新版本第 8 4 3 节
  • 如何在 C# 中打开 Internet Explorer 属性窗口

    我正在开发一个 Windows 应用程序 我必须向用户提供一种通过打开 IE 设置窗口来更改代理设置的方法 Google Chrome 使用相同的方法 当您尝试更改 Chrome 中的代理设置时 它将打开 Internet Explorer
  • free 和 malloc 在 C 中如何工作?

    我试图弄清楚如果我尝试 从中间 释放指针会发生什么 例如 看下面的代码 char ptr char malloc 10 sizeof char for char i 0 i lt 10 i ptr i i 10 ptr ptr ptr pt
  • 从经典 ASP 调用 .Net C# DLL 方法

    我正在开发一个经典的 asp 项目 该项目需要将字符串发送到 DLL DLL 会将其序列化并发送到 Zebra 热敏打印机 我已经构建了我的 DLL 并使用它注册了regasm其次是 代码库这使得 IIS 能够识别它 虽然我可以设置我的对象
  • WPF 数据绑定到复合类模式?

    我是第一次尝试 WPF 并且正在努力解决如何将控件绑定到使用其他对象的组合构建的类 例如 如果我有一个由两个单独的类组成的类 Comp 为了清楚起见 请注意省略的各种元素 class One int first int second cla
  • C# - 当代表执行异步任务时,我仍然需要 System.Threading 吗?

    由于我可以使用委托执行异步操作 我怀疑在我的应用程序中使用 System Threading 的机会很小 是否存在我无法避免 System Threading 的基本情况 只是我正处于学习阶段 例子 class Program public
  • x:将 ViewModel 方法绑定到 DataTemplate 内的事件

    我基本上问同样的问题这个人 https stackoverflow com questions 10752448 binding to viewmodels property from a template 但在较新的背景下x Bind V
  • C 编程:带有数组的函数

    我正在尝试编写一个函数 该函数查找行为 4 列为 4 的二维数组中的最大值 其中二维数组填充有用户输入 我知道我的主要错误是函数中的数组 但我不确定它是什么 如果有人能够找到我出错的地方而不是编写新代码 我将不胜感激 除非我刚去南方 我的尝
  • 复制目录下所有文件

    如何将一个目录中的所有内容复制到另一个目录而不循环遍历每个文件 你不能 两者都不Directory http msdn microsoft com en us library system io directory aspx nor Dir
  • 为什么 isnormal() 说一个值是正常的,而实际上不是?

    include
  • 如何在 Android 中使用 C# 生成的 RSA 公钥?

    我想在无法假定 HTTPS 可用的情况下确保 Android 应用程序和 C ASP NET 服务器之间的消息隐私 我想使用 RSA 来加密 Android 设备首次联系服务器时传输的对称密钥 RSA密钥对已在服务器上生成 私钥保存在服务器
  • C++ 继承的内存布局

    如果我有两个类 一个类继承另一个类 并且子类仅包含函数 那么这两个类的内存布局是否相同 e g class Base int a b c class Derived public Base only functions 我读过编译器无法对数
  • 对于某些 PDF 文件,LoadIFilter() 返回 -2147467259

    我正在尝试使用 Adob e IFilter 搜索 PDF 文件 我的代码是用 C 编写的 我使用 p invoke 来获取 IFilter 的实例 DllImport query dll SetLastError true CharSet
  • C++ 中的 include 和 using 命名空间

    用于使用cout 我需要指定两者 include
  • 为什么 std::uint32_t 与 uint32_t 不同?

    我对 C 有点陌生 我有一个编码作业 很多文件已经完成 但我注意到 VS2012 似乎有以下语句的问题 typedef std uint32 t identifier 不过 似乎将其更改为 typedef uint32 t identifi
  • C# 使用“?” if else 语句设置值这叫什么

    嘿 我刚刚看到以下声明 return name null name NA 我只是想知道这在 NET 中叫什么 是吗 代表即然后执行此操作 这是一个俗称的 条件运算符 三元运算符 http en wikipedia org wiki Tern
  • 指针和内存范围

    我已经用 C 语言编程有一段时间了 但对 C 语言还是很陌生 有时我对 C 处理内存的方式感到困惑 考虑以下有效的 C 代码片段 const char string void where is this pointer variable l
  • Mono 应用程序在非阻塞套接字发送时冻结

    我在 debian 9 上的 mono 下运行一个服务器应用程序 大约有 1000 2000 个客户端连接 并且应用程序经常冻结 CPU 使用率达到 100 我执行 kill QUIT pid 来获取线程堆栈转储 但它总是卡在这个位置
  • 现代编译器是否优化乘以 1 和 -1

    如果我写 template

随机推荐