【发布时间】: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