问题描述
我正在尝试实施维纳滤波器以对模糊图像执行反卷积。我的实现是这样的
从numpy.fft导入numpy作为np
导入fft2,ifft2
def wiener_filter(img,kernel,K = 10):
dummy = np.copy(img)
kernel = np.pad(内核,[(0,dummy.shape [0]- kernel.shape [0]),(0,dummy.shape [1]-kernel.shape [1])],'constant')
#傅立叶变换
dummy = fft2(dummy)
内核= fft2(内核)
内核= np.conj(内核)/(np.abs(内核)** 2 + K)
虚拟=虚拟*内核
虚拟= np .abs(ifft2(dummy))
return np.uint8(dummy)
此实现基于。
我认为这个去模糊的图像质量不好。所以我想问一下我的实现是否正确。
更新我加噪声的方式。
从scipy.signal导入高斯,convolve2d
def blur(img,mode ='box',block_size = 3):
#mode ='box'或'gaussian'或'motion'
虚拟= np.copy(img)
如果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
虚拟= convolve2d(dummy,h,mode ='valid')
return np.uint8(dummy),h
def gaussian_add(img,sigma = 5):
虚拟= np.copy(img).astype(float)
高斯= np.random.normal(0,sigma,np.shape(img))
#加性噪声
虚拟= np.round (高斯+虚拟)
#饱和下界
虚拟[np.where(dummy< 0)] = 0
#饱和上限
dummy [np.where(dummy> 255)] = 255
return np.uint8(dummy)
使用,通常这样使用:
>>从skimage导入颜色,数据,恢复
>> img = color.rgb2gray(data.astronaut())
>>从scipy.signal导入卷积
>> psf = np.ones((5,5))/ 25
>> img = convolve2d(img,psf,相同)
>>> img + = 0.1 * img.std()* np.random.standard_normal(img.shape)
>> deconvolved_img = recovery.wiener(img,psf,1100)
我也用过它:。
I am trying to implement the Wiener Filter to perform deconvolution on blurred image. My implementation is like this
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.
The TIFF image used is from : http://www.ece.rice.edu/~wakin/images/lena512color.tiff
But here is a PNG version:
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.
When I apply my wiener_filter to this image the result is like this..
I think this deblurred image is not of good quality. So I would like to ask if my implementation is correct.
Update the 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)
Use skimage.restoration.wiener, which is usually used like:
>>> 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.
这篇关于维纳滤镜用于图像去模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!