I recently used a Gaussian convolution to blur an image. It works well, in fact it works excellently. However, now I'm trying to reverse it and my inverse filter keeps running into problems. The filter is supposed to run on the following principle:
G(x, y) / H(x, y) = F(x, y) where G(x, y) is the Fourier transform of the blurred image, H(x, y) is the Fourier transform of the blurring function and F(x, y) is the Fourier transform of the original image.
Currently, the resulting image looks exactly the same as the original. My algorithm is as follows:
from PIL import Image
import math
import cmath
import numpy as np
def reverseGaussianBlur(picture, r):
rs = int(math.ceil(r * 2.57) # Calculate significant radius
w, h = picture.size
pixels = list(picture.getdata()) # Image's pixels as list
fft_pixels = runFourier(pixels, False) # Run FFT
temp_picture = []
for u in range(0, h):
for v in range(0, w):
val = [0] * 3
wsum = 0
for iy in range(u - rs, u + rs + 1):
y = min(h - 1, max(0, iy))
for ix in range(v - rs, v + rs + 1):
x = min(w - 1, max(0, ix))
weight = (2 * math.pi) ** 0.5 * cmath.exp(-r * r * ((ix - v) *
(ix - v) + (iy - u) * (iy - u)) / 2)
if (weight.real > 1e-5):
val = [n + p / weight for n, p in zip(val, fft_pixels[y * w + x])]
wsum += weight
temp_picture.append(tuple([v * wsum for v in val]))
return_picture = [tuple(int(round(p)) for p in pixel) for pixel in
runFourier(temp_picture, True)] # Run Inverse FFT
return return_picture
Anyway, I'm not quite sure what's wrong and any help would be great.
When you blur an image, you're basically removing the high frequency components. They're gone. There is no reverse filter. If you had applied a "filter" that took each pixel and replaced it with flat white, you wouldn't expect there to be a reverse filter for that, because all the details (except the size of the the original image) are lost. It's the same thing with blurring, only you've lost just the high frequency components.