两幅图像幅度谱和相位谱替换

两幅图像幅度谱和相位谱替换

两幅图像幅度谱和相位谱替换

例子:苹果与橘子图像的幅度谱和相位谱替换

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()

输出结果:

14657665-df52c9b404c89198.png
幅度谱与相位谱结合.png
发布了82 篇原创文章 · 获赞 39 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_28368377/article/details/104570526
今日推荐