兩幅圖像幅度譜和相位譜替換
兩幅圖像幅度譜和相位譜替換
例子:蘋果與橘子圖像的幅度譜和相位譜替換
import numpy as np
import cv2
from matplotlib import pyplot as plt
def magnitude_phase_split(img):
# 分離幅度譜與相位譜
dft = np.fft.fft2(img)
dft_shift = np.fft.fftshift(dft)
# 幅度譜
magnitude_spectrum = np.abs(dft_shift)
# 相位譜
phase_spectrum = np.angle(dft_shift)
return magnitude_spectrum,phase_spectrum
def magnitude_phase_combine(img_m,img_p):
# 幅度譜與相位譜結合
img_mandp = img_m*np.e**(1j*img_p)
img_mandp = np.uint8(np.abs(np.fft.ifft2(img_mandp)))
img_mandp =img_mandp/np.max(img_mandp)*255
return img_mandp
# 讀取圖像
img1 = cv2.imread("apple.jpg",0)
img2= cv2.imread("orange.jpg",0)
# 分離幅度譜與相位譜
img1_m,img1_p = magnitude_phase_split(img1)
img2_m,img2_p = magnitude_phase_split(img2)
# 合并幅度譜與相位譜
# 將蘋果圖像的幅度譜與橘子圖像的相位譜結合
img_1mAnd2p = magnitude_phase_combine(img1_m,img2_p)
# 將橘子圖像的幅度譜與蘋果圖像的相位譜結合
img_2mAnd1p = magnitude_phase_combine(img2_m,img1_p)
plt.figure(figsize=(10,8))
plt.subplot(221)
plt.xlabel("apple")
plt.imshow(img1,cmap="gray")
plt.subplot(222)
plt.imshow(img2,cmap="gray")
plt.xlabel("orange")
plt.subplot(223)
plt.imshow(img_1mAnd2p,cmap="gray")
plt.xlabel("applem_and_orangep")
plt.subplot(224)
plt.imshow(img_2mAnd1p,cmap="gray")
plt.xlabel("orangem_and_applep")
plt.show()
輸出結果:
幅度譜與相位譜結合.png