维纳滤波器输出看起来不够好
Wiener filter output doesn't look good enough
我有一张图片,我在其中添加了一些噪声并尝试使用维纳滤波器对其进行去噪:
在加性白噪声和无模糊的情况下简化为:
这是我根据上面的公式编写的代码,但是,与输入图像几乎没有区别。
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)
这是地面实况图像:
这是输入:
这是输出:
以下在 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)
Noise=100000000 的恢复结果:
Noise=500000000 的恢复结果:
Noise=1000000000 的恢复结果:
噪音=5000000000 的恢复结果:
我有一张图片,我在其中添加了一些噪声并尝试使用维纳滤波器对其进行去噪:
在加性白噪声和无模糊的情况下简化为:
这是我根据上面的公式编写的代码,但是,与输入图像几乎没有区别。
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)
这是地面实况图像:
这是输入:
这是输出:
以下在 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)
Noise=100000000 的恢复结果:
Noise=500000000 的恢复结果:
Noise=1000000000 的恢复结果:
噪音=5000000000 的恢复结果: