5
$\begingroup$

I am trying to denoise many several noises with several filters for a research i have, i found a deconvolution Wiener filter made by "mr.tranleanh" on Github, as you can see here .

what I did is that I canceled the blurring part in the code and only add Gaussian noise to my images, and I made a PSNR calculation each time I apply the filter, and for each time I was increasing the size of the Gaussian kernel the PSNR value is getting bigger, so that's mean that the noise is being reduced, and all that without adding blurring to the image.

I am wondering whether I can use the Wiener filter for reducing noise in this way (without blurring).

  • Can a deconvolution Wiener filter reduce noise without blurring?
  • Why does the image get darker each time I apply the Wiener filter?

Shortly this is how I wrote the code:

def add_gaussian_noise(img, sigma):
    gauss = np.random.normal(0, sigma, np.shape(img))
    noisy_img = img + gauss
    noisy_img[noisy_img < 0] = 0
    noisy_img[noisy_img > 255] = 255
    return noisy_img
def wiener_filter(img, kernel, K):
    kernel /= np.sum(kernel)
    dummy = np.copy(img)
    dummy = fft2(dummy)
    kernel = fft2(kernel, s = img.shape)
    kernel = np.conj(kernel) / (np.abs(kernel) ** 2 + K)
    dummy = dummy * kernel
    dummy = np.abs(ifft2(dummy))
    return dummy
def gaussian_kernel(kernel_size = 3):
    h = gaussian(kernel_size, kernel_size / 3).reshape(kernel_size, 1)
    h = np.dot(h, h.transpose())
    h /= np.sum(h)
    return h
def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
if __name__ == '__main__':
    # Load image and convert it to gray scale
    file_name = os.path.join('/content/Hand.jpeg') 
    img = rgb2gray(plt.imread(file_name))
    # Add Gaussian noise
    noisy_img = add_gaussian_noise(blurred_img, sigma = 20)
    # Apply Wiener Filter
    kernel = gaussian_kernel(3)
    filtered_img = wiener_filter(noisy_img, kernel, K = 10)

edited :

the type of images I want to apply the wiener filter on :

they are just normal gray images, but I will add salt&pepper noise by this code :

   import cv2

  def add_noise(img):

row , col = img.shape
number_of_pixels = random.randint(300, 10000 )
for i in range(number_of_pixels):
    y_coord=random.randint(0, row - 1)
    
    x_coord=random.randint(0, col - 1)
    
    img[y_coord][x_coord] = 255
    
number_of_pixels = random.randint(300 , 10000)
for i in range(number_of_pixels):

    y_coord=random.randint(0, row - 1)
    x_coord=random.randint(0, col - 1)
    img[y_coord][x_coord] = 0
return img
img = cv2.imread('/content/Hand.jpeg',
                cv2.IMREAD_GRAYSCALE)
cv2.imwrite('s&p12spe.jpg', add_noise(img))

the full code for adding salt&pepper noise source in case you want more explanation.

again I just want to denoise salt&pepper noise via Wiener filter, and I know salt&pepper noise is denoised via other filters like median filter, but am doing this for research purposes, and the only code I found for the Wiener filter is the one I uploaded here in the question, i don't know if more information can help you, but thanks anyway

$\endgroup$
3
  • $\begingroup$ What type of images are you working on? Could you share some examples? $\endgroup$
    – Royi
    May 1, 2022 at 11:56
  • $\begingroup$ @Royi , my purpose is to use the code I write up in the question for denoising medical images being affected with a noise like salt&pepper noise, and I thought that if i cancel the blurring part, the code then could de-noise images with salt&pepper noise, am working with grayscale medical images, and i add salt&pepper noise to these images, i don't what specifically you want to know about the images am working with, could you specifies what examples you want to see, thanks for you reply $\endgroup$
    – kode224
    May 1, 2022 at 18:43
  • $\begingroup$ @Royi , i made an edit for the question where I added more information, I don't know if it will help you, but thanks anyway $\endgroup$
    – kode224
    May 1, 2022 at 18:54

2 Answers 2

2
$\begingroup$

For Salt and Pepper noise on medical or real world images using the Wiener Filter isn't recommended.

The Wiener filter basically takes advantage only on the knowledge from the spectrum of the data.
The spectrum is global while the data of those kind of images is very local: Edges, Contrast, Texture, etc...

Hence a much better approach would be using local filters.
You may try the 2D Median filter which is a classic solution for the Salt and Pepper noise model. You may also use the Bilateral Filter with some tweaking for this kind of noise.

Basically, any Edge Preserving Filter will do much better preserving the details while suppressing noise.

$\endgroup$
11
  • $\begingroup$ thanks, sir for your answer, but already said it's for the research I have, I know that for salt&pepper noise we must use a filter like the median filter, but in my case, I want to apply a Wiener filter on an image with salt&pepper noise to test the wiener filter efficiency in reducing that noise. $\endgroup$
    – kode224
    May 3, 2022 at 10:14
  • $\begingroup$ So what's the question? If it can remove without blurring? No. Exactly because what I wrote, it works globally without any adaptation to content but its spectrum. $\endgroup$
    – Royi
    May 3, 2022 at 10:17
  • $\begingroup$ the question was: is that can I use the deconvolution wiener filter for reducing noise like salt&pepper $\endgroup$
    – kode224
    May 3, 2022 at 10:24
  • $\begingroup$ @kode224, I wrote everything. You can, but it won't be effective as it is global while the noise is local. $\endgroup$
    – Royi
    May 3, 2022 at 10:51
  • 1
    $\begingroup$ @kode224, Of course you can ask. Please. $\endgroup$
    – Royi
    May 3, 2022 at 11:35
3
$\begingroup$

Can a deconvolution Wiener filter reduce noise without blurring?

Maybe. Maybe not. There is not one Wiener filter. Any concrete "Wiener filter" is a plain old filter that has been designed using the algorithm for defining a filter laid out by Norbert Weiner, and given exact descriptions of the underlying signal's spectrum, and the noise.

You can, within limitations, choose the filter you want, then choose noise and signal characteristics that will result in that filter. If you want a high-pass filter (or at least a high frequency boost filter), choose a signal with no DC content, and white noise. You'll get an edge-enhance filter. If you want a low-pass filter, choose a signal with little high frequency content, and white noise. You'll get a filter that blurs. If you want some random filter, choose your signal and your noise randomly.

Why does the image get darker each time I apply the Wiener filter?

Because when you randomly chose a signal characteristic that's almost white, and white noise, the result is a filter that attenuates everything.


What you are missing in your design process is this: Weiner filters are only optimal if the noise is Gaussian and stationary and known, and if the signal acts as if it were generated by a known linear system excited by Gaussian white noise, and if the optimality criterion is mean-squared error.

In image processing, the signal statistics are almost never act like they were generated by a linear system excited by Gaussian white noise, the optimality criterion is almost never mean-squared error (although sometimes it's better to pretend so). In the case of salt-and-pepper noise, the signal statistics are most definitely not Gaussian, although they may act like they're generated by some stationary process.

So the choice of a Wiener filter to enhance an arbitrary image that's beset by salt-and-pepper noise does not check any of the boxes for using a Wiener filter.

In fact, most of the check boxes for any linear filter at all remain empty when you're looking at salt-and-pepper noise. And, indeed, the filter that ends up getting used most often -- the median filter -- is not a linear filter, and it works pretty well.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.