Sunday, August 11, 2013

Activity 8 - Enhancement in the Frequency Domain

The aim of the activity is to be able to use the properties of Fourier transform in removing unwanted repetitive patterns or enhancing the desired frequency of an image.

We start off by observing the properties of FT in repetitive images and other symmetric patterns.


Figure 1. (a) 2 Pixel dot symmetric along the x- axis (b) Fourier transform image

As we can see, the image with two small dots produced a sinusoid pattern on the FT space.  We know, from previous activity, that an object symmetric about the x axis, can be considered as a convolution of the object and a two dots symmetric at the x axis, and thus with the convolution property, the FT of




Figure 2. (Left) Two circles with increasing radius (from top to bottom);  Right) Corresponding FT of the left image

Like from previous activity, increasing the size of the shapes decreases the airy function that the FT produces. 

Using the following code, i implemented these for square and a gaussian symmetric along x-axis

nx = 500; ny = 250; //defines the number of elements along x and y
x = linspace(-1,1,nx); //defines the range
y = linspace(-0.5,0.5,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

//Two Circles
r= sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y
A = zeros (nx,ny);
A (find(r<0.3) ) = 1;
B = zeros (nx,ny);
B (find(r<0.3) ) = 1;

C = [A B]; // CIRCLE

//Two Squares 
S1 = zeros(nx,ny);
r= (abs(X+Y) + abs(X-Y));
S1 (find(r<0.1) ) = 1;

S2 = zeros(nx,ny);
S2 (find(r<0.1) ) = 1;

S = [S1 S2] // SQUARE

//Two Gaussian Circles
r = sqrt(X.^2 +Y.^2);
sigma = 0.01
g = exp((-2*r.^2)/sigma**2); //gaussian function
A = zeros(nx,ny);
A(find(r<0.1)) = 1;
B1 = g.*A; 
B2 = g.*A; 
G = [B1 B2] //GAUSSIAN

imshow(C); //Choose if Circle, Square, or Gaussian
imwrite(C,'C:\Users\GreyFOX\Desktop\Activity 8\r=0.3.png')

Cft = abs(fft2(C));
image = mat2gray(fftshift(Cft));
imshow(image);
imwrite(image,'C:\Users\GreyFOX\Desktop\Activity 8\r=0.3fft.png' );

Note that I used this code to create the desired initial image and its corresponding FT for Figure 2. and the following results. Notice the codes for square and gaussian images.

Square


Figure 3. (Left) Two squares with increasing length (from top to bottom) , (Right) Corresponding FT of the left image


Gaussian


Figure 4. (Left) Two gaussian circles with increasing sigma (from top to bottom, sigma = 0.05, sigma = 0.1, sigma = 0.3) (Right) Corresponding FT of the left image

The next part is the implementation of convolution

//Creating 200x200 image with 10 randomly placed ones
A = zeros(200,200);

for i = 1:10
    A(round(rand(1)*200),round(rand(1)*200)) = 1;
end
imshow(A);
imwrite(A,'C:\Users\GreyFOX\Desktop\Activity 8\random.png');

d = imread('C:\Users\GreyFOX\Desktop\Activity 8\flower.png');
d1 = double(rgb2gray(d));

FTA = fft2(A);
FTd = fft2(d1);
FTAd = FTA .* FTd;
inverse = mat2gray(abs(fft2(FTAd)));
imshow(inverse);
imwrite(inverse,'C:\Users\GreyFOX\Desktop\Activity 8\convolvedflwr.png');






Figure 5. Result from Convolution of the two upper image


Basically, its the flowery pattern convolved to a randomly placed dirac delta, is the just the same as placing the flowery pattern randomly.


Lastly, using the results in the previous part, we may now began to study how the FFT enhances images.
Given an image




Figure 6 .Lunar Orbiter image of one of the craters in the far side of the moon with its corresponding FT.

From the previous part, we know that what we need to do is that to remove the thin line forming a cross through the center. More importantly, since the center possess all the information, we should take note to filter only the thin lines forming a cross as much as possible.

Figure 7. Thin lines used as mask to filter the thin lines. 

The last thing to do is to retrieve the image after filtering out the thin lines. The line used as mask is 3 pixel in width, to make sure complete masking.


Figure 8. Retrieving the image after masking.

Finally, we obtained this image, which is obviously enhanced.

I will give myself a grade of 10, for producing the results from the most basic up to  main objective of this activity. the which is to ENHANCED images using our knowledge about the Fourier Space.





Activity 7 - Fourier Transform Model of Image Formation

To further explain the different characteristics of the Fourier transform that is very useful to image processing, this activity is divided into four parts.

7.A Familiarization with discrete FFT
The first part of the activity is a simple demonstration of the Fourier transform. We are tasked to create a 128 x 128 image with a small circle in the center. This was done using paint. Then the following code is implemented to obtain the Fourier transform of the image.

//Activity 7 Fourier Transform


I = imread('C:\Users\GreyFOX\Desktop\Acitivity 7\circle3.png');
Igray = rgb2gray(I);
Igray2 = double(Igray);
//imshow(Igray2)

FIgray = fft2(Igray2); //remember, FIgray is complex
imshow(abs(FIgray));

//
//FFT shift
image = mat2gray(fftshift(abs(FIgray)));
imshow(image)
imwrite(image,'C:\Users\GreyFOX\Desktop\Acitivity 7\fftimg3.png')

One may notice that after the Fourier transform function, I had to use another related function which is the fftshift to obtain the following image.

Figure 1. Fourier transform of a prepared 128x 128 image of a circle using paint.

The fftshift is necessary so that the zero frequency is on the center of the image. Also based from the discussion, the output of fft2() has quadrants along the diagonals interchanged.The fftshift() function interchanges the quadrants back.[1] The resulting image Figure 1 (right) is known as an airy pattern.

The codes are also applied to another prepared image of a letter "A" on a black background. As a result, 
Figure 2. Fourier transform of a prepared 128 x 128 image of letter A

The result showed a figure that is symmetric both along the x and y axis. I dont know how to describe it but it somehow 'airy'. Another relevant concept to know is if you can RETRIEVE the original image from a Fourier transform image.


Figure 3. (left) image after using fft() again (right) after using ifft() 

To correctly retrieve the image from a Fourier transform, one must implement an inverse Fourier transform, doing so can retrieve the original image figure 3(b), on the other hand, doing fft() again to the Fourier transform image will result to an inverted image in figure 3(a).

The image of letter the circle in figure 1. and the image of  "A" in figure 2 is both constructed using paint. So we don't know the exact sizes of these objects. So to try and find out if the size of an object affects the results, i tried it to smaller and larger radius.

Figure 4. Fourier transform with varying initial size of circle
Given a bigger object its corresponding fft image is much smaller than the smaller circle. One can say that with the smaller circle, the airy pattern is more evident compared to the bigger one.

7.B Simulation of an imaging device

With the convolution property of the Fourier transform, one can simulate an imaging device, specifically in this activity, a simple circular aperture.

Figure 5. Image used for simulating the aperture

On the handout, the VIP image was dictated to be more than 50% of the area of the image. I tried to use smaller font size, and I found out that the larger images will just produce more observable than the smaller ones. The following results are done using Figure 5. The algorithm is just based on the manual, which utilizes the convolution property of the FT. 
1) Get the product of the FT of both images (the aperture need not be FFT'ed) [1]
2) Get the inverse to get the convolved image.


