Thursday, July 14, 2011

seven

We’ve already met Mr. Fourier Transform before (see here).  Now, we get to know him better.  The Fourier Theorem tells us that any images and signals in general can be expressed as a superposition of sinusoids.  In this activity, that was exactly what happened.
 
We first have to look at the Fourier Transforms of different 2-dimensional patterns.  Specifically, we study a square, an annulus, a square annulus, two slits and two dots the last two of which are symmetric along the center and located the x-axis.  Save for the double slits, all of these were hand-made via MS Paint.  I had to generate the double slits through Scilab because, long story short, I should have learned to maximize the figures before declaring that something was wrong.  The code I used to obtain the Fourier Transforms of these patterns used Scilab’s fft2() and fftshift() functions. 
 
a
Figure 1. (A to E) Square, annulus, square annulus,two slits and dots located the x-axis and symmetric along the center and (F to J) their Fourier Transform patterns.
 
From the previous activity, it was seen that patterns with similar original structures will also have similar Fourier Transforms.  That can also be observed here for the square, the annulus and the square annulus. 
 
As I’d previously mentioned, images and signals can be represented through the superposition and/or variation of sinusoids.  Studying the Fourier Transforms of different sinusoids now has renewed importance.  We start with the sinusoids of varying frequencies.  I highly recommend clicking on the image of the Fourier Transforms of these sinusoids to view it properly – unless, of course, you have bionic eyes and, in that case, ignore Figure 4. 
 
b_1_sinusoids
  Figure 2. Sinusoids of frequency (A) 2, (B) 4 and (C) 10 Hz.
 
b_1_fft
Figure 3. Fourier Transforms of sinusoids of frequency (A) 2, (B) 4 and (C) 10 Hz.
 
b_1_fft_close
Figure 4. Close-up shots of the Fourier Transforms of sinusoids of frequency (A) 2, (B) 4 and (C) 10 Hz.
 
For all Fourier Transforms, we see two tiny (and I mean tiny) dots whose separation increases with frequency.  I suppose that these paired dots which are symmetrical along the center are a result of the sine function being odd.  That is, the sinusoids that are being analyzed have negative and positive values.  These two dots represent the positive and negative values of equal magnitudes. 
 
We crash back to reality with the idea that images don’t have negative values and so to keep values positive, we shift these sinusoids by some value.  First, we do this using constants – a.k.a. applying a constant bias.  Mostly because I didn’t realize it was needed, I varied the values of the constant and I got some interesting results.  Basically, the Fourier transform of a constant biased sinusoid yields three dots.  The middle dot is the one that represents the bias.  The greater the bias, the less we see of the sinusoid’s Fourier Transform.
 
b_2_sinusoids
Figure 5. Sinusoids with constant bias of (A) 1, (B) 2 and (C) 3.
 
b_2_fft
Figure 6. Fourier Transforms of sinusoids with constant bias of (A) 1, (B) 2 and (C) 3.
 
b_2_fft_close
Figure 7. Close-up shots of the Fourier Transforms of sinusoids with constant bias of (A) 1, (B) 2 and (C) 3.
 
We now launch ourselves back down to earth and apply an unconstant bias in the form of another sinusoid.  The addition of a constant bias simply adds another pair of dots which you can differentiate from the main sinusoid because it’s amplitude is half of that.
 
b_3_sinusoids
Figure 8. Sinusoids with non-constant bias of half-amplitude sinusoid with frequency of (A) 2 and (B) 10 Hz.
 
b_3_fft
Figure 9. Fourier Transforms of sinusoids with non-constant bias of half-amplitude sinusoid with frequency of (A) 2 and (B) 10 Hz.

b_3_fft_close
Figure 10. Close-up shots of Fourier Transforms of sinusoids with non-constant bias of half-amplitude sinusoid with frequency of (A) 2 and (B) 10 Hz.
 
When sinusoids are rotated by some degree, I found that their Fourier Transforms are rotated with a some “mirror” effect going on.  Also, the greater the rotating angle, the greater the intensity of the dots.
 
b_4_sinusoids
Figure 11. Sinusoids rotated by (A) 30, (B) 45 and (C) 60 degrees.
 
b_4_fft
Figure 12. Fourier Transforms of sinusoids rotated by (A) 30, (B) 45 and (C) 60 degrees.
 
b_4_fft_close
Figure 13. Close-up shots of Fourier Transforms of sinusoids rotated by (A) 30, (B) 45 and (C) 60 degrees.

Finally, I make a checkerboard … err … a sinusoid along the x- and y-axis.  Did I forget to mention that the sinusoids used so far were along the x-direction?  Well, I have now. *evil smile*
 
b_5_sinusoid
Figure 14. Sinusoid along the x- and y-direction.
 
