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 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.