我有一張影像,我給它添加了一些噪音,并嘗試使用維納濾波器對其進行降噪:

在加性白噪聲且沒有模糊的情況下,簡化為:

這是我根據上述公式撰寫的代碼,但是,與輸入影像幾乎沒有區別。
import cv2
import numpy as np
img=cv2.imread('Images/P3.jpg',0)
freq2 = np.fft.fft2(img)
mean = 0
var = 100
sigma = var**0.5
gauss = np.random.normal(mean,sigma,np.shape(img))
courrupted=img gauss
freq2h = np.fft.fft2(gauss)
courrupted[courrupted<0]=0
courrupted[courrupted>255]=255
courrupted=courrupted.astype(np.uint8)
crfre=np.fft.fftshift(np.fft.fft2(courrupted))
sf=np.abs(crfre)**2
wiener=sf/(sf (100))
F_hat = crfre*wiener
f_hat = np.fft.ifft2( (F_hat))
restored = abs(f_hat)
normalizedImg=np.ones(img.shape)
normalizedImg = cv2.normalize(restored, normalizedImg, 0, 255, cv2.NORM_MINMAX)
cv2.imwrite('output.jpg',normalizedImg)
cv2.imwrite('input.jpg',courrupted)
這是地面實況影像:

這是輸入:

這是輸出:

uj5u.com熱心網友回復:
以下在 Python/OpenCV/Numpy 中對我有用。正如@Cris Luengo 建議的那樣,您需要測驗噪聲值的各種值,因為您需要的值可能不完全是您的高斯方差輸入值。
輸入:

import cv2
import numpy as np
# read image as grayscale
img = cv2.imread('pandas_noisy.jpg',0)
# take dft
dft = np.fft.fft2(img)
# get power spectral density of dft = square of magnitude
# where abs of complex number is the magnitude
pspec = (np.abs(dft))**2
print(np.amin(pspec))
print(np.amax(pspec))
# estimate noise power spectral density
# try different values to achieve compromise between noise reduction and softening/blurring
#noise = 100000000
#noise = 500000000
#noise = 1000000000
noise = 5000000000
# do wiener filtering
wiener = pspec/(pspec noise)
wiener = wiener*dft
# do dft to restore
restored = np.fft.ifft2(wiener)
# take real() component (or do abs())
restored = np.real(restored)
print(np.amin(restored))
print(np.amax(restored))
# clip and convert to uint8
restored = restored.clip(0,255).astype(np.uint8)
# save results
#cv2.imwrite('pandas_noisy_restored_100000000.jpg',restored)
#cv2.imwrite('pandas_noisy_restored_500000000.jpg',restored)
#cv2.imwrite('pandas_noisy_restored_1000000000.jpg',restored)
cv2.imwrite('pandas_noisy_restored_5000000000.jpg',restored)
# display results
cv2.imshow("input", img)
cv2.imshow("restored", restored)
cv2.waitKey(0)
噪聲 = 100000000 的恢復結果:

噪聲 = 500000000 的恢復結果:

噪聲 = 1000000000 的恢復結果:

噪聲 = 5000000000 的恢復結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/484524.html