b_5_fft
Figure 15. Fourier Transform of a sinusoid along the x- and y-direction.
 
b_5_fft_close
Figure 16. Close-up shot of the Fourier Transform of a sinusoid along the x- and y-direction.
 
Compared to the previous results, there are now 4 dots present which I somehow expected since the sinusoid was along the x- and y-direction and hence, it would have “dots” that are symmetric along both of these axes.
 
Is it right to give myself a 9.5 for this activity? I was originally planning on giving myself a 9 because I wasn’t able to combine the sinusoid in the x- and y-direction with rotated sinusoids.  However.  I realized that I investigated a few properties that weren’t asked for as well.  That has to count for something right?
 
Credits to Mr. Kirby Cheng and Mr. James Pang because they gave me the idea of making image layouts in MS Paint (they use PowerPoint but the idea is the same) before placing it on my blog. 
 
SUPPLEMENTARY INFORMATION
 
Once again, I omitted the first few lines because they were just used to change the directory and the stacksize but here’s the code I used for the sinusoid-related items.
 
code
Figure S1. Scilab code used for the study of the Fourier Transforms of sinusoids.

REFERENCES

Activity 7 – Properties of the 2D Fourier Transform, Applied Physics 186 Manual

Tuesday, July 12, 2011

six

I would like to apologize for the totem pole approach I took in presenting my images.  Neither Blogger nor Windows Live Writer 2011 (which I still love by the way) were kind to me for this activity.

I always thought Fourier Transforms were just for numbers.  Here, I am obviously proven wrong.  From the mathematical expression (Figure 1), Cooley and Tukey were able to create the Fast Fourier Transform Algorithm or FFT – a fast and efficient Fourier Transform implementation.  Because of my other Physics subjects and exposure to various topics in our research group.  I’ve always wondered where the ‘2’ from fft2() came from and I now know that thus kind of FFT algorithm is for 2-dimensional signals.

eq_fft
Figure 1. Mathematical expression of the Fourier Transform

Before we were introduced to the more complicated side of the FFT algorithm, we were first tasked to familiarize ourselves with the algorithm itself.  This was done with images of a circle and the letter ‘A’ and variations of the FFT algorithm.

a_circlea_circle_1_ffta_circle_2_fta_circle_3_fft_of_fft
Figure 2. (Top to Bottom) Original circle image, result of using fft2() on circle image and using fftshift() and fft2() on top right image.

a_lettera_letter_1_ffta_letter_2_fta_letter_3_fft_of_fft
Figure 3. (Top to Bottom) Original letter image, result of using fft2() on letter image and using fftshift() and fft2() on top right image.

I forgot to mention it earlier but the output of the fft2() has interchanged the quadrants along the diagonal.  The fftshift() function simply places these quadrants in their proper place.  In relation to this, it seems both the second and third images of Figures 2 and 3 show this phenomena.  Another equally surprising thing is that the last image of Figure 3 is a vertically-flipped version of the original image.  Note that the results from both the fft2() and fftshift() are complex number arrays and hence the images that you see above are the absolute value of these arrays.  Another way of looking at it is that the images you see above are the amplitudes of these transforms.

From what I figure, Convolution it is a measure of how similar two functions (or in this case, images) are.  With this, we can simulate imaging systems as what was done with an object (VIP) and apertures of different pixel radii (6, 12, 18, 28 and 42).

b_vipb_aperture_r_06b_aperture_r_12b_aperture_r_18b_aperture_r_28b_aperture_r_42
Figure 4. (Top to Bottom) Object and apertures of radius 6, 12, 18, 28 and 42.
  
b_vip_aperture_06b_vip_aperture_12b_vip_aperture_18b_vip_aperture_28b_vip_aperture_42
Figure 5. (Top to Bottom) Resulting images from convolution with apertures of radii 6, 12, 18, 28 and 42.

As you can see, the images become clearer with larger aperture sizes – consistent with the theories behind diffraction.  I can also infer that the apertures used were converging lenses due to the inverted orientation of the images compared to the object.

Next, template matching using correlation was used.  Here, we more or less compare a sentence and the letter ‘A’ that have the same font size and color.  As you can see from the resulting image, greater intensities can be seen where the letter ‘A’ is roughly located in the sentence file.

c_letterc_sentencec_template_matching
Figure 6. (Top to Bottom) The letter ‘A’, sentence and their resulting correlation.

Can we only correlate sentences and texts then?  Of course not.  The correlations of an object (VIP) and different patterns (horizontal, vertical, diagonal and spot)  were obtained.  I have to note that these results somehow remind me of the effects of a few Image Filters in Photoshop.

d_vipd_vip_horizontald_vip_verticald_vip_diagonald_vip_spot
Figure 7. Resulting images from the correlation of the object with a (top left) horizontal, (top right), vertical, (bottom left) diagonal and (bottom right) spot pattern.

