Thursday, August 6, 2009

ACTIVITY 7 - Enhancement in the Frequency Domain

The goals of this activity is to be able to remove unwanted repetitive patterns by creating filter masks to block frequencies of the unwanted patterns in the Fourier domain. In doing so, the desired frequencies of the image may be enhanced thus, enhancing the quality of the image at the same time.
Activity 7.A Convolution Theorem

An binary image of an aperture consisting of two dots (one pixel each) was created using MS Paint. Its FT was taken and then displayed.

The FT modulus resulted a sine function along an axis which is symmetric about the center, which is expected. As we know, the FT of a sine function will result to two dirac deltas which will peak at the frequency of the sine function. Conversely, if we take the FT of two dirac deltas represented by two one-pixel point symmetric at the center, a sine function will be obtained with frequency related to the separation of the points.

Next, the dots were replaced with circles of some varying radius.


When the two dots were replaced with two circles with certain radius, the FT resulted into an airy disk pattern with embedded alternating parallel lines. This was due to the convolution of the circular aperture with the dirac deltas. The convolution theorem states that the FT of a convolution of two functions in space is the product of the two functions' FT. When the circular aperture and the two points were convolved, its FT came from the product of each function's FT, which is an airy disk pattern and a sine function, correspondingly. We can also observe that as the radius of the circles increases, the resulting radius of the airy disk pattern decreases.

After with circles, squares of some varying width were utilized.

Again, this is a convolution of two function, the square aperture and the two dirac deltas with FT resulting from the product of FT of each function. The FT of a square aperture is a sync function and the FT of the two dots is a sine function. The FT modulus came from the product of the two FTs. As with the varying radius of the circles, the as the width of the square increases, the width of the FT images decreases.
-------------------------------------------------------------------------------------------------
Lastly, the dots were replaced with Gaussians with the form Gaussians with varying variance (sigma squared) were created. The peak location of these Gaussians are located at ±mu.

The figures above shows the Gaussians with increasing variance (sigma squared) alongside with its FT modulus. For a better view, the images below showed it in 3D.









































We can say that this is a convolution of the Gaussian function and the two dots (dirac deltas). Taking its FT will again result to the product of the FT of each function. The Fourier Transform of a Gaussian function is also a Gaussian function and the FT of the dirac deltas is a sine function. Therefore, the resulting FT modulus is a Gaussian function with alternating parallel lines. As the variance of each Gaussians was increased, the radius of the Gaussian in the frequency domain decreases.
-------------------------------------------------------------------------------------------------
Activity 7.B Fingerprints : Ridge Enhancement
In this part of the activity, we tried to enhance an image of a fingerprint which was downloaded from the internet. The enhancement includes emphasizing the ridges of the fingerprint and removing its blotches.
The image was then opened in Scilab as grayscale image. Its FT was obtained by the procedure done in the previous activities. Then the FT was displayed in logarithmic scale in order for us to visualize where the frequencies of the fingerprint ridges lie. This will give us the idea on how to design the filter mask that will enhance the appearance of the ridges and at the same time remove the blotches.

I designed the filter mask below. The white blobs corresponds to the frequencies that will be enhanced relatively with the black areas which are frequencies that will be lowered/omitted.
The filtering was done by taking the FT of the fingerprint image and multiplying it to the filter mask image which was shifted using fftshift function. The product was then inversed and resulted to the enhanced image below.
From the enhanced image, we can see that the diagonal line is not as visible as with the original fingerprint. Although the ridges are not as sharp as with the original, we can notice that the blotches are removed.
-------------------------------------------------------------------------------------------------
Activity 7.C Lunar Landing Scanned Pictures : Line removal
This part of the activity focuses on the removal of the frequencies that produces vertical lines in the image. An image of the moon's surface from a lunar landing was utilized in this part. Parallel vertical lines are visible through out the image.
To remove these lines, we must know where the frequencies of the lines lie. We get the FT of the image and displayed it in logarithmic form. From this image, we have the idea that the frequencies correspond to the parallel lines lie in the bright region that forms the vertical line. As we know, the FT of two dots along horizontal is a sinusoidal function which corresponds to the parallel vertical lines. So in order to remove these lines, we block the bright horizontal line and have our filtering mask. We might as well block the bright vertical line so that some horizontal lines in the image may be removed. We must be careful not to block the center of the filter because there are so much information there that we needed. The image below was used as the filtering mask of the image.
As before, the enhanced image was obtained from inversed of the product of the FT of the image and the fftshift of the filter mask.
From the enhanced image above, the removal of the parallel lines is visible. The quality of the image was enhanced as well.
-------------------------------------------------------------------------------------------------
Activity 7.D Canvas Weave Modeling and Removal

