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)過(guò)程中一系列相應(yīng)像素的亮度對(duì)該點(diǎn)作用之總和。也就是說(shuō),運(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)建過(guò)程如下:
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 graphimport numpy as npfrom numpy import fftimport mathimport 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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. nestjs實(shí)現(xiàn)圖形校驗(yàn)和單點(diǎn)登錄的示例代碼4. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式5. python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能6. python開發(fā)飛機(jī)大戰(zhàn)游戲7. laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)8. css進(jìn)階學(xué)習(xí) 選擇符9. Echarts通過(guò)dataset數(shù)據(jù)集實(shí)現(xiàn)創(chuàng)建單軸散點(diǎn)圖10. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))
