IMAGE FILTERING USING FOURIER ANALYSIS

CHARLES SPRAGUE

ANDREW PIPER

Using fourier analysis within Sage to decompose an image into its fourier modes and use a linear filter to remove as much 'noise' as possible while retaining an acceptable amount image quality. By using a 2 dimensional fourier transform the dominant modes of the image become apparent and a specific filter can be applied to reduce the amount of noise to near zero creating a somewhat cleaner looking image.

{{{id=24| import numpy import pylab import warnings warnings.filterwarnings("ignore") /// }}}

This is a demonstration of the techniques used in our image denoiser. The images below are, respectively, a grainy image, the log of the absolute value of the Fourier Transform of that image, and the Fourier Transform after the filter is applied.

{{{id=23| e = numpy.mean(pylab.imread(DATA + 'derek3.png'), 2) ef = pylab.fftshift(pylab.fft2(e)) mgrid1 = numpy.lib.index_tricks.nd_grid() kx, ky = mgrid1[0:e.shape[0], 0:e.shape[1]] efilt = pylab.multiply(ef,exp(-.0001*(kx-e.shape[0]/2)**2 - .0001*(ky-e.shape[1]/2)**2)) g = graphics_array([matrix_plot(e), matrix_plot(log(abs(ef)), cmap='hot'), matrix_plot(log(abs(efilt)), cmap='hot')]) show(g, axes=False, frame=false, figsize=(14,7)) /// }}}

This is the Gaussian Filter that was applied to the Fourier Transform

{{{id=37| matrix_plot(exp(-.0001*(kx-e.shape[0]/2)**2 - .0001*(ky-e.shape[1]/2)**2), frame=false) /// }}}

This is the function that performs the Fourier Transform, applies the filter, and then reverts it back to a denoised image which is then returned.

{{{id=19| def denoise(img, i, j): # Does a fast, 2-dimensional Fourier Transform, then shifts the quadrants of the matrix im = pylab.fftshift(pylab.fft2(img)) # Creates a meshgrid for use in the filter mgrid = numpy.lib.index_tricks.nd_grid() kx, ky = mgrid[0:img.shape[0], 0:img.shape[1]] # The midpoints with respect to the x and y dimensions of the image matrix for use in the filter x_mid = img.shape[0]/2 y_mid = img.shape[1]/2 # The desired widths for the filter wx = 1/i wy = 1/j # The filter used to remove noise from the Fourier Transform of the image filter = exp(-wy*(kx-x_mid)**2 - wx*(ky-y_mid)**2) # Applying the filter to the Fourier Transform imfilt = pylab.multiply(im,filter) # Reverses the previous shift on the quadrants and inverses the Fourier Transform to get the filtered image imf = pylab.ifft2(pylab.ifftshift(imfilt)) return imf /// }}}

This cell intakes the image, converts it to black and white, then displays a graphic containing, from right to left, a noisy version of the original image, the orignal image, and the noisy image after being denoised.

{{{id=26| # Imports the image we wish to denoise and converts it to black and white with numpy.mean() img = numpy.mean(pylab.imread(DATA + 'climb.png'), 2) a = numpy.random.random_sample((img.shape[0], img.shape[1])) img_dirty = img + a; matrix_plot(e) # Displays the image and lets us change the filter width @interact def img_filt(i = ("Horizontal Filter Width (1/x) x=", (1000,(100..10000))), j = ("Vertical Filter Width (1/y) y=", (1000,(100..10000)))): imf = denoise(img_dirty, i, j) g = graphics_array([matrix_plot(imf), matrix_plot(img), matrix_plot(img_dirty)]) show(g, frame=false, axes=false, figsize=(14,7)) ///
Horizontal Filter Width (1/x) x= 
Vertical Filter Width (1/y) y= 
}}}

MOTIVATION


  The idea for this project came mostly from wanting to do something that while useful was still fun to implement.  The really useful thing about this particular technique for image filtering is that it can be used to filter more than just images.  Finding the important and more prominent points in two-dimensional  or one-dimensional frequency data can be achieved in a very similar way with little changes needing to be made to the underlying code of the process.  We was also curious how easily one could take code written in Matlab and adapt it for use in sage and perhaps even speed it up. Rewriting the code to filter images in sage was rather straightforward with the use of numpy and pylab.  Any issues that did arise were fixed rather easily with a little google searching or reading the source code for various functions.

BACKGROUND

    When dealing with recorded information, such as that of sound or images, there is always an amount of “noise” included in the recording. Noise is information that is unrelated to the actual data being recorded, but is included through the process by which the information was recorded. In digital images, noise manifests itself in the form of seemingly random pixels that do not appear to coincide with the surrounding pixels. This may make an image appear “grainy”, or a sound may contain “white noise” or hiss. Noise can originate at several stages in data recording, such as faulty or dirty equipment, the processing algorithms in digital devices, or limits in the precision of recording medium. Our project only concerns digital images, and so I will not discuss recorded sound further, although much of this information applies to recorded sound as well. It is at times beneficial to reduce the amount of noise contained in an image; this can make an image more aesthetically pleasing, make objects more visible, or make an image easier to process with a computer. There are many methods of “denoising” an image, however, they all inevitably remove information beyond that of just noise. In order to remove noise, a program has to have a method of removing unimportant information, and no matter the method, important information is removed along with the noise. This usually involves converting the image to a form where unimportant information is more obvious, filtering out this information, and then reverting the image back to its original form.  When performing this operation it can still be difficult separate important information from noise, and edges and fine details are susceptible to blurring, and if noise is removed too aggressively, these features may be removed altogether. By varying the amount of noise removed, it is possible to preserve important structures while at the same time removing an acceptable amount of noise. Of the many methods of denoising, we chose to use a linear filter of the Fourier Transform of the image.


ALGORITHM

  The main process behind the image filtering is converting the image into its Fourier modes to determine where the bulk of the important information regarding the image is located.  When this is plotted the Fourier modes are seen to mainly be around the center of the plot of the Fourier modes while the rest of plot is just noise.  Using this information we can then use certain filters to single out the bulk of the image and remove the parts of the image that do not matter significantly.  The filter that we chose to use was a Gaussian filter constructed to be very close to zero except for at the center of the image.  When the filter is then applied to the center of the image the noise is removed from being multiplied by zero (or very near zero).  With a variable width one can achieve different levels of quality however at the cost of sacrificing various levels of detail.  This drawback is because of the how the filter removes the noise, since it is removing all but the important and large details the much smaller and finer things located further from the center of the image get lost.  This can be alleviated with more sophisticated filters however our simple Gaussian filter can only change its width in the horizontal or vertical directions.

{{{id=38| /// }}}