Python 用于图像去模糊的维纳滤波器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35192550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wiener Filter for image deblur
提问by yc2986
I am trying to implement the Wiener Filter to perform deconvolution on blurred image. My implementation is like this
我正在尝试实现 Wiener Filter 以对模糊图像执行反卷积。我的实现是这样的
import numpy as np
from numpy.fft import fft2, ifft2
def wiener_filter(img, kernel, K = 10):
dummy = np.copy(img)
kernel = np.pad(kernel, [(0, dummy.shape[0] - kernel.shape[0]), (0, dummy.shape[1] - kernel.shape[1])], 'constant')
# Fourier Transform
dummy = fft2(dummy)
kernel = fft2(kernel)
kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K)
dummy = dummy * kernel
dummy = np.abs(ifft2(dummy))
return np.uint8(dummy)
This implementation is based on the Wiki Page.
此实现基于Wiki 页面。
The TIFF image used is from : http://www.ece.rice.edu/~wakin/images/lena512color.tiff
But here is a PNG version:
使用的 TIFF 图像来自:http: //www.ece.rice.edu/~wakin/images/lena512color.tiff
但这里是 PNG 版本:
I have a input image motion blurred by a diagonal kernel and some gaussian additive noise is added to it. The lena picture is 512x512 and the blurring kernel is 11x11.
我有一个被对角内核模糊的输入图像运动,并且添加了一些高斯加性噪声。lena图片为512x512,模糊内核为11x11。
When I apply my wiener_filter to this image the result is like this.
.
当我将 wiener_filter 应用于此图像时,结果是这样的。
.
I think this deblurred image is not of good quality. So I would like to ask if my implementation is correct.
我认为这张去模糊的图像质量不好。所以我想问一下我的实现是否正确。
Updatethe way I add noise.
更新我添加噪音的方式。
from scipy.signal import gaussian, convolve2d
def blur(img, mode = 'box', block_size = 3):
# mode = 'box' or 'gaussian' or 'motion'
dummy = np.copy(img)
if mode == 'box':
h = np.ones((block_size, block_size)) / block_size ** 2
elif mode == 'gaussian':
h = gaussian(block_size, block_size / 3).reshape(block_size, 1)
h = np.dot(h, h.transpose())
h /= np.sum(h)
elif mode == 'motion':
h = np.eye(block_size) / block_size
dummy = convolve2d(dummy, h, mode = 'valid')
return np.uint8(dummy), h
def gaussian_add(img, sigma = 5):
dummy = np.copy(img).astype(float)
gauss = np.random.normal(0, sigma, np.shape(img))
# Additive Noise
dummy = np.round(gauss + dummy)
# Saturate lower bound
dummy[np.where(dummy < 0)] = 0
# Saturate upper bound
dummy[np.where(dummy > 255)] = 255
return np.uint8(dummy)
回答by tfv
For data comparison, you can find a sample implementation of Wiener filtering and unsupervisived Wiener filtering at
对于数据比较,您可以在以下位置找到 Wiener 过滤和无监督 Wiener 过滤的示例实现
http://scikit-image.org/docs/dev/auto_examples/plot_restoration.html
http://scikit-image.org/docs/dev/auto_examples/plot_restoration.html
If you give your original image data, we may be able to help further.
如果您提供原始图像数据,我们或许可以提供进一步帮助。
EDIT: Original link seems to be down, try this one: http://scikit-image.org/docs/dev/auto_examples/filters/plot_restoration.html
编辑:原始链接似乎已关闭,请尝试此链接:http: //scikit-image.org/docs/dev/auto_examples/filters/plot_restoration.html
回答by gsamaras
Use skimage.restoration.wiener, which is usually used like:
使用skimage.restoration.wiener,通常使用如下:
>>> from skimage import color, data, restoration
>>> img = color.rgb2gray(data.astronaut())
>>> from scipy.signal import convolve2d
>>> psf = np.ones((5, 5)) / 25
>>> img = convolve2d(img, psf, 'same')
>>> img += 0.1 * img.std() * np.random.standard_normal(img.shape)
>>> deconvolved_img = restoration.wiener(img, psf, 1100)
I have also used it in: Deblur an image using scikit-image.
回答by Sandipan Dey
We could try unsupervised weiner too (deconvolution with a Wiener-Hunt approach, where the hyperparameters are automatically estimated, using a stochastic iterative process (Gibbs sampler), as described here):
我们可以尝试无监督韦纳太(解卷积维纳-亨特的方法,其中超参数自动估计,使用随机迭代过程(Gibbs采样),如所描述这里):
deconvolved, _ = restoration.unsupervised_wiener(im, psf)