Figure 6. 'Imaged VIP' using the aperture (circle) of varying radius.

The results shows that with larger aperture, it produces a very clear image compared to the smaller aperture. With the smallest aperture, i think that it is unidentifiable. This is consistent with natural phenomenon, since smaller aperture allows lesser light to pass or minimal light to pass, it will more likely result to a blurry image compared with bigger sized aperture.

7.C Template matching using correlation

In contrast with convolution, correlation can be described as the element per element product of the conjugate of the FT of first image and FT of another. Their difference is that convolution is the element by element product of both FT's of two images(without conjugating the one image). Its application will be presented by the following results

Figure 7. 128 x 128 image of two images to be correlated.

As you can see, "A" image will be correlated to the an image text composed of different letters. The correlation measures the degree of similarity between two images.[1]

Figure 8. Correlated images using figure 7.

In Figure 8, it is evident that there are bright white spots on different parts of the image. If you would closely, to the original text, and this result, you can notice that the bright spots were the place where the letter "A"s are located. Meaning at this point, there occurs a higher correlation value between the two images.In this note, it is reasonable that this correlation function are used in template matching and pattern recognition[1]

7.D Edge Detection
The last part of these activity is edge detection. Similar to the previous part, the method will be just template matching of an edge pattern with an image.
Given the following patterns as implemented in the scilab code (and given by the manual).

