数字图像处理之二维傅里叶变换

二维连续傅里叶变换

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二维离散傅里叶变换

在这里插入图片描述

二维离散傅里叶变换的性质

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


from builtins import print, int

import cv2
import numpy as np
from matplotlib import pyplot as plt


# shape: 600 * 600
img = cv2.imread('../pic/Fig0438(a)(bld_600by600).tif', 0)
# 返回的是复数 dtype.complex128
fft = np.fft.fft2(img)
print("fft is: ", fft)
# 平移
fftshift = np.fft.fftshift(fft)
print("fftshift is: ", fftshift)
# 频谱 dtype.float64 magnitude_spectrum[180,180] = 329.2611
magnitude_spectrum = 20 * np.log(np.abs(fftshift))
print(magnitude_spectrum[300, 300])

# 如果想要用cv2.imshow()显示
# magnitude_spectrum_uint8 = np.uint8(255 * (magnitude_spectrum / np.max(magnitude_spectrum)))
# cv2.imshow("origin", img)
# cv2.imshow("magnitude_spectrum_uint8", magnitude_spectrum_uint8)
# cv2.waitKey(0)
rows, cols = img.shape
crow, ccol = rows / 2, cols / 2
# 频率中心区域添加60x60的蒙板, 相当于过滤了低频部分
fftshift[int(crow - 30):int(crow+30), int(ccol-30):int(crow+30)] = 0
magnitude_spectrum_filter = 20 * np.log(np.abs(fftshift)+0.0000001)
# 中心平移回到左上角
f_ishift = np.fft.ifftshift(fftshift)
# 使用FFT逆变换, 结果是复数
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
# img_back_uint8 = np.uint8(255 * (img_back / np.max(img_back)))
# cv2.imshow("img_back_uint8", img_back_uint8)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
plt.subplot(221)
plt.imshow(img, cmap='gray')
plt.title('origin image')
# 省略x, y坐标
plt.xticks([]), plt.yticks([])
plt.subplot(222), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('magnitude_spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(223), plt.imshow(magnitude_spectrum_filter, cmap='gray')
plt.title('High Pass Filter'), plt.xticks([]), plt.yticks([])
plt.subplot(224), plt.imshow(img_back, cmap='gray')
plt.title("High Pass Result"), plt.xticks([]), plt.yticks([])
plt.show()



"""
Created by HenryMa on 2020/8/27
"""

__author__ = 'HenryMa'

from builtins import print, int

import cv2
import numpy as np


img = cv2.imread('../pic/Fig0438(a)(bld_600by600).tif', 0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
# 平移还是要靠numpy
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
# float32
print(dft.dtype)

rows, cols = img.shape
crow, ccol = int(rows / 2), int(cols / 2)

# 创建蒙板
mask = np.ones((rows, cols, 2), np.uint8)
msize = 70
mask[crow-int(msize/2): crow+int(msize/2), ccol-int(msize/2): ccol+int(msize/2)] = 0
fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)

# 此时img_back为复数
img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])

cv2.imshow('origin', img)
cv2.imshow('img_back', img_back)
cv2.waitKey(0)
cv2.destroyAllWindows()

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨痕_777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值