MatLab - 使用FFT移动图像 [英] MatLab - Shifting an image using FFT

查看:218
本文介绍了MatLab - 使用FFT移动图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用exp(-j * 2 * pi * x * F)乘以 fft 来移动图像(由2D矩阵表示),其中x是位移。我有:

I want to shift an image (represented by a 2D matrix) using the multiplication of its fft by exp(-j*2*pi*x*F), where x is the displacement. I have:

input=peaks(200);
H=fftshift(fft2(fftshift(input)));
x=19;
H=H*exp(-1i*x*2*pi*F);
IF_image=fftshift(ifft2(fftshift(H)));
imshow(IF_image)

但是我在识别/代表H中的F时遇到了麻烦[F]因为我的输入是一个二维数组。我怎么能这样做?
所需的输出将是我的原始图像在同一帧中在水平轴上移位(x个单位),因此它将从x + 1开始。例如:

But I'm having troubles identifying/representing the F in H[F] since my input is a 2 dimensional array. How could I do this? The desired output will be my original image shifted in the horizontal axis (by x units) in the same frame so it would start at x+1. As an example:

如果输入=

1 2 3 4 5
6 7 8 9 0

和x = 2,我想要:

and x=2, I want:

4 5 1 2 3
9 0 6 7 8


推荐答案

您在1D中确定了翻译/转移的属性。对于2D,它略有不同,但基于相同的原理。要在2D中实现平移,这是平移/移位属性,定义为:

You identified the property for translation / shifting in 1D. For 2D, it's slightly different but based on the same principle. To achieve the translation in 2D, this is the translation / shift property, which is defined as:

x0,y0 将是班次你想介绍一下。因此, x0 的正值会使您的2D信号向右移动,而负值将向左移动。同样, y0 的正值会使您的2D图像向下移动,而负值会向上移动。

x0,y0 would be the shift you want to introduce. As such, positive value of x0 would shift your 2D signal to the right, while a negative value would shift to the left. Similarly, a positive value of y0 would shift your 2D image downwards, while a negative value would shift upwards.

因此,给定2D中的傅里叶变换,您需要为指数添加一个附加项。此外,您必须 N 或2D信号的大小进行标准化。这假设您的2D信号具有相同的行数和列数。如果不是这种情况,那么你将不得不采用 u * x0 ,你将除以列数和 v * y0 将除以行数。

现在,您在上面的代码中对 F 感到困惑的原因是因为你不确定如何在2D中定义它。您必须为2D网格中的每个点定义频率值 。由于您的 fftshift 调用,我们将定义 x y 介于-100和99之间的值,因为您的2D信号大小为200 x 200,这将使我们的2D信号居中。这实际上是 fftshift 正在做的事情。同样, ifftshift 撤消 fftshift 完成的居中。要在2D中定义这些点,我使用 meshgrid 。定义这些点后,您将获取每对(x,y)坐标,然后按照上面的属性创建复数指数。

Therefore, given your Fourier Transform in 2D, you would need to add an additional term to the exponent. In addition, you must normalize by N or the size of your 2D signal. This is assuming that your 2D signal has the same number of rows and columns. If this isn't the case, then you would have to take u*x0 and you would divide by the number of columns and v*y0 would be divided by the number of rows.
Now, the reason why you're confused about F in your above code is because you aren't sure how to define this in 2D. You must define a frequency value for every point in the 2D grid. Because of your fftshift call, we would define the x and y values between -100 and 99, as your 2D signal is of size 200 x 200 and this would centre our 2D signal to be in the middle. This is actually what fftshift is doing. Similarly, ifftshift undoes the centering done by fftshift. To define these points in 2D, I use meshgrid. Once you define these points, you would take each pair of (x,y) co-ordinates, then create the complex exponential as you see in the above property.

因此,您的代码必须以这种方式进行修改。请记住,我在原始代码中删除了多余的 fftshift ifftshift 调用。您可以调用 fft ,然后执行 fftshift 以使频谱居中。我还将变量输入更改为中的,如 输入 是MATLAB中的一个函数,我们不希望无意中用变量遮蔽函数。

As such, your code would have to be modified in this fashion. Bear in mind that I got rid of redundant fftshift and ifftshift calls in your original code. You would call the fft, then do fftshift to centre the spectrum. I also changed your variable input to in, as input is a function in MATLAB, and we don't want to unintentionally shadow the function with a variable.

我还将 x 转换为-35,并将 y定义为转为-50。这意味着结果信号将向左移动35,然后向上移动50。

I've also defined the x shift to be -35, and the y shift to be -50. This will mean that the resultant signal will shift to the left by 35, and up by 50.

因此:

in=peaks(200); %// Define input signal
H=fftshift(fft2(in)); %// Compute 2D Fourier Transform
x0=-35; %// Define shifts
y0=-50;

%// Define shift in frequency domain
[xF,yF] = meshgrid(-100:99,-100:99);

%// Perform the shift
H=H.*exp(-1i*2*pi.*(xF*x0+yF*y0)/200);

%// Find the inverse Fourier Transform
IF_image=ifft2(ifftshift(H));

%// Show the images
figure;
subplot(1,2,1);
imshow(in);
subplot(1,2,2);
imshow(real(IF_image));






请注意我显示真实结果图像的组件。这是因为一旦你采用逆傅立叶变换,可能会有一些数值不精确,而信号的复杂部分实际上非常小。我们可以通过使用信号的实部来忽略这一点。


Take note that I displayed the real component of the resultant image. This is due to the fact that once you take the inverse Fourier Transform, there may be some numerical imprecision and the complex part of the signal is actually quite small. We can ignore this by just using the real component of the signal.

这是我得到的图像:

正如您所见,正如上面所见的财产所证实的那样,图像确实正确地移动了。如果您想指定不同的班次,您只需要更改 x0 y0 以符合您的口味。在您的情况下,您将指定 y0 = 0 ,然后 x0 可以是您想要的任何水平翻译。

As you can see, the image did shift properly, as verified by the property seen above. If you want to specify different shifts, you just need to change x0 and y0 to suit your tastes. In your case, you would specify y0 = 0, then x0 can be whatever you wish as you want a horizontal translation.

这篇关于MatLab - 使用FFT移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