Hi guys greetings
am trying to denoise a simple blurred image by this wiener filter code that I found online, according to my knowledge that the Wiener filter is a deconvolution method, and when the image is blurred by a known lowpass filter, it is possible to recover the image by inverse filtering or generalized inverse filtering, so I used a (1/16)[1] blurring kernel but my code still not work.
wiener filter code:
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
the way that I wrote the code by using (1/16)[1] blurring kernel and applying the wiener filter :
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("Hand.jpeg")
kernel = (1/16) * np.array([[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]])
wiener_filterr(img, kernel, K = 10)
I know that this may be a simple task for others and dont require a hole question on stackOF, but am really struggling to solve it, and thanks in regard
edited
the error message :
ValueError Traceback (most recent call last)
<ipython-input-19-a245135dd767> in <module>()
6 [1,1,1,1,1],
7 [1,1,1,1,1]])
----> 8 wiener_filter(img, kernel, K = 10)
3 frames
<__array_function__ internals> in fft2(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/numpy/fft/_pocketfft.py in _cook_nd_args(a, s, axes, invreal)
693 axes = list(range(-len(s), 0))
694 if len(s) != len(axes):
--> 695 raise ValueError("Shape and axes have different lengths.")
696 if invreal and shapeless:
697 s[-1] = (a.shape[axes[-1]] - 1) * 2
ValueError: Shape and axes have different lengths.