频域旋转 - 关于问题 64441200 的问题
我刚刚运行了这个确切的代码(从作者的代码中复制粘贴,仅对结果图像在 0 到 255 之间进行额外的标准化,如图所示) ...但是我得到了可怕的“锯齿”伪影...这怎么可能?我看到OP显示了来自频率空间旋转的漂亮的未加工图像...我很好奇如何获得它,你肯定没有显示所有代码吗?
import numpy as np
import cv2
from numpy.fft import fftshift as fftshift
from numpy.fft import ifftshift as ifftshift
angle = 30
M = cv2.imread("phantom.png")
M = cv2.cvtColor(M, cv2.COLOR_BGR2GRAY)
M=np.float32(M)
hanning=cv2.createHanningWindow((M.shape[1],M.shape[0]),cv2.CV_32F)
M=hanning*M
sM = fftshift(M)
rotation_center=(M.shape[1]/2,M.shape[0]/2)
rot_matrix=cv2.getRotationMatrix2D(rotation_center,angle,1.0)
FsM = fftshift(cv2.dft(sM,flags = cv2.DFT_COMPLEX_OUTPUT))
rFsM=cv2.warpAffine(FsM,rot_matrix,(FsM.shape[1],FsM.shape[0]),flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
IrFsM = ifftshift(cv2.idft(ifftshift(rFsM),flags=cv2.DFT_REAL_OUTPUT))
x = IrFsM
x = ((x-np.min(x[:]))/(np.max(x[:])-np.min(x[:])))*255.0
cv2.imwrite('rotated_phantom.png',x)
输出图像为:
另外,我总是被告知由于插值,不可能正确旋转(离散的,非连续的)傅立叶空间,那么您如何解释这一点?
Following : How to rotate a non-squared image in frequency domain
I just ran this exact code (copy pasted from the author's code with just an additional normalization of the resulting image between 0 an 255 as shown)
... but I get horrible "aliasing" artifacts ... How is this possible ? I see that the OP shows nice unartifacted images from the rotation in frequency space... I would be very curious to know how to obtain that, surely you did not show all your code?
import numpy as np
import cv2
from numpy.fft import fftshift as fftshift
from numpy.fft import ifftshift as ifftshift
angle = 30
M = cv2.imread("phantom.png")
M = cv2.cvtColor(M, cv2.COLOR_BGR2GRAY)
M=np.float32(M)
hanning=cv2.createHanningWindow((M.shape[1],M.shape[0]),cv2.CV_32F)
M=hanning*M
sM = fftshift(M)
rotation_center=(M.shape[1]/2,M.shape[0]/2)
rot_matrix=cv2.getRotationMatrix2D(rotation_center,angle,1.0)
FsM = fftshift(cv2.dft(sM,flags = cv2.DFT_COMPLEX_OUTPUT))
rFsM=cv2.warpAffine(FsM,rot_matrix,(FsM.shape[1],FsM.shape[0]),flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
IrFsM = ifftshift(cv2.idft(ifftshift(rFsM),flags=cv2.DFT_REAL_OUTPUT))
x = IrFsM
x = ((x-np.min(x[:]))/(np.max(x[:])-np.min(x[:])))*255.0
cv2.imwrite('rotated_phantom.png',x)
the output image is:
Also, i ve always been told that it was impossible to correctly rotate (in discrete, not continuous) Fourier space because of interpolation, so how can you explain that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DFT 在频率域和空间域中给图像强加了周期性(有些人不同意这一点,但仍然同意这种观点是解释 DFT 中发生的几乎所有事情的好方法......)。因此,想象一下你的输入不是谢普-洛根幻影,而是它的无限重复。在频域中操作数据时,您不仅会影响您看到的图像的一份副本,还会影响所有图像,而且并不总是以直观的方式。
后果之一是空间域中图像的相邻副本会旋转,但也会扩展并进入您的图像。
避免这种情况的最简单方法是用零填充图像以使其大小加倍。
现在您可以像以前一样在频域中应用旋转。但请注意,旋转中心应为
shape//2
处的像素,不要使用真正的除法!另请注意,我们不再需要应用窗口函数。最后,将结果裁剪回原始大小。
请注意,结果并不漂亮。在空间域中旋转要好得多,因为正如您所说,频域中的插值并不真正有意义。
The DFT imposes a periodicity to the image in both the frequency and the spatial domain (some people disagree with this, but still agree that this view is a good way to explain just about everything that happens in the DFT...). So imagine that your input is not the Shepp-Logan phantom, but an infinite repetition of it. When manipulating the data in the frequency domain, you affect not just the one copy of the image you see, but all of them, and not always in intuitive ways.
One of the consequences is that neighboring copies of your image in the spatial domain rotate, but also expand and come into you image.
The simplest way to avoid this is to pad the image with zeros to double its size.
Now you can apply the rotation in the frequency domain like before. But note that the center of rotation should the pixel at
shape//2
, do not use a true division! Also note that we no longer need to apply a window function.Finally, crop the result back to its original size.
Do note that the result is not pretty. It is much better to rotate in the spatial domain because, as you said, interpolation in the frequency domain is not really sensical.