In terms of edge detection though, the correlation with the spot pattern wins.  The other three – especially the correlation with a vertical pattern – just look like fancily processed text.

The task that was given to us, at first, seems gargantuan but it didn’t turn out that way.  The codes were already given to us and all we had to do were create the images for processing.  Yes, I went overboard with my OCD (mostly) and tried to ensure that my circles were indeed circles and all my letters, words, etc. were positioned in the middle of the image.  Despite this absence of effort on my part, I think I deserve a grade of 10 anyways.

SUPPLEMENTARY INFORMATION

For archiving purposes – or basically if you’re just curious – here are the codes I used for this activity.  You may notice I removed the first three lines simply because these lines of code were used to change the directory and the stacksize.

code a
Figure S1. The code used for the familiarization with the FFT algorithm.

code b
Figure S2. The code used for the investigation of convolution or the simulation of an imaging device using Fourier Transforms.

code c
Figure S3. The code used for the investigation of correlation using Fourier Transforms.

code d
Figure S4. The code used for the edge detection using the convolution integral.

REFERENCES

Activity 6 – Fourier Transform Model of Image Formation, Applied Physics 186 Manual

Thursday, July 7, 2011

five

For grayscale images, their histogram is the normalized (by the total number of pixels) graylevel probability distribution function (PDF).  Because of this, the grayscale PDF can be modified.  One way of going this is to use histogram manipulation mainly to improve image quality, enhance image features of mimic different imaging system responses.  This modification is done, given the cumulative distribution function (CDF) of a desired PDF, by backprojecting the grayscale values using the CDF.

The PDF of an image can be described by p1(r) where r represents the different grayscale values of the image and consecutively, the CDF can be described by this mathematical relationship.

t
Figure 1. Equation for the image CDF

However, it is our goal to alter the image in some way and so we use a CDF described below where z, in this case, represents the different graylevels in the image. 

g
Figure 2. Equation for the desired CDF

This is where the backprojection method comes in and as a result, we may be able to obtain the new value that each pixel should have.

z
Figure 3. Mathematical representation of the backprojection method

As in other fields of physics, we must make sure that these theories can be applied to the real world.  For this, I used a picture of one of my adorable nieces – who, because she is a minor, I shall not name – taken by Mr. John Joan A. Mende. 

gasa_picgasa_grayscale
Figure 4. (top) Original Image (bottom) Grayscale version of original image

histplot_originalhistplot_normalized
Figure 5. (top) Original Histogram (bottom) Normalized Histogram

The main difference in the original and normalized histograms lie in the x-axis values.  For the original histogram, these values range from 0 to 1 while the values for the normalized histogram range from 0 to 255 (despite what the image tells you).

I found that the Scilab function tabul proved to be very helpful in computing for the CDF.  It took quite a bit of Google-ing to figure this out but it was worth it.  Note that I’m just showing snippets of my code since the previous parts have already been performed in activities before this.  Also, my code shows comments made rather unnecessarily to myself because I have the short term memory of Dory (from Finding Nemo) and I have to have something there to make sure I know what the code does.  In regards to the CDF of the original image, I have to be amazed at the smoothness of the curve.  My uncle must be that great of a photographer.

code 1
Figure 6. Scilab Code used to determine and plot the original and linear CDF.

cdf_origcdf_linear
Figure 7. (top) Original CDF (bottom) Desired linear CDF

Finally, we get into the whole backprojecting mania and of course the results obtained from it.

gasa_enhancedhistplot_enhancedcdf_enhanced
Figure 8. (top) Enhanced image and its (middle) PDF and (bottom) CDF

In comparison to the original image, the enhanced image seems lighter – you can distinguish the girl’s features better in the enhanced image.  As expected, its CDF  is the same as the desired CDF.

I manipulated the image using the Color Curves feature of GIMP, and with a exponential-looking CDF, the image looks darker and a damped oscillation-like CDF, the image looks extremely grainy. 

exponentialoscillation
Figure 9. (top) Exponential and (bottom) damped oscillation CDF manipulation
using GIMP

In this activity, we were told to use and/or compare similar color curve manipulation techniques using other free image editing software.  I really don’t know of any other free editing software and since I had Adobe Photoshop installed on my computer upon buying it, I decided to make use of it.  I obtained similar results as those above but we do have to consider that I used the same types of CDF manipulation.  All in all, I give myself a 10 for this activity.  Doing this for me was entertaining.  I leave the following images to say my goodbyes for me.

exponential PSoscillation PS
Figure 10. (top) Exponential and (bottom) damped oscillation CDF manipulation
using Adobe Photoshop CS5

REFERENCES:
Enhancement using Histogram Manipulation, Applied Physics 186 Manual