The last part of the activity utilizes a canvas painting. We will use what we have learned in image enhancement to remove the canvas weave visible in the painting.
We begin our filtering by first getting the FT of the painting in logarithmic form. This shows where the frequencies of the unwanted pattern can be found. From the image below, we notice the presence of some bright spots along the center. Compared from the previous part, the location of the frequencies that correspond to the combination of parallel vertical and horizontal lines is distinct. In order to remove these frequencies, we design the filtering image by blocking these bright spots. The image below shows the filtering mask used.
The enhanced image was then obtained by from the inversed of the product of the FT of the image and fftshift of the filter mask.
Clearly from the image above, we can see the image was enhanced by the removal of the canvas weave pattern.

To show that what we have blocked using the filter mask is really the weave pattern, we tried to generate the pattern from the filter. First the filter was inverted and the inverse FT was taken. The FT modulus is closely similar to the canvas weave as shown below.

I grade myself a 10 out of 10 in finishing this activity. This activity shows me how the convolution of two functions affect their FT modulus. The FT of two convolved function will result to the superposition of the FT of each function. I was able to use this idea in removing unwanted patterns and enhancing an image. I acknowledged Gilbert who was always there discussing and working with me during class hours.

Monday, July 13, 2009

ACTIVITY 6 - Properties of the 2D Fourier Transform

Activity 6.A Familiarization with FT of Different 2D Patterns

This activity is similar to the previous one. We created different patterns, took their respective Fourier Transform and then display their FT modulus. This patterns were done with the aid of MS Paint and Scilab made it possible to compute their Fourier Transform.

The first pattern is a square aperture and beside it is its FT. The FT of a square aperture is similar to the FT of a circle (from Activity 5.A) but instead of an "airy disk" pattern, it has a central square maximum and maxima and minima intensities along its four sides. This pattern is know as the sinc function.

The next pattern is an annular aperture. Again, this is similar to the pattern obtained from a circular aperture except that at some fringes are not present due to the circular blocking at the center.
Then, a square annular aperture. This was analogous to the circular and annular aperture. The pattern obtained was almost the same with the square aperture and as expected, there are fringes missing. This is similar to a diffraction from a slit only that it is along x and y.

Next is a two-slit pattern. Along its side is its FT which yields from the cancellation of the upper and lower sections from the aperture. Thus, we see a horizontal pattern which is due to the interference coming from two slits.

The last pattern is a two-dot pattern or a double circular aperture. As has been said, the result looks like that of a circular aperture with maxima and minima intensities.

Activity 6.B Anamorphic Property of the Fourier Transform

Using Scilab, a 2D sinusoid in the x direction was created, with frequency that can be varied.

nx = 100; ny = 100;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
f = 4 //frequency
z = sin(2*%pi*f*X);
imshow(z,[]);


And then, just like in previous part its Fourier Transform was taken and its FT modulus was displayed.

The figure above shows the source signal and its FT in 2D and 3D, respectively. The first figure shows a sinusoidal signal with frequency equals to 5 and below it is a signal with frequency 10. They corresponding FT shows that the peaks moves farther apart as the frequency increases. This is expected because the FT of a sinusoidal signal will have its peak on its frequency. Thus if you increase it, the peaks will farther along its axis.

-------------------------------------------------------------------------------------------------

Then, a real image was simulated by adding a constant bias to the sinusoid created earlier. Its FT was also taken.

z = sin(2*%pi*20*X)+0.95
The figure above shows the sinusoidal signal with constant bias and its FT in 2D and 3D. On the contrary with the previous result, the FT of a sinusoidal signal with constant bias has 3 peaks. The two peaks with equal amplitude are from the frequency of the sinusoid and the peak at the center comes from the constant bias. We recall that the FT of a constant is a dirac delta. Also note that the sinusoid signal used has a frequency 20.
--------------------------------------------------------------------------------------------------

If we suppose to have an image of an interferogram in a Young's Double Slit experiment, we can use this routine to extract its actual frequencies. We just have to find where the FT peaked. If a non-constant bias was added like those very low frequency sinusoids, we just neglect the peaks at low frequencies.

--------------------------------------------------------------------------------------------------

The sinusoid was then rotated.

theta = 30;
z = sin(2*%pi*f*(Y*sin(theta) + X*cos(theta)));


As expected, the peaks of its FT are also rotated.

-------------------------------------------------------------------------------------------------

Then a combination of sinusoids along x and y are investigated.

z = sin(2*%pi*4*X).*sin(2*%pi*4*Y);


Again, we expect this results, a four-peak plot. The first two peaks are due to the sinusoid along x and the last two are due to the sinusoid along y.

-------------------------------------------------------------------------------------------------

Aside from a combination of sinusoids, we added rotated sinusoid to the signal.

z = sin(2*%pi*f*X).*cos(2*%pi*f*Y)+sin(2*%pi*f*(Y*sin(30) + X*cos(30)));

