Python 低通 高通 理想滤波器 巴特沃斯 数字图像处理 频域滤波 图像增强

本文主要是介绍Python 低通 高通 理想滤波器 巴特沃斯 数字图像处理 频域滤波 图像增强,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题

(一)频域低通滤波

  1. 产生白条图像 f1(x,y)(640×640 大小,中间亮条宽160,高 400,居中,暗处=0,亮处=255)
  2. 设计不同截止频率的理想低通滤波器、Butterworth低通滤波器,对其进行频域增强。观察频域滤波效果,并解释之。

(二)频域高通滤波

  1. 设计不同截止频率的理想高通滤波器、Butterworth高通滤波器,对上述白条图像进行频域增强。观察频域滤波效果,并解释之。
  2. 设计不同截止频率的理想高通滤波器、Butterworth高通滤波器,对含高斯噪声的lena图像进行频域增强。观察频域滤波效果,并解释之。

代码

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt"""
(一)频域低通滤波
产生如图所示图象 f1(x,y)(64×64 大小,中间亮条宽16,高 40,居中,暗处=0,亮处=255)
产生实验四中的白条图像。
设计不同截止频率的理想低通滤波器、Butterworth低通滤波器,对其进行频域增强。
观察频域滤波效果,并解释之。
"""def pro_11():def ideal_low_filter(lr, cr, cc, img):tmp = np.zeros((img.shape[0], img.shape[1]))for i in range(img.shape[0]):for j in range(img.shape[1]):tmp[i, j] = (1 if np.sqrt((i - cr) ** 2 + (j - cc) ** 2) <= lr else 0)return tmp# 产生白条图像im_arr = np.zeros((640, 640))for i in range(im_arr.shape[0]):for j in range(im_arr.shape[1]):if 120 < i < 520 and 240 < j < 400:im_arr[i, j] = 255im_ft2 = np.fft.fft2(np.array(im_arr))  # 白条图二维傅里叶变换矩阵im_ft2_shift = np.fft.fftshift(im_ft2)r, c = im_arr.shape[0], im_arr.shape[1]cr, cc = r // 2, c // 2  # 频谱中心# 理想滤波器ideal_filter1 = ideal_low_filter(10, cr, cc, im_ft2_shift)ideal_filter2 = ideal_low_filter(30, cr, cc, im_ft2_shift)# 求经理想低通滤波器后的图像tmp = im_ft2_shift * ideal_filter1irreversed_im_ft2 = np.fft.ifft2(tmp)tmp2 = im_ft2_shift * ideal_filter2irreversed_im_ft22 = np.fft.ifft2(tmp2)plt.figure(figsize=(13, 13))plt.subplot(221)plt.imshow(Image.fromarray(np.abs(im_arr)))plt.subplot(223)plt.imshow(Image.fromarray(np.abs(im_ft2_shift)))plt.subplot(222)plt.title("lr=10")plt.imshow(Image.fromarray(np.abs(irreversed_im_ft2)))plt.subplot(224)plt.title("lr=30")plt.imshow(Image.fromarray(np.abs(irreversed_im_ft22)))plt.show()def pro_12():def butterworth(lr, cr, cc, n, img):tmp = np.zeros((img.shape[0], img.shape[1]))for i in range(img.shape[0]):for j in range(img.shape[1]):tmp[i, j] = 1 / (1 + np.sqrt((i - cr) ** 2 + (j - cc) ** 2) / lr) ** (2 * n)return tmp# 产生白条图像im_arr = np.zeros((640, 640))for i in range(im_arr.shape[0]):for j in range(im_arr.shape[1]):if 120 < i < 520 and 240 < j < 400:im_arr[i, j] = 255im_ft2 = np.fft.fft2(np.array(im_arr))  # 白条图二维傅里叶变换矩阵im_ft2_shift = np.fft.fftshift(im_ft2)r, c = im_arr.shape[0], im_arr.shape[1]cr, cc = r // 2, c // 2  # 频谱中心# 理想滤波器butterworth1 = butterworth(10, cr, cc, 2, im_arr)butterworth2 = butterworth(30, cr, cc, 2, im_arr)# 求经理想低通滤波器后的图像tmp = im_ft2_shift * butterworth1irreversed_im_ft2 = np.fft.ifft2(tmp)tmp2 = im_ft2_shift * butterworth2irreversed_im_ft22 = np.fft.ifft2(tmp2)plt.figure(figsize=(13, 13))plt.subplot(221)plt.imshow(Image.fromarray(np.abs(im_arr)))plt.subplot(223)plt.imshow(Image.fromarray(np.abs(im_ft2_shift)))plt.subplot(222)plt.title("lr=10")plt.imshow(Image.fromarray(np.abs(irreversed_im_ft2)))plt.subplot(224)plt.title("lr=30")plt.imshow(Image.fromarray(np.abs(irreversed_im_ft22)))plt.show()def pro_12():def ideal_low_filter(lr, cr, cc, img):tmp = np.zeros((img.shape[0], img.shape[1]))for i in range(img.shape[0]):for j in range(img.shape[1]):tmp[i, j] = (1 if np.sqrt((i - cr) ** 2 + (j - cc) ** 2) <= lr else 0)return tmpdef butterworth(lr, cr, cc, n, img):tmp = np.zeros((img.shape[0], img.shape[1]))for i in range(img.shape[0]):for j in range(img.shape[1]):tmp[i, j] = 1 / (1 + np.sqrt((i - cr) ** 2 + (j - cc) ** 2) / lr) ** (2 * n)return tmpdef gauss_noise(img, sigma):temp_img = np.float64(np.copy(img))h = temp_img.shape[0]w = temp_img.shape[1]noise = np.random.randn(h, w) * sigmanoisy_img = np.zeros(temp_img.shape, np.float64)if len(temp_img.shape) == 2:noisy_img = temp_img + noiseelse:noisy_img[:, :, 0] = temp_img[:, :, 0] + noisenoisy_img[:, :, 1] = temp_img[:, :, 1] + noisenoisy_img[:, :, 2] = temp_img[:, :, 2] + noise# noisy_img = noisy_img.astype(np.uint8)return noisy_imglena = np.array(Image.open("lena_gray_512.tif"))noise_lena = gauss_noise(lena, 25)noise_lena_fft2 = np.fft.fft2(noise_lena)noise_lena_fft2_shift = np.fft.fftshift(noise_lena_fft2)r, c = lena.shape[0], lena.shape[1]cr, cc = r // 2, c // 2  # 频谱中心butterworth1 = butterworth(30, cr, cc, 2, lena)butterworth2 = butterworth(50, cr, cc, 2, lena)ideal_filter1 = ideal_low_filter(10, cr, cc, noise_lena_fft2_shift)ideal_filter2 = ideal_low_filter(30, cr, cc, noise_lena_fft2_shift)btmp1 = noise_lena_fft2_shift * butterworth1blena_ift21 = np.fft.ifft2(btmp1)btmp2 = noise_lena_fft2_shift * butterworth2blena_ift22 = np.fft.ifft2(btmp2)itmp1 = noise_lena_fft2_shift * ideal_filter1ilena_ift21 = np.fft.ifft2(itmp1)itmp2 = noise_lena_fft2_shift * ideal_filter2ilena_ift22 = np.fft.ifft2(itmp2)plt.figure(figsize=(13, 13))plt.subplot(221)plt.title("Butterworth Filter: lr=30/100")plt.imshow(Image.fromarray(np.abs(blena_ift21)))plt.subplot(223)plt.imshow(Image.fromarray(np.abs(blena_ift22)))plt.subplot(222)plt.title("Ideal Filter: lr=10/30")plt.imshow(Image.fromarray(np.abs(ilena_ift21)))plt.subplot(224)plt.imshow(Image.fromarray(np.abs(ilena_ift22)))plt.show()"""
(二)频域高通滤波
1. 设计不同截止频率的理想高通滤波器、Butterworth高通滤波器,对上述白条图像进行频域增强。观察频域滤波效果,并解释之。
2. 设计不同截止频率的理想高通滤波器、Butterworth高通滤波器,对含高斯噪声的lena图像进行频域增强。观察频域滤波效果,并解释之。
"""def pro_2():def ideal_high_filter(lr, cr, cc, img):tmp = np.zeros((img.shape[0], img.shape[1]))for i in range(img.shape[0]):for j in range(img.shape[1]):tmp[i, j] = (0 if np.sqrt((i - cr) ** 2 + (j - cc) ** 2) <= lr else 1)return tmpdef butterworth_high(lr, cr, cc, n, img):tmp = np.zeros((img.shape[0], img.shape[1]))for i in range(img.shape[0]):for j in range(img.shape[1]):tmp[i, j] = 1 / (1 + lr / np.sqrt((i - cr) ** 2 + (j - cc) ** 2)) ** (2 * n)return tmpdef gauss_noise(img, sigma):temp_img = np.float64(np.copy(img))h = temp_img.shape[0]w = temp_img.shape[1]noise = np.random.randn(h, w) * sigmanoisy_img = np.zeros(temp_img.shape, np.float64)if len(temp_img.shape) == 2:noisy_img = temp_img + noiseelse:noisy_img[:, :, 0] = temp_img[:, :, 0] + noisenoisy_img[:, :, 1] = temp_img[:, :, 1] + noisenoisy_img[:, :, 2] = temp_img[:, :, 2] + noise# noisy_img = noisy_img.astype(np.uint8)return noisy_imgdef lena_proceed():lena = np.array(Image.open("lena_gray_512.tif"))noise_lena = gauss_noise(lena, 25)noise_lena_fft2 = np.fft.fft2(noise_lena)noise_lena_fft2_shift = np.fft.fftshift(noise_lena_fft2)r, c = lena.shape[0], lena.shape[1]cr, cc = r // 2, c // 2  # 频谱中心butterworth1 = butterworth_high(10, cr, cc, 1, lena)butterworth2 = butterworth_high(5, cr, cc, 1, lena)ideal_filter1 = ideal_high_filter(10, cr, cc, noise_lena_fft2_shift)ideal_filter2 = ideal_high_filter(30, cr, cc, noise_lena_fft2_shift)btmp1 = noise_lena_fft2_shift * butterworth1blena_ift21 = np.fft.ifft2(btmp1)btmp2 = noise_lena_fft2_shift * butterworth2blena_ift22 = np.fft.ifft2(btmp2)itmp1 = noise_lena_fft2_shift * ideal_filter1ilena_ift21 = np.fft.ifft2(itmp1)itmp2 = noise_lena_fft2_shift * ideal_filter2ilena_ift22 = np.fft.ifft2(itmp2)plt.figure(figsize=(13, 13))plt.subplot(221)plt.title("Butterworth Filter: lr=30/5")plt.imshow(Image.fromarray(np.abs(blena_ift21)))plt.subplot(223)plt.imshow(Image.fromarray(np.abs(blena_ift22)))plt.subplot(222)plt.title("Ideal Filter: lr=10/30")plt.imshow(Image.fromarray(np.abs(ilena_ift21)))plt.subplot(224)plt.imshow(Image.fromarray(np.abs(ilena_ift22)))plt.show()def white_bar_proceed():# 产生白条图像im_arr = np.zeros((640, 640))for i in range(im_arr.shape[0]):for j in range(im_arr.shape[1]):if 120 < i < 520 and 240 < j < 400:im_arr[i, j] = 255im_ft2 = np.fft.fft2(np.array(im_arr))  # 白条图二维傅里叶变换矩阵im_ft2_shift = np.fft.fftshift(im_ft2)r, c = im_arr.shape[0], im_arr.shape[1]cr, cc = r // 2, c // 2  # 频谱中心butterworth1 = butterworth_high(30, cr, cc, 1, im_arr)butterworth2 = butterworth_high(5, cr, cc, 1, im_arr)ideal_filter1 = ideal_high_filter(10, cr, cc, im_ft2_shift)ideal_filter2 = ideal_high_filter(30, cr, cc, im_ft2_shift)btmp1 = im_ft2_shift * butterworth1blena_ift21 = np.fft.ifft2(btmp1)btmp2 = im_ft2_shift * butterworth2blena_ift22 = np.fft.ifft2(btmp2)itmp1 = im_ft2_shift * ideal_filter1ilena_ift21 = np.fft.ifft2(itmp1)itmp2 = im_ft2_shift * ideal_filter2ilena_ift22 = np.fft.ifft2(itmp2)plt.figure(figsize=(13, 13))plt.subplot(221)plt.title("Butterworth Filter: lr=30/5")plt.imshow(Image.fromarray(np.abs(blena_ift21)))plt.subplot(223)plt.imshow(Image.fromarray(np.abs(blena_ift22)))plt.subplot(222)plt.title("Ideal Filter: lr=10/30")plt.imshow(Image.fromarray(np.abs(ilena_ift21)))plt.subplot(224)plt.imshow(Image.fromarray(np.abs(ilena_ift22)))plt.show()lena_proceed()white_bar_proceed()if __name__ == '__main__':pro_11()pro_12()pro_2()

结果

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

这篇关于Python 低通 高通 理想滤波器 巴特沃斯 数字图像处理 频域滤波 图像增强的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/289570

相关文章

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