如何使用python将一幅图像的相位和另一幅图像的幅值合并为一张图像 [英] How to combine the phase of one image and magnitude of different image into 1 image by using python
问题描述
我想将一幅图像的相位谱和另一幅图像的幅值谱合并为一幅图像.
我得到了图像A和图像B的相位谱和幅度谱.
这是代码.
f = np.fft.fft2(grayA)fshift1 = np.fft.fftshift(f)phase_spectrumA = np.angle(fshift1)itude_spectrumB = 20 * np.log(np.abs(fshift1))f2 = np.fft.fft2(grayB)fshift2 = np.fft.fftshift(f2)phase_spectrumB = np.angle(fshift2)itude_spectrumB = 20 * np.log(np.abs(fshift2))
我试图弄清楚,但我仍然不知道该怎么做.
下面是我的测试代码.
imgCombined = abs(f)* math.exp(1j * np.angle(f2))
我希望我能像那样出来
为使代码按预期工作,您需要修复以下几件事:
-
请注意,
imgCombined
可能包含[0,1]
范围之外的值.然后,您需要确定如何重新缩放值以适合预期的[0,1]
范围.- 默认缩放比例(导致上图所示)是线性缩放值,使得最小值设置为0,最大值设置为0.
- 另一种方法可能是将值限制在该范围内(即,将所有负值强制为0,并且将所有大于1的值强制为1).
- 最后,另一种方法似乎提供的结果更接近所提供的屏幕截图,该方法将是使用
imgCombined = np.abs(imgCombined)
取绝对值
I want to combine phase spectrum of one image and magnitude spectrum of different image into one image.
I have got phase spectrum and magnitude spectrum of image A and image B.
Here is the code.
f = np.fft.fft2(grayA) fshift1 = np.fft.fftshift(f) phase_spectrumA = np.angle(fshift1) magnitude_spectrumB = 20*np.log(np.abs(fshift1)) f2 = np.fft.fft2(grayB) fshift2 = np.fft.fftshift(f2) phase_spectrumB = np.angle(fshift2) magnitude_spectrumB = 20*np.log(np.abs(fshift2))
I trying to figure out , but still i do not know how to do that.
Below is my test code.
imgCombined = abs(f) * math.exp(1j*np.angle(f2))
I wish i can come out just like that
解决方案Here are the few things that you would need to fix for your code to work as intended:
- The
math.exp
function supports scalar exponentiation. For an element-wise matrix exponentiation you should usenumpy.exp
instead. - Similary, the
*
operator would attempt to perform matrix multiplication. In your case you want to instead perform element-wise multiplication which can be done withnp.multiply
With these fixes you should get the frequency-domain combined matrix as follows:
combined = np.multiply(np.abs(f), np.exp(1j*np.angle(f2)))
To obtain the corresponding spatial-domain image, you would then need compute the inverse transform (and take the real part since there my be residual small imaginary parts due to numerical errors) with:
imgCombined = np.real(np.fft.ifft2(combined))
Finally the result can be shown with:
import matplotlib.pyplot as plt plt.imshow(imgCombined, cmap='gray')
Note that
imgCombined
may contain values outside the[0,1]
range. You would then need to decide how you want to rescale the values to fit the expected[0,1]
range.- The default scaling (resulting in the image shown above) is to linearly scale the values such that the minimum value is set to 0, and the maximum value is set to 0.
- Another way could be to limit the values to that range (i.e. forcing all negative values to 0 and all values greater than 1 to 1).
- Finally another approach, which seems to provide a result closer to the screenshot provided, would be to take the absolute value with
imgCombined = np.abs(imgCombined)
这篇关于如何使用python将一幅图像的相位和另一幅图像的幅值合并为一张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!