h_pattern = [-1 -1 -1; 2 2 2; -1 -1 -1]; //horizontal
v_pattern = [-1 2 -1; -1 2 -1; -1 2 -1];//vertical
diag_pattern = [2 -1 -1; -1 2 -1; -1 -1 2];//diagonal
spot_pattern= [-1 -1 -1; -1 8 -1; -1 -1 -1];//spot

jazz = imread('C:\Users\GreyFOX\Desktop\Jazz.png');
jazz2 = double(rgb2gray(jazz));

Edge1 = conv2(jazzgray, pattern_h); \\ varying pattern
imshow(Edge1);
imwrite('C:\Users\GreyFOX\Desktop\horizontal.png');



Figure 9. Original image to be detected.



 Figure 10. Edge detection using different patterns (a) vertical (b) horizontal (c) spot (d) diagonal

The results showed a very nice implementation of edge detection. As you can see, with my name, it is better to use the spot pattern, since the letters have a lot of variation with edge patterns. Obviously, like the previous part, the (a)vertical pattern will detect vertical lines of the letters, and similarly with (b) horizontal pattern for horizontal lines, and so on.

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

When I finished the activity, i felt that it was so simple, but I was really stucked in this activity, especially on the first parts. It was really frustrating having minor mistakes like not having the absolute value(abs), or having errors which took me time to understand (because i am new to scilab).

For my self-grade, i would like to give myself a 10/10 for having produced the required output (though it took me a lot of time). I only enjoyed the last part where it demonstrates a nice way on finding the edge; unlike in Activity 5, where we only utilize the built-in function edge(). Maybe that's why i did not explore much on this activity.

Actually i tried changing the values of the edging pattern, with sum not = 0, unlike what it was said in the handout. The edge detection still works even the sum of the pattern is not 0. Only it would result to a weird pattern and it would recognize weird edges.

References:
[1]Maricor Soriano. AP 186 Activity Manual A7 – Fourier Transform Model of Image Formation.2013


Activity 6 - Enhancement by Histogram Manipulation

The activity aims for us to be able for us to enhance an image using Histogram Manipulation

Sometimes, when we capture an image, 
The following image is a sample of a not so good photograph taken from my album.


Figure 1. Dark photograph with my batchmates

Figure 2. Histogram plot of the image using scilab
As we can see, the histogram are crowded in the left, and its not extending even at least at the center. T
Figure 3. CDF plot
Figure 3 is produced simply by utilizing the cumsum function in scilab.

Since we already know the behavior of the CDF of our sampled image, we can now start manipulating the factors affecting the histogram. 

Figure 4. New image adjusted to have the following CDF
Figure 5. New CDF plot of the image.

Alternatively, we could use GIMP to manipulate the histogram and adjust Color Values. I think this is more convenient than the previous method. Well anyway, The




Another example:






I think this is a very nice activity, though I am really having a hard time blogging. Its like, I always don't have something to say.

Anyway I produced all the necessary results using scilab and especially with GIMP as well. I just don't have the time to explore this activity more.

References
[1] AP186 Handouts.Activity 6 - Enhancement by Histogram Manipulation. Maricor Soriano 2013
-------------------------------------------------------------------------------------------
Self Grade:
5 or 6/10 I was able to produce all required results but, obviously, my discussion is very poor.

---------------------------------------------------------------------------------------------------
Anyway, I am not really fond of story telling, maybe that's why I am having a hard time to blogging,

Activity 5 - Area Estimation of Images with Defined Edges

One of the most helpful information from image processing is the area or size estimation. Usually, images for research, or other data acquiring activities make sure that there is an object with known scale included in the picture. This is for us to know the actual size of a desired object in the image.

