python实现逆滤波与维纳滤波

python实现逆滤波与维纳滤波
构建运动模糊模型
现假定相机不动,图像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度时,则该模型函数的构建过程如下:痰浊
⾸先是创建与图像同等⼤⼩的全0矩阵,然后到全0矩阵的中⼼⾏数center_position,再计算出运动⾓度的tan值与cot值,算出运动的偏移量offset。
再令α≤45°时,PSF[int(center_position+offset),int(center_position-offset)]=1
α≥45°时,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)
酷我音乐盒2008PSF_fft=fft.fft2(PSF) +eps
PSF_fft_j(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.imshow(image)    #显⽰原图像
graph.figure(2)
#进⾏运动模糊处理
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.

本文发布于:2024-09-22 18:20:15,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/477285.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:运动   图像   模型   实现   时间   找到   噪声   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议