Before calculating and displaying its FT, I had a prediction that the rotated FT of the additional signal will add up to the four-peak FR of the two-sinusoid combination.

As I predicted, six peaks appear on the FT.

-------------------------------------------------------------------------------------------------

I will give myself a grade of 8/10 for finishing the blog late. Although I was able to finish the activity during class hours, I'm having hard time doing blog because I have no access to the internet at home. Thanks again to Raffy and Gilbert for always answering my questions and for always having a good discussions.

Monday, July 6, 2009

ACTIVITY 5 - Fourier Transform Model of Image Formation

Activity 5.A - Familiarization with Discrete FFT

The function fft2() was demonstrated by utilizing a 128x128 images. This FFT was used for an image of a 'circle' which was created with the given code:

x = [-1:0.01:1];
[X,Y] = meshgrid(x);
r = sqrt(X.^2 + Y.^2);
circle = zeros(size(X,1), size(X,2));
circle(find (r <=0.5)) = 1.0; imshow(circle,[]);


Because of being complex, the abs() function was used for the output of the FFT to be able to view the intensity values. The first image shows the output of fft2() which has interchanged quadrants along the diagonal.

image = imread('imagefilename');
gray = im2gray(image);
FT = fft2(gray);
a = abs(FT);


To compensate, the function fftshift() was used to bring the interchanged quadrants back which is show in the second figure. With a closer look, this image is consistent to the analytical fourier transform of a circle which is an airy disk pattern.

b = fftshift(abs(FT));

Then, fft2() was again applied and the intensity of the image was displayed.

