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如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常

基于Python实现多语言朗读与单词选择测验

《基于Python实现多语言朗读与单词选择测验》在数字化教育日益普及的今天,开发一款能够支持多语言朗读和单词选择测验的程序,对于语言学习者来说无疑是一个巨大的福音,下面我们就来用Python实现一个这... 目录一、项目概述二、环境准备三、实现朗读功能四、实现单词选择测验五、创建图形用户界面六、运行程序七、

浅析Python中的绝对导入与相对导入

《浅析Python中的绝对导入与相对导入》这篇文章主要为大家详细介绍了Python中的绝对导入与相对导入的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1 Imports快速介绍2 import语句的语法2.1 基本使用2.2 导入声明的样式3 绝对import和相对i

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Python中conda虚拟环境创建及使用小结

《Python中conda虚拟环境创建及使用小结》本文主要介绍了Python中conda虚拟环境创建及使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录0.前言1.Miniconda安装2.conda本地基本操作3.创建conda虚拟环境4.激活c

使用Python创建一个能够筛选文件的PDF合并工具

《使用Python创建一个能够筛选文件的PDF合并工具》这篇文章主要为大家详细介绍了如何使用Python创建一个能够筛选文件的PDF合并工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录背景主要功能全部代码代码解析1. 初始化 wx.Frame 窗口2. 创建工具栏3. 创建布局和界面控件4

一文详解如何在Python中使用Requests库

《一文详解如何在Python中使用Requests库》:本文主要介绍如何在Python中使用Requests库的相关资料,Requests库是Python中常用的第三方库,用于简化HTTP请求的发... 目录前言1. 安装Requests库2. 发起GET请求3. 发送带有查询参数的GET请求4. 发起PO

Python与DeepSeek的深度融合实战

《Python与DeepSeek的深度融合实战》Python作为最受欢迎的编程语言之一,以其简洁易读的语法、丰富的库和广泛的应用场景,成为了无数开发者的首选,而DeepSeek,作为人工智能领域的新星... 目录一、python与DeepSeek的结合优势二、模型训练1. 数据准备2. 模型架构与参数设置3

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应