Obviously, one can measure the area of an image, by using the measurements provided by a RULER (a real one or from a document/image processing program, and then calculation by brute force.

The activity aims to be able to measure an area of a given figure/image, using image processing tools without having to resolve to tedious calculations. In this note we utilize the the derived discrete form of Greens Theorem given by the following equation [1]

 
where Nb is the total number of pixels in the boundary or contour of the area.[1]


To start this activity, we again use the codes in scilab previously discussed in Activity 4 to create different shapes like we did in Activity 2.

nx = 500; ny = 500; //defines the number of elements along x and y
x = linspace(-1,1,nx); //defines the range
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

//Centered Circle
r = sqrt(X.^2 + Y.^2); //note element-per-element squaring of X and Y
image1 = zeros (nx,ny);
image1 (find(r<0.3) ) = 1;
imshow(image1);
imwrite(image1,'C:\Users\GreyFOX\Desktop\Activity5\circle.bmp');

// Centered Square
image2 = zeros(nx,ny);
r = (abs(X+Y) + abs(X-Y));
image2 (find(r<0.4) ) = 1;
imshow(image2);
imwrite(image2,'C:\Users\GreyFOX\Desktop\Activity5\square.bmp');

The previous code, produces image1 - a circle, and image2 - a square. Obviously the theoretical area of the is calculated as 0.2642 given that r = 0.29 from the code; while the area of the square is known to be 0.16 given the arbitrary unit of the matrix space.

Figure 1. Image with areas to be measured

circle1 = imread('C:\Users\GreyFOX\Desktop\Activity 7\circle.bmp');
square1 = imread('C:\Users\GreyFOX\Desktop\Activity 7\square.bmp');

edges = edge(circle1, 'sobel'); \\ 'sobel' can be varied 
imwrite(edges,'C:\Users\GreyFOX\Desktop\Acitivity 7\sobel.bmp'); \\the filename is also varied according to the edge method
imshow(edges);


With the predefined images(figure 1), the previous code is used to detect the edges of both images. Consequently, there are different types of edge methods in scilab. Using the help function in scilab and finding edge in it. I obtained the following description.

Figure 2. Description 


Figure 3. Edge using sobel method

Figure 4. Edge using prewitt method


Figure 5. Edge using log method



Figure 6. Edge using fftderiv method


Figure 7. Edge using canny method


For me, i think the best method would be the 'fftderiv' method with a defined edges, compared to 'sobel' and 'canny' which we could see that there are some undefined or blurred parts. On the other hand, the 'log method' produced very thick edges that may affect the area calculation.

Given these images of the edges of the areas were calculated using the derived discrete form of Green's theorem implemented by the following code. Take note that these codes are continuous with the previous codes, meaning circle1 is the previously created circle in figure 1a.


After having the edge determined by the function edge() in scilab, the following code's algorithm is as follows.
1)Find the pixel coordinate of the entire edge
2)Sort these with increasing angle
3)Convert to polar coordinates r and theta
4)Sort [x,y] pairs to increasing theta

Given these contour data, we implemented the greens function using the for loop in the code.
The last part just displays the results and convert it to an arbitrary unit

edges = edge(circle1, 'sobel');

[i,j] = find(edges);
yc = sum(i)/length(i);
xc = sum(j)/length(j);

y = i-yc;
x = j-xc;

theta = atan(y,x);

[theta, k] = gsort(theta, 'g', 'i');
r = sqrt(x**2+y**2);

SUMMATION = 0;
i($+1) = i(1);
j($+1) = j(1);
k($+1) = length(theta)+1;

for l = 1:length(theta)
    SUMMATION = SUMMATION + i(k(l+1))*j(k(l)) - i(k(l))*j(k(l+1));
    //greens function
end
Area = 0.5*SUMMATION;

disp(Area);

realarea = Area*(0.004**2)
disp(realarea);

Using these, I compared the percent deviation of the calculated area with the theoretical value given beforehand.

Table 1. %Deviation of results




As we can see, the method fftderiv is better than the others, one may use these methods with their interest. Maybe the sobel method can detect corners of other shapes and may be helpful to someone.

Using the best method based on the results, which is the fftderiv, I used this to calculate the area of The Bahay ng Alumni in UP Diliman, viewed by a satellite from google maps. I choose these place with known area around 3000 sqm. [2]



Using the scale at the bottom right, I found out that it has 35 pixels for every 10m. I did this by noting the pixel coordinate of the scale.

Figure 9. Area of The Bahay ng Alumni

Using the same algorithm, and 'fftderiv' edge method the calculated area is 2824.081632653061 m^2. I already converted it using the conversion from pixel area to meters which is already presented. It yielded 5.8639% deviation to the estimated area of The Bahay ng Alumni

-------------------------------------------------
I would give myself a grade of 11/10 for i produced all the necessary outputs as well as observe different methods of the edge() function. I also was able to calculate with a good accuracy an area given a map using the knowledge i learned.

References:
[1]AP 186 Handouts. A2 – Area Estimation for Images with Defined Edges. Maricor Soriano 2013
[2] http://www.upalumni.ph/wp-content/uploads/2013/06/rates-new.pdf

Acknowledgements:
Thanks to Tor (Bareza), for helping me understand pixel to other unit conversion which lead me to finish this activity. Also thanks to Ate Bastine (Carpio) for helping me understand scilab matrix and operations.