python实现逆滤波与维纳滤波示例
作者:Bing_Shieh 发布时间:2023-07-01 16:36:25
构建运动模糊模型
现假定相机不动,图像f(x,y)在图像面上移动并且图像f(x,y)除移动外不随时间变化。令x0(t)和y0(t)分别代表位移的x分量和y分量,那么在快门开启的时间T内,胶片上某点的总曝光量是图像在移动过程中一系列相应像素的亮度对该点作用之总和。也就是说,运动模糊图像是由同一图像在产生距离延迟后与原图像想叠加而成。如果快门开启与关闭的时间忽略不计,则有:
由于各种运动都是匀速直线运动的叠加,因而我们只需考虑匀速直线运动即可。但由于我们自身水平有限,且旨在探讨找到实现运动模糊复原方法的思想与方向,因而我们未能自行构建模型,而是借鉴了参考文献[1]中建立的运动模糊模型。关于本模型的理论依据参见参考文献[1].
下面我们描述一下该模型函数motion_process(image_size,motion_angle),它包含两个参数:图像的尺寸大小image_size以及运动的角度motion_angle。
例如,当运动位移为9、运动角度为45度时,则该模型函数的构建过程如下:
1. 首先是创建与图像同等大小的全0矩阵,然后找到全0矩阵的中心行数center_position,再计算出运动角度的tan值与cot值,算出运动的偏移量offset。
2. PSF[int(center_position+offset),int(center_position-offset)]=1
3. PSF[int(center_position-offset),int(center_position+offset)]=1
则该模型对应的图像如下图所示:
运动位移为9,运动角度分别为45°、30°、60°时,运动模糊模型对应的图像
import matplotlib.pyplot as graph
import numpy as np
from numpy import fft
import math
import cv2
# 仿真运动模糊
def motion_process(image_size,motion_angle):
PSF = np.zeros(image_size)
print(image_size)
center_position=(image_size[0]-1)/2
print(center_position)
slope_tan=math.tan(motion_angle*math.pi/180)
slope_cot=1/slope_tan
if slope_tan<=1:
for i in range(15):
offset=round(i*slope_tan) #((center_position-i)*slope_tan)
PSF[int(center_position+offset),int(center_position-offset)]=1
return PSF / PSF.sum() #对点扩散函数进行归一化亮度
else:
for i in range(15):
offset=round(i*slope_cot)
PSF[int(center_position-offset),int(center_position+offset)]=1
return PSF / PSF.sum()
#对图片进行运动模糊
def make_blurred(input, PSF, eps):
input_fft = fft.fft2(input)# 进行二维数组的傅里叶变换
PSF_fft = fft.fft2(PSF)+ eps
blurred = fft.ifft2(input_fft * PSF_fft)
blurred = np.abs(fft.fftshift(blurred))
return blurred
def inverse(input, PSF, eps): # 逆滤波
input_fft = fft.fft2(input)
PSF_fft = fft.fft2(PSF) + eps #噪声功率,这是已知的,考虑epsilon
result = fft.ifft2(input_fft / PSF_fft) #计算F(u,v)的傅里叶反变换
result = np.abs(fft.fftshift(result))
return result
def wiener(input,PSF,eps,K=0.01): #维纳滤波,K=0.01
input_fft=fft.fft2(input)
PSF_fft=fft.fft2(PSF) +eps
PSF_fft_1=np.conj(PSF_fft) /(np.abs(PSF_fft)**2 + K)
result=fft.ifft2(input_fft * PSF_fft_1)
result=np.abs(fft.fftshift(result))
return result
image = cv2.imread('you.jpg')
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
img_h=image.shape[0]
img_w=image.shape[1]
graph.figure(1)
graph.xlabel("Original Image")
graph.gray()
graph.imshow(image) #显示原图像
graph.figure(2)
graph.gray()
#进行运动模糊处理
PSF = motion_process((img_h,img_w), 60)
blurred = np.abs(make_blurred(image, PSF, 1e-3))
graph.subplot(231)
graph.xlabel("Motion blurred")
graph.imshow(blurred)
result = inverse(blurred, PSF, 1e-3) #逆滤波
graph.subplot(232)
graph.xlabel("inverse deblurred")
graph.imshow(result)
result=wiener(blurred,PSF,1e-3) #维纳滤波
graph.subplot(233)
graph.xlabel("wiener deblurred(k=0.01)")
graph.imshow(result)
blurred_noisy=blurred + 0.1 * blurred.std() * \
np.random.standard_normal(blurred.shape) #添加噪声,standard_normal产生随机的函数
graph.subplot(234)
graph.xlabel("motion & noisy blurred")
graph.imshow(blurred_noisy) #显示添加噪声且运动模糊的图像
result = inverse(blurred_noisy, PSF, 0.1+1e-3) #对添加噪声的图像进行逆滤波
graph.subplot(235)
graph.xlabel("inverse deblurred")
graph.imshow(result)
result=wiener(blurred_noisy,PSF,0.1+1e-3) #对添加噪声的图像进行维纳滤波
graph.subplot(236)
graph.xlabel("wiener deblurred(k=0.01)")
graph.imshow(result)
graph.show()
参考文献
[1] 何红英. 运动模糊图像恢复算法的研究与实现[D]. 西安科技大学硕士学位论文. 2011.
[2] Rafael C.Gonzalez,Richard E.Woods,Steven L.Eddins. 数字图像处理的MATLAB实现(第2版)[M]. 阮秋琦,译. 北京:清华大学出版社,2013.
[3] 陈建功. 运动模糊图像复原算法研究[D]. 南昌航空大学硕士学位论文. 2012.
来源:https://blog.csdn.net/bingbingxie1/article/details/79398601


