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中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图