【问题标题】:Image Registration - Phase Correlation Algorithm using AForge.NET Framework (C#)图像配准 - 使用 AForge.NET Framework (C#) 的相位相关算法
【发布时间】:2015-03-17 21:24:45
【问题描述】:

我一直在用 C# 编写代码,以使用此相位相关算法记录两个图像之间的偏移(x、y 轴),如维基百科中所示:Phase Correlation

为了帮助我解决这个问题,我使用了 AForge.NET Framework 的 ComplexImage 类及其快速傅里叶变换方法。

代码如下:

 Bitmap fixed = (Bitmap)System.Drawing.Image.FromFile("Lenna.png");
 Bitmap shifted = (Bitmap)System.Drawing.Image.FromFile("Lenna2.png");

 //apply greyscale
 fixed = Grayscale.CommonAlgorithms.BT709.Apply(fixed);
 shifted = Grayscale.CommonAlgorithms.BT709.Apply(shifted);

 ComplexImage fixedCplx = ComplexImage.FromBitmap(fixed);
 int height = fixedCplx.Data.GetLength(0);
 int width  = fixedCplx.Data.GetLength(1);
 ComplexImage shiftedCplx = ComplexImage.FromBitmap(shifted);

 fixedCplx.ForwardFourierTransform();
 shiftedCplx.ForwardFourierTransform();

 for (int y = 0; y < height; y++)
 {
     for (int x = 0; x < width; x++)
     {
         //Calculating elementwise complex conjugate of the shifted image 2d vector
         shiftedCplx.Data[y, x].Im = -1 * shiftedCplx.Data[y, x].Im;
         //Elementwise multiplication to obtain cross power spectrum
         shiftedCplx.Data[y, x] = Complex.Multiply(fixedCplx.Data[y, x], shiftedCplx.Data[y, x]);
         //Elementwise normalization
         shiftedCplx.Data[y, x] = Complex.Divide(shiftedCplx.Data[y, x], shiftedCplx.Data[y, x].Magnitude);
     }
 }
 shiftedCplx.BackwardFourierTransform();

然而,在计算逆变换时应该出现的峰值函数并没有显示出来……我已经用维基百科链接上的图片进行了测试,但结果我得到的只是一张白色的图片。上面显示的代码有什么问题吗?

一些注意事项:

  • 也许我不了解算法背后的数学原理。当采用元素乘积 [Ga].[Gb*] 时,这个 * 符号是否意味着我需要像我一直在做的那样共轭矩阵的每个元素?还是矩阵的conjugate transpose
  • 当归一化 [Ga].[Gb*]/|[Ga].[Gb*]| 时,将每个矩阵元素(都是复数)除以其大小是否正确?我已经在我的代码中完成了这项工作,并且正如预期的那样,在算法结束时,shiftedCplx.Data[] 数组中的每个元素的幅度 = 1(这意味着此时所有相关信息都是它们的相位)。

看起来“BackwardFourierTransform()”函数并没有真正按预期工作,或者在我破坏数据的过程中的某个地方。

编辑 1

刚刚编辑了评论中指出的 jaket 的代码。

另外,我刚刚意识到的一件事:当傅里叶变换灰度图像时,相应 2D 向量中的每个值的范围都是 0 到 255。对我来说,这暗示逆变换会产生介于该范围之间的某个值也是如此(即使事先对数据进行了标准化),但我发现在这种情况下并非如此。 shiftedCplx.Data[,] 向量中的大多数元素在转换回空间域后的值都会大于 255,当然,用它制作位图图像会创建一个大部分都是白色的图像。我有什么遗漏吗?

【问题讨论】:

  • imgTrslCpl.Data[y, x] 是如何定义的?
  • 抱歉,应该是shiftedCplx.Data[y,x]。在任何情况下,ComplexImage.Data[ ] 都被定义为Complex 类型结构的二维数组。
  • Complex 结构具有存储复数的实部和虚部以及幅度和相位的字段。方法包括Complex.Multiply()Complex.Divide()等操作
  • 是的,我明白了,我只是没有看到 imgTrslCpl 的定义位置,想知道这是否与您的问题有关。

标签: c# algorithm image-processing fft aforge


【解决方案1】:

在 POC 中,F*G 不是矩阵的乘法,而是矩阵的元素产生。

所以,它被称为Hadamard Production。

在 matlab 中,该产生式由“.*”运算符操作。

但是在AForge.net的Complex.functions中,并没有这样的操作,可能是……

    // 푸리에 변환 한 이미지를 현실과 상상의 부분 분해
    Cv.Split (dft1, srcRe1, srcIm1, null , null );
    Cv.Split (dft2, srcRe2, srcIm2, null , null );

    for  ( int  I = 0; i <dft_M; i ++)
    {
        for  ( int  J = 0; j <dft_N; j ++)
        {
            // 이미지를  F × G *
            dstRe [i, j] = srcRe1 [i, j] * srcRe2 [i, j] - srcIm1 [i, j] * srcIm2 [i, j];
            dstIm [i, j] = srcRe1 [i, j] * srcIm2 [i, j + srcRe2 [i, j] * srcIm1 [i, j];

            // 절대 값을 계산하고 그 값으로 나누면 | F × G * |
            double  Spectrum = Math.Sqrt (dstRe [i, j] * dstRe [i, j + dstIm [i, j] * dstIm [i, j]);
            dstRe [i, j] = dstRe [i, j] / spectrum;
            dstIm [i, j] = dstIm [i, j] / spectrum;
        }
    }
    // 계산 결과를 통합
    Cv.Merge (dstRe, dstIm, null , null , result);

我希望这会有所帮助。我还需要你完成的 POC 的 c# 代码。 我也是AForge.Net用户~。 (_:

enter image description here

【讨论】:

  • 使用正向 FFT。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 2015-09-17
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多