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的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合