Thursday, July 21, 2011

A8 - Enhancement in the Frequency Domain

Today is July 21, 2011 -- exactly 5 weeks after I put up this blog... It was fun writing notes and nerdy stuff here knowing the fact that anybody, anywhere around the globe, can actually reach and read this blog. Amazing huh!

Anyway, going back to what this post really is about, I will show you how to enhance an image by removing or improving some parts in the frequency domain. To be able to follow the concepts I will use in this post, you must have some background in using Fourier Transform, if not, you can visit first my previous posts here and here.

The basic idea of this post is that whenever we have an image with repetitive patterns which are not necessary, we can get its Fourier Transform. By doing so, we are now in the frequency domain so we can create a filter that will remove frequencies pertaining to the unwanted patterns. And viola! The unwanted patterns are gone!


Before moving on, we must first know deeply the tricks in using Convolution Theorem.
  • The product of the Fourier Transform(FT) of two functions is just the FT of their convolution.
  
Equation 1.
  • The convolution of a function and a dirac delta (a single sharp peak) will result to a replica of the function in the location of the dirac delta.
  
Equation 2.

As a simple preliminary exercise, we observe the resulting FT modulus of two position- and size- symmetric dots, squares and Gaussian dots as their sizes are changed.
A.) Dots
We first create two dots along the x-axis symmetric about the center as shown in figure1a-e. Then we take the FT modulus of the image as shown in figure 1f-j.
 
Figure 1. Image of two dots and its FT. (a-e) Dots with radii of 0.01, 0.03
0.05, 0.08 and 0.1 units. (f-j) FT modulus of a to e, correspondingly.
(Note that, here 128 pixels = 2 units)

We can see in figure 1f that the FT modulus is a series of dark and bright fringes. As we increase the radii of the spots, formation of concentric circles (with dark and bright patterns) are observed; the larger the radii is the more concentric circles appear. The results are theoretically sound since the dots can represent circular apertures and the corresponding diffraction pattern formed is a Bessel function with development of concentric circular patterns.

B.) Squares
Same as the dots, we create two squares symmetric about the center and take its modulus of its FT.
 
Figure 2. Image of two squares and its FT. (a-e) Squares with length of 0.02, 0.06
0.1, 0.16 and 0.2 units. (f-j) FT modulus of a to e, correspondingly.
(Note that, here 128 pixels = 2 units)

If we compare figures 2f and 2g with figures 1f and 1g, respectively, the results look the same. This is because the radii of the circle used to obtain figures1f and 1g are very small that the circle seems to look like squares. However, as we increase the side length of the squares, obvious differences are observed, that is series of dark and bright squares of varying sizes along the x- and y-axes occur. This can be attributed to the sinc function theoretical diffraction pattern observed for square apertures.

C.) Gaussian dots
In creating Gaussian dots, we use the form shown to the right where a is the center of the Gaussian dot at the x-axis and sigma^2  is the variance. We then vary the variance and observe changes in the modulus of its FT.

 
Figure 3. Image of two Gaussian dots and its FT. (a-e) Gaussian dots with variance of 0.01, 0.03, 0.05, 0.08 and 0.1 units. (f-j) FT modulus of a to e, correspondingly.

The resulting FT for two Gaussian dots is shown in figure 3f. The pattern formed is also a Gaussian function with dark bands in between. As we increase the variance of the dots, the smaller the pattern becomes.
 
Now, we implement the phenomena defined by equation 2. We can do this by creating peaks in random positions and convolve patterns to the formed image.
 
Figure 4. 10 peaks in random locations.
(Here, a 200x200 pixel image is used)

The peaks created in figure 4 will serve as dirac deltas. We then create different 3x3 patterns such as horizontal and vertical and convolve it with figure 4. The convolution can be done in Scilab using the function imconv(Img, mask) where Img is a grayscale image (figure 4 here) and mask is the pattern to be used.

 
Figure 5. Implementation of equation 2. (a) horizontal pattern, (b) vertical pattern,
(c) diagonal pattern, (d) spot pattern, (e-h) resulting convolution of figure 4 with
figures 5a-d, respectively.

If we closely observed the resulting convolutions in figures 5e-h (try clicking the image to enlarge), the location of the peaks in figure 4 are still present. However, this time, the peaks are surrounded by black pixels depending on the pattern used. This is to show that the convolution of a pattern and dirac deltas will result to replication of the pattern on the dirac deltas' locations as proven by equation 2.

The next task is to create a 200x200 array of zeros and putting equally spaced peaks. The FT is then  obtained and observe the results as the spacing between peaks is varied.
 
Figure 6. Peaks at equally spaced locations and their FT. (a-d) peaks with 
spacing of 5, 10, 20 and 40 pixels. (e-h) Corresponding FT modulus of a-d.