猜你喜欢
- <!DOCTYPE html> <html> <head> <meta charset="
- 前言本文主要给大家介绍了关于python指定时间调用函数的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:在前面的一
- USE Demo GO /* 将表Code的列String中的值提取放到Record表中 String 中字符类型为 dsddddd,222
- SQLSERVER查看数据库日志方法和语句示例,已亲测。首先需要查看日志大小:EXEC sys.sp_enumerrorlogs; 可以获取
- 一、基础介绍Go 是静态(编译型)语言,是区别于解释型语言的弱类型语言(静态:类型固定,强类型:不同类型不允许直接运算)例如 python
- 一、变量1.变量Python 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在 Python 中,变量就是变
- PyQt5是python中一个非常实用的GUI编程模块,功能十分强大。刚刚学完了Pyqt的编程,就迫不及待的写出了一个电子词典GUI程序。整
- 最近为了熟悉一下 js 用有道翻译练了一下手,写一篇博客记录一下,也希望能对大家有所启迪,不过这些网站更新太快,可能大家尝试的时候会有所不同
- 引子Matlab中有一个函数叫做find,可以很方便地寻找数组内特定元素的下标,即:Find indices and values of n
- python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,那可以用下面的方法
- 一、Pycharm下载与安装附:Python、Pycharm和Anaconda的关系:Python是一种解释型、面向对象、动态数据类型的高级
- asp函数代码 代码如下:<% Function RemoveHTML(str) Dim objRegExp, Match,strHT
- 问题在一个python web应用中需要定时执行一些任务,所以用了APScheduler这个库。又因为是用flask这个web框架,所以用了
- Socket有一个缓冲区,缓冲区是一个流,先进先出,发送和取出的可自定义大小的,如果取出的数据未取完缓冲区,则可能存在数据怠慢。其中【rec
- 有时候使用到获取本机IP,就采用以下方式进行。#!/usr/bin/python import socketimport stru
- php数组中元素的存在方式是以键值对的方式('key'=>'value'),有时候我们需要根据键删除数
- 本文实例讲述了Python通过调用mysql存储过程实现更新数据功能。分享给大家供大家参考,具体如下:一、需求分析由于管理费率配置错误,生成
- Blog Posts的提交让我们从简单的开始。首页上必须有一张用户提交新的post的表单。首先我们定义一个单域表单对象(fileapp/fo
- Go 单元测试工具测试分为4个层次单元测试:对代码进行测试集成测试:对一个服务的接口测试端到端测试(链路测试):从一个链路的入口输入测试用例
- 前言 一直用ASP+ACCESS来编写网页和公司的内部应用系统,内部应用系统也就是大家说的OA吧,这个我也不知道,公司又叫它ERP,反正不管