python實(shí)現(xiàn)逆濾波與維納濾波示例
構(gòu)建運(yùn)動(dòng)模糊模型
現(xiàn)假定相機(jī)不動(dòng),圖像f(x,y)在圖像面上移動(dòng)并且圖像f(x,y)除移動(dòng)外不隨時(shí)間變化。令x0(t)和y0(t)分別代表位移的x分量和y分量,那么在快門開啟的時(shí)間T內(nèi),膠片上某點(diǎn)的總曝光量是圖像在移動(dòng)過程中一系列相應(yīng)像素的亮度對(duì)該點(diǎn)作用之總和。也就是說,運(yùn)動(dòng)模糊圖像是由同一圖像在產(chǎn)生距離延遲后與原圖像想疊加而成。如果快門開啟與關(guān)閉的時(shí)間忽略不計(jì),則有:

由于各種運(yùn)動(dòng)都是勻速直線運(yùn)動(dòng)的疊加,因而我們只需考慮勻速直線運(yùn)動(dòng)即可。但由于我們自身水平有限,且旨在探討找到實(shí)現(xiàn)運(yùn)動(dòng)模糊復(fù)原方法的思想與方向,因而我們未能自行構(gòu)建模型,而是借鑒了參考文獻(xiàn)[1]中建立的運(yùn)動(dòng)模糊模型。關(guān)于本模型的理論依據(jù)參見參考文獻(xiàn)[1].
下面我們描述一下該模型函數(shù)motion_process(image_size,motion_angle),它包含兩個(gè)參數(shù):圖像的尺寸大小image_size以及運(yùn)動(dòng)的角度motion_angle。
例如,當(dāng)運(yùn)動(dòng)位移為9、運(yùn)動(dòng)角度為45度時(shí),則該模型函數(shù)的構(gòu)建過程如下:
1. 首先是創(chuàng)建與圖像同等大小的全0矩陣,然后找到全0矩陣的中心行數(shù)center_position,再計(jì)算出運(yùn)動(dòng)角度的tan值與cot值,算出運(yùn)動(dòng)的偏移量offset。
2.
PSF[int(center_position+offset),int(center_position-offset)]=1
3.
PSF[int(center_position-offset),int(center_position+offset)]=1
則該模型對(duì)應(yīng)的圖像如下圖所示:

運(yùn)動(dòng)位移為9,運(yùn)動(dòng)角度分別為45°、30°、60°時(shí),運(yùn)動(dòng)模糊模型對(duì)應(yīng)的圖像
import matplotlib.pyplot as graph
import numpy as np
from numpy import fft
import math
import cv2
# 仿真運(yùn)動(dòng)模糊
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() #對(duì)點(diǎn)擴(kuò)散函數(shù)進(jìn)行歸一化亮度
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()
#對(duì)圖片進(jìn)行運(yùn)動(dòng)模糊
def make_blurred(input, PSF, eps):
input_fft = fft.fft2(input)# 進(jìn)行二維數(shù)組的傅里葉變換
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) #計(jì)算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()
#進(jìn)行運(yùn)動(dòng)模糊處理
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產(chǎn)生隨機(jī)的函數(shù)
graph.subplot(234)
graph.xlabel("motion & noisy blurred")
graph.imshow(blurred_noisy) #顯示添加噪聲且運(yùn)動(dòng)模糊的圖像
result = inverse(blurred_noisy, PSF, 0.1+1e-3) #對(duì)添加噪聲的圖像進(jìn)行逆濾波
graph.subplot(235)
graph.xlabel("inverse deblurred")
graph.imshow(result)
result=wiener(blurred_noisy,PSF,0.1+1e-3) #對(duì)添加噪聲的圖像進(jìn)行維納濾波
graph.subplot(236)
graph.xlabel("wiener deblurred(k=0.01)")
graph.imshow(result)
graph.show()
參考文獻(xiàn)
[1] 何紅英. 運(yùn)動(dòng)模糊圖像恢復(fù)算法的研究與實(shí)現(xiàn)[D]. 西安科技大學(xué)碩士學(xué)位論文. 2011.
[2] Rafael C.Gonzalez,Richard E.Woods,Steven L.Eddins. 數(shù)字圖像處理的MATLAB實(shí)現(xiàn)(第2版)[M]. 阮秋琦,譯. 北京:清華大學(xué)出版社,2013.
[3] 陳建功. 運(yùn)動(dòng)模糊圖像復(fù)原算法研究[D]. 南昌航空大學(xué)碩士學(xué)位論文. 2012.
以上這篇python實(shí)現(xiàn)逆濾波與維納濾波示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 調(diào)試器pdb的簡(jiǎn)單使用
這篇文章主要介紹了python 調(diào)試器pdb的簡(jiǎn)單使用,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03
使用Python程序抓取新浪在國內(nèi)的所有IP的教程
這篇文章主要介紹了使用Python程序抓取新浪在國內(nèi)的所有IP的教程,作為Python網(wǎng)絡(luò)編程中獲取IP的一個(gè)小實(shí)踐,需要的朋友可以參考下2015-05-05
tensorflow 2.0模式下訓(xùn)練的模型轉(zhuǎn)成 tf1.x 版本的pb模型實(shí)例
這篇文章主要介紹了tensorflow 2.0模式下訓(xùn)練的模型轉(zhuǎn)成 tf1.x 版本的pb模型實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想過來看看吧2020-06-06
Python函數(shù)式編程之返回函數(shù)實(shí)例詳解
函數(shù)式編程的一個(gè)特點(diǎn)就是,允許把函數(shù)本身作為參數(shù)傳入另一個(gè)函數(shù),還允許返回一個(gè)函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python函數(shù)式編程之返回函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-09-09
Pycharm終端顯示PS而不顯示虛擬環(huán)境名的解決
這篇文章主要介紹了Pycharm終端顯示PS而不顯示虛擬環(huán)境名的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Pandas 類型轉(zhuǎn)換astype()的實(shí)現(xiàn)
本文主要介紹了Pandas 類型轉(zhuǎn)換astype()的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
基于Python3 逗號(hào)代碼 和 字符圖網(wǎng)格(詳談)
下面小編就為大家?guī)硪黄赑ython3 逗號(hào)代碼 和 字符圖網(wǎng)格(詳談)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