We can notice in figures 6e-h that as we increase the spacing between peaks, the number of frequencies decomposed via FT increases. Looking further, the number of frequencies observed in a single column/row is actually the spacing itself. For example, if the spacing between peaks is 5 as shown in figure 6a, the resulting FT in figure 6e will contain 5 observable frequencies in a row or column.

We now move on to the main objective of this post, to enhance images via enhancement in the frequency domain.

A. RIDGE ENHANCEMENT
The first task is to enhance the ridges in a fingerprint photograph (figure 7).
 
Figure 7. Photograph of a fingerprint.
(Image from http://www.reachoutmichigan.org/funexperiments/agesubject/lessons/prints.html)

The thing to do is to convert figure 7 to grayscale, take its FT and design a filter mask to remove unwanted frequencies. The resulting FT modulus is shown in figure 8.
 
Figure 8. FT modulus of figure 7.
(Note: This is in logarithmic scale because the real FT image span orders of magnitude)

The circular looking feature in figure 8 can be attributed to the ridges of the fingerprint while the bright spot in the middle is the DC term that gives more details to the fingerprint image. Since we want to enhance figure 7, we create masks that will zero out the surrounding noise and maintain the circular looking pattern and bright middle spot. We create three masks as shown in figures 9a-c.

 
Figure 9. (a-c) Masks (d-f) Superimposed masks with the FT of figure 7.
(g-i) Inverse FT of d to f, correspondingly

The first two masks are two concentric circles, one of which has a bigger inner circle than the second. While the third mask is made by following the edges of the wanted frequencies in figure 8 using Gimp. In frequency domain, the combination of these masks with the FT modulus is shown in figures 9d to 9f. And by taking their inverse FT, the resulting enhanced images are shown in figures 9g to 9i. By looking close (enlarging) figures 9g-i, the ridges are more defined as compared to the original image. By comparing the results, I must say that the filter described in figure 9a enhanced better the fingerprint image.


B. LINE REMOVAL
Most often than not, you had encounter images with unwanted vertical lines in it. These vertical lines may have caused by stitching parts of the image to create a composite image. The objective of this part of the post is to remove these lines by going to the frequency domain.

One image from the Apollo mission is shown below
 
Figure 10.  Image from 
http://www.lpi.usra.edu/lunar/missions/apollo/apollo_11/images

Taking its FT modulus (in logarithmic scale again)
 
Figure 11. FT modulus in logarithmic scale of figure 10. 

Note that the FT of a vertical line is a horizontal line, thus to be able to remove the vertical lines in figure 10, we must remove the observed bright horizontal line in figure 11. So we try creating three filters as shown in figure 12a-c.

 
Figure 12. (a-c) Masks (d-f) Superimposed masks with figure 11
(g-i) Resulting image using the filters in a-c, respectively

Using the filter described by figure 12a, the resulting image (figure 12g) do not contain important details from the original picture, this is because the bright rectangular part in the middle was removed. Because of the bad result of the first mask, we then create a mask which doesn't cover the center rectangular bright part. The resulting image shown in figure 12h is an improvement and the vertical lines are indeed gone. For comparison, we create another mask containing vertical and horizontal lines and still not covering the middle bright part. The resulting enhanced image in figure 12i do not have significant difference with figure 12h. 


C. CANVAS WEAVE MODELING AND REMOVAL
We use the same method employed above in removing weaves in a painting's image formed because of the canvas used. We remove these canvas weaves in the frequency domain.
 
Figure 13. Image of an oil painting from the
UP Vargas Museum collection.
The FT modulus of figure 13 is
 
Figure 14. FT modulus in logarithmic scale.
Notice the bright spots in the modulus, these spots may have been the cause of the weaves in the original image. So we create filters to remove these bright spots shown in figures 15a-c

 
Figure 15. (a-c) Masks (d-f) Combining the masks and the FT modulus 
in figure 14 (g-i) Enhanced image using the masks in a to c.

Same as the result in the Lunar landing photo vertical line removal, filtering out the center bright spot results to a massive removal of important details as shown in figure 15g. Using the masks shown in figures 15b and 15c, the resulting image do not have comparable differences. However, both significantly removed the weave pattern from the original image and the brushstrokes in the painting are improved.

We then try inverting the developed filters and take their inverse Fourier Transforms.
Figure 16. Inverse FT of the masks in figures 15a, 15b and 15c

Amazingly, the generated modulus images look the same as the canvas weave in the original image.


Phew! This was a tiring activity, but same with previous posts, it was worth a try. Imagine the power of Fourier Transform. Imagine how beautiful and easy life becomes when we move to the frequency domain!
For this activity, I would give myself a 10.0 for having all the required outputs, for achieving the goals of this activity and for checking out different masks for comparison.

References:
[1] 'Enhancement in the Frequency Domain', 2010 Applied Physics 186 manual by Dr. Maricor Soriano


No comments:

Post a Comment