c = abs(fft2(FT)
This procedure was also done for an image of letter 'A'.

The inversion of the image after FFT being applied was clearly seen from this sample than the image of a circle. Thus, applying FFT twice will make the image inverted.



Activity 6.B - Simulation of an Imaging Device

A 128X128 image of the letters "VIP" which represents an object and an image of a white circle against black background which represents the "aperture" of a circular lens were created using MS Paint. The images were opened as grayscale in Scilab, taking the 2D FFT of the "VIP" image using fft2() function and using fftshift() function for the aperture image (the aperture is already in Fourier space).

object = gray_imread('vip.jpg');
aperture = gray_imread('circle.jpg');
offt = fft2(object);
afftshift = fftshift(aperture);


The product their FFT was inversed to get the convolved image.

FOA = offt.*(afftshift);
IOA = fft2(FOA);
The images above show the results for different radii of white circles. From left to right, the radius of white circle decreases. Thus, for the largest circle resulted for the sharpest image and for the smallest circle resulted for the blurriest image. This shows that the system acts just like a microscope.



Activity 6.C Template Matching Using Correlation

A 128X128 image with the text "THE RAIN IN SPAIN STAYS MAINLY IN THE PLAIN" on it was created in MS Paint. Then another 128X128 image with letter "A" at the center was also created with the same type of font as the first image. These images were opened as grayscale in Scilab and then got their 2D FFT.

text = gray_imread('text.bmp');
A = gray_imread('atext.bmp');
TFFT = fft2(text);
TA = fft2(A);


Then the Fourier transform of the image with letter "A" was multiplied element per element to the conjugate of the Fourier transform of the text image. Lastly, the inverse FFT of the product was computed.

P = TA.*(conj(TFFT));
R = fftshift((fft2(P)));

The picture above shows the output image besides the original image. It noticeable that the output image shows hazy counterpart of each letter on the original image. There is a significant peaks (white dots) on the letter "A" counterpart. This template matching technique recognizes the pattern (letter "A") from the template (text image). However, this can be problematic when the pattern looks like any part of each letter on the text. Take for example, when we use letter "I" as our pattern the output image will shows peaks on parts of each letters with straight vertical line just like the pattern.



Activity 6.D Edge Detection Using the Convolution Integral

Using Scilab, a 3X3 matrix pattern of an edge which has a total sum of zero was created. Three variants of this pattern were created.

horizontal:
template = [-1 -1 -1; 2 2 2; -1 -1 -1];

vertical:
template = [-1 2 -1; -1 2 -1; -1 2 -1];

spot:
template = [-1 -1 -1; -1 8 -1; -1 -1 -1];

This pattern was convolve with a "VIP" image using the function imcorrcoef().

C = imcorrcoef(vip, template);

The image above shows the results. We can see there are sharp edges on the output image depending on the pattern used. The pattern was detected along the edge of the template.


It felt good that I finished this activity during class hours. However, I was not able to finish the blog right away because of lack of time. I will give myself 10/10 for this activity because I was able to produce expected output although I have published the blog late. Thanks again for the help of Gilbert and Raffy in discussing the activity.

Sunday, June 28, 2009

ACTIVITY 2 - Area Estimation of Images with Defined Edges

In this activity, our goal is to be able to compute for the area of images with the use of Green's Theorem. First, we created images (with black background and white as the object) using MS Paint.


























Then, the areas of the object on each image were calculated using Green's equation given by
I create a code using scilab that computes for the area of the object in a black and white image. This code utilizes Green's equation. The computed area was then compared to the area obtained by summing up all 1's in the image (white object).

SOURCE CODE:

image = imread('tri 4900.JPG');//read image from file
bin = im2bw(image, 0.5);//convert image to black and white
[x,y] = follow(bin);//follow the contour of the object
plot2d(x,y);//plot of the contour

//Green's Theorem
area=[];
for i = 1:(length(y)-1)
area(i) = x(i)*y(i+1)-y(i)*x(i+1);
end
areatot =((sum(area))/2)

//pixel area
pixarea = sum(bin)-length(y)

//percentage error
%err = abs((areatot-pixarea)/pixarea)*100


By using the syntax 'follow', Greens's equation disregards the contour in computing for the area. In order to compensate the pixel area, we have to subtract the perimeter of the contour in the sum of the pixel area. The table below shows the computed areas and corresponding percent error. I give myself a 10/10 in this activity. I understand well the objective of the activity and thus being able to create a code. I acknowledged Raffy and Gilbert for agreeing with me about the fact that 'follow' disregard the number of pixels of the contour using Green's equation.

Saturday, June 27, 2009

ACTIVITY 3 - Images Types and Basic Image Enhancement

In this activity, we collected digitized images from the web and classified them into four types: binary images, grayscale images, indexed images and truecolor images. Using the imfinfo of Scilab-4.1.2, the properties of each image were determined.
BINARY IMAGE

FileSize: 344
Format: PNG
Width: 248
Height: 248
Depth: 8
StorageType: indexed
NumberOfColors: 2
ResolutionUnit: centimeter
XResolution: 72.000000
yResolution: 72.000000


GRAYSCALE IMAGE


FileSize: 28244
Format: PNG
Width: 290
Height: 300
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 14.960000
YResolution: 14.960000

***http://peps.redprince.net/peps/tiger-Q300.png

INDEXED IMAGE

FileSize: 6989
Format: GIF
Width: 491
Height: 334
Depth: 8
StorageType: indexed
NumberOfColors: 4
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

**http://www.springboardmagazine.com/SpringImage s/tiger.gif

TRUE COLOR IMAGE
FileSize: 31029
Format: JPEG
Width: 625
Height: 450
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
XResolution: 100.000000
YResolution: 100.000000

***http://animal.discovery.com/mammals/tiger/pictures/tiger-picture.jpg

----------------------------------------------------------------------------------------------------

I looked for a 3 1/2 diskette and had it scanned. Then I got its properties using imfinfo.
The image was then converted into grayscale. I created a code that will compute and plot the histogram of the scanned image. It is clearly shown from the histogram that the region of interest (ROI) is well separated from the background which is expected.With the use of im2bw, the image was converted into black and white using the best threshold (for this case, 0.5). For a nicer image, I used MS Paint. To get the area of the image, I applied the area calculation program I made for Activity 2. With this procedure, I got a 0.33 % error from the area calculated.

I will give myself a grade of 9/10 for this activity. I finished this activity late because I had a hard time looking for an object to be scanned. I thought that any little object I could think of might have already done by my classmates. I acknowledged again the help of Gilbert and Raffy for answering all questions I had in mind.

Wednesday, June 17, 2009

ACTIVITY 1 - Digital Scanning

Primarily, this activity aims to find the numerical values of a digitally scanned hand-drawn plot with the use of ratio and proportion.

First, I looked for a hand-drawn graph from an old journal and I found this plot from Annals of Chemistry, 1940.
This graph relates the carbon dioxide output from washed potato tubers under aerobic and anaerobic conditions.

I started noting down the pixel values of each data point by first having a conversion factor which relates the number of pixels to a certain number of physical value along both x and y axis.

For the x-axis, the ratio is 222:1 pixels and for the y-axis, there is 1440:1 pixels ratio. With these ratios in hand, I was able to tabulate the number of pixels of each data points and their equivalent physical values on each axis.


Finally, a reconstruction of the graph was done in OpenOffice.org Calc.


The plot above shows a decent reconstruction of the original graph. With the original graph being placed at the background of the reconstructed graph, we can see the correspondence among each data points with insignificant deviations. The trendline imposed on the reconstructed graph also fits the trendline of the original graph.

I evaluate myself with a grade of 10/10 from being able to find the numerical values of a hand-written graph by utilizing the ratio of number of pixels to number of physical value. I exerted enough effort to finish this activity on time. I also acknowledge the help of Raffy, Gilbert and Kaye in discussing the way on how to begin with the activity.