溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用python實現(xiàn)逆濾波與維納濾波示例

發(fā)布時間:2021-03-23 12:42:52 來源:億速云 閱讀:799 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下怎么使用python實現(xiàn)逆濾波與維納濾波示例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

構(gòu)建運動模糊模型

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

怎么使用python實現(xiàn)逆濾波與維納濾波示例

由于各種運動都是勻速直線運動的疊加,因而我們只需考慮勻速直線運動即可。但由于我們自身水平有限,且旨在探討找到實現(xiàn)運動模糊復(fù)原方法的思想與方向,因而我們未能自行構(gòu)建模型,而是借鑒了參考文獻(xiàn)[1]中建立的運動模糊模型。關(guān)于本模型的理論依據(jù)參見參考文獻(xiàn)[1].

下面我們描述一下該模型函數(shù)motion_process(image_size,motion_angle),它包含兩個參數(shù):圖像的尺寸大小image_size以及運動的角度motion_angle。

例如,當(dāng)運動位移為9、運動角度為45度時,則該模型函數(shù)的構(gòu)建過程如下:

1. 首先是創(chuàng)建與圖像同等大小的全0矩陣,然后找到全0矩陣的中心行數(shù)center_position,再計算出運動角度的tan值與cot值,算出運動的偏移量offset。

2. 怎么使用python實現(xiàn)逆濾波與維納濾波示例PSF[int(center_position+offset),int(center_position-offset)]=1

3. 怎么使用python實現(xiàn)逆濾波與維納濾波示例PSF[int(center_position-offset),int(center_position+offset)]=1

則該模型對應(yīng)的圖像如下圖所示:

怎么使用python實現(xiàn)逆濾波與維納濾波示例

運動位移為9,運動角度分別為45°、30°、60°時,運動模糊模型對應(yīng)的圖像

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() #對點擴(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()
 
#對圖片進(jìn)行運動模糊
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) #計算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)行運動模糊處理
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)  #顯示添加噪聲且運動模糊的圖像
 
result = inverse(blurred_noisy, PSF, 0.1+1e-3) #對添加噪聲的圖像進(jìn)行逆濾波
graph.subplot(235)
graph.xlabel("inverse deblurred")
graph.imshow(result)
 
result=wiener(blurred_noisy,PSF,0.1+1e-3)   #對添加噪聲的圖像進(jìn)行維納濾波
graph.subplot(236)
graph.xlabel("wiener deblurred(k=0.01)")
graph.imshow(result)
 
graph.show()

以上是“怎么使用python實現(xiàn)逆濾波與維納濾波示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI