opencv的傅里叶变换,实际使用举例

opencv的傅里叶变换,实际使用举例

'''
有关于傅里叶变换的高频低频:在经过频谱居中后的频谱中,中间最亮的点是最低频率,属于直流分量(DC分量)。越往边外走,频率越高
'''
import numpy as np
import cv2
from matplotlib import pyplot as plt


def ifft2(fshift):
    ishift = np.fft.ifftshift(fshift)
    iimg = np.fft.ifft2(ishift)
    iimg = np.abs(iimg)
    return iimg


# plt显示中文
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['font.serif'] = ['KaiTi']

# 原图,灰度图单通道
img = cv2.imread('E:/work/Image/DogCat/dog_10.jpg', 0)  # 0表示灰度图
height, width = img.shape
c_height, c_width = int(height/2), int(width/2)  # 计算中心位置的坐标
plt.subplot(151), plt.imshow(img, cmap = 'gray')
plt.title('原图'), plt.axis('off')

# 傅里叶变换
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20*np.log(np.abs(fshift))
plt.subplot(152), plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('傅里叶'), plt.axis('off')

# 逆傅里叶
iimg = ifft2(fshift)
plt.subplot(153), plt.imshow(iimg, cmap='gray')
plt.title('逆傅里叶'), plt.axis('off')

# 高通滤波,将傅里叶变换结果图像中的低频分量值替换为0,屏蔽低频信号,保留高频信号,实现高通滤波,相当于提取边缘特征
fShift = fshift.copy()
fShift[c_height-30:c_height+30, c_width-30:c_width+30] = 0  # 中心上下左右30个像素大小的区域置零,实现高通滤波
iimg = ifft2(fShift)
plt.subplot(154), plt.imshow(iimg, cmap='gray')
plt.title('高通滤波'), plt.axis('off')

# 低通滤波,将傅里叶变换结果图像中的高频信号值替换为0,屏蔽高频信号,保留低频信号,实现低通滤波,相当于模糊图片
fShift = fshift.copy()
fShift[0:height, 0:width] = 0
fShift[c_height-30:c_height+30, c_width-30:c_width+30] = fshift[c_height-30:c_height+30, c_width-30:c_width+30]
iimg = ifft2(fShift)
plt.subplot(155), plt.imshow(iimg, cmap='gray')
plt.title('低通滤波'), plt.axis('off')

plt.show()

图片显示结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值