维纳滤波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度時,則該模型函數的構建過程如下:
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.
以上這篇python實現逆濾波與維納濾波示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。
總結
以上是生活随笔為你收集整理的维纳滤波python 函数_python实现逆滤波与维纳滤波示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原理图的绘制
- 下一篇: Arduino驱动IIC/I2C LCD