Tuesday, June 25, 2013

Activity 5 - Area Estimation

We started this activity last Thursday and we were allotted two meeting for it. The activity was a bit tedious but is easy once you get the hang of it. Scilab, GIMP and even Microsoft Paint (for my case at least) were utilized in this activity.

To start, the task is to estimate the area of a certain figure in an image. As a start, we were asked to create black and white pictures of regular polygons. I created five - rectangle, square, circle, equilateral triangle and right triangle - using Paint, as it was the easiest method for me. The background was black, and the shapes were white. Also, the area of the shapes should be analytically known. For my case, I measured the pixel dimensions to obtain the area.

Next, I loaded the image in Scilab and obtained the edge image as asked in the activity. Before running the script I made, however, I typed in help edge on the console to know more about the command. I found out that there are five ways that Scilab calculates for the edges. I used all five methods and compared the results.

So here are my results. First I used the rectangle. I measured the length and width of the figure to be 119 and 449 pixels, respectively, and so the area of the rectangle is 53,431 square pixels (sq. px.).
Figure 1.  A rectangle, with an area of
53,341 sq. px.

Then I used all five methods to obtain the edges, and ultimately the area, of the rectangle. First, I used the Sobel gradient estimation method, which uses a pair of 3x3 convolution masks to estimate the gradient along the rows and the columns of a 2x2 matrix (in our case an image) [1]. The result is shown below.
Figure 2. Obtaining the edges of the
rectangle using the Sobel method.
e = edge(im, 'sobel');


Afterwards I took all the pixel values of the edges and located the centroid of the image. Subtracting the coordinates of the centroid from the xy coordinates of the pixels, I was able to translate the centroid of the image to be the origin of the Cartesian plane. Then, I converted the xy positions into r and θ and sorted all xy pairs according to increasing θ. The result would be an ordered list of (x,y) coordinates that, when plotted, would give a closed contour. Now, I applied the discrete form of Green's theorem to the coordinates. Here is a snippet of the script I used:
[row,col] = find(e);
yc = sum(row)/length(row);
xc = sum(col)/length(col);
x = col-xc;
y = yc-row;
r = sqrt(x**2+y**2);
theta = atan(y,x);
[theta,k]=gsort(theta,'g','i');
xn=zeros(x);
yn=zeros(y);
for i = 1:length(k)
    xn(i)=x(k(i));
    yn(i)=y(k(i));
end
A = 0;
for i = 1:length(xn)-1
    A = A + xn(i)*yn(i+1)-yn(i)*xn(i+1);
end
A = 0.5*A
disp(A);
The area of the rectangle returned by this method is 40,723 sq. px., which has a percent difference of 27.0% with respect to the manually obtained area. Using the same script above I just changed the method used in edge detection and measured the area of the rectangle. I present below the results for the other four methods:

Prewitt Gradient Operator - another type of 3x3 convolution mask [2]
Area = 53,322 sq px; % diff = 0.204%
Figure 3. Edge detection using Prewitt
method.\
e = edge(im, 'prewitt');
Laplacian of Gaussian (LoG) method - highlights regions of rapid intensity variation of an image that is first smoothed by a Gaussian to reduce noise [3]
Area = 53,335.5 sq px; % diff = 0.179%
Figure 4. Edge detection using Laplacian
of Gaussian method.
e = edge(im, 'prewitt');
FFT Gradient method - uses a combination of Fourier transform and a convolution mask to obtain edges [4]
Area = 53,322 sq px; % diff = 0.204%
Figure 5. Edge detection using FFT
gradient method.
e = edge(im, 'prewitt');
Canny method - known as most optimal, minimizes loss and smoothens image to minimize noise, produces edge points that are well-localized [5]
Area = 53,315.552 sq px; % diff = 0.216%
Figure 6. Edge detection using Canny
method.
e = edge(im, 'prewitt');
After this, I also measured the area of the square, circle, equilateral and right triangle manually and by using the script I wrote above. The table below summarizes the results that I obtained.

Shape Manual Area Sobel % diff Prewitt % diff LoG % diff FFT deriv % diff Canny % diff
Rectangle 53431 40723 27.0 53322 0.204 53335.5 0.179 53322 0.204 53315.5 0.216
Square 9025 6958 25.9 9004.5 0.227 9040 0.166 9004.5 0.227 8998.1 0.298
Circle 21382.5 20751.8 3.00 21338.5 0.206 21387.5 0.0236 21338.5 0.206 21332.5 0.234
Equilateral 24664.5 24926.3 1.06 24784.5 0.485 24801.9 0.555 24784.5 0.485 24648.5 0.0651
Right 19710 679.9 186.7 19743.5 0.170 19770.9 0.309 19743.5 0.170 19671.3 0.196


As we can see, the results for the Prewitt and FFT Gradient methods are exactly the same. Also, the LoG method was the most precise, obtaining a very small percent difference with respect to the manually measured area in 3 out of 5 figures. On the other hand, the Sobel method was the least precise, obtaining as high as 187% difference.

The next task is to measure the lot area of a place of interest as taken from Google Maps. I decided to choose my house, since I know our floor area. After taking a screenshot of my house in Google Maps, I outlined the lot in white and filled the surrounding pixels in black, similar to the figures we did earlier. The actual screenshot and the filled outline are shown below.
Figure 7. (a) Actual screen shot of our street from Google Maps. (b) Marked area
where our house is. 
As you can see, our house is inside a compound. My grandmother (dad's aunt) owns the entire complex (including the green L-shaped house that is divided into four apartments), and she let us stay there for free since I was born. I won't say my exact address for obvious security reasons.

Then I used the filled outline of my house to be processed by my Scilab script, using LoG as edge detection method. The area returned by the method was 2,772.474 sq px. I measured the actual lot area of our house to be 2,735.135 sq px, and so the percent difference is 1.36%. Since our lot is not a perfect square, and I could not be sure if it was a perfect trapezoid either, so I used Brahmagupta's theorem [6] in measuring the area of our lot in pixels.
Figure 8. "Squarified" lot area
of our house. The white polygon
represents our actual lot.
Of course, Google Maps has its own scale. I measured 66 px for 10 m, or 0.1515 m/px. Using this conversion factor I determined our lot area to be 62.79 sq m for the manual pixel counting and 63.65 sq m for the Scilab script. This is very accurate, since I know that our floor area is about 126 sq m, divided into two floors, which gives 63 sq m.

Grade I give myself: 12/10. Aside from the minimum requirement of measuring area using Green's theorem on a figure, I did four more figures and compared five different ways of edge detection. I was also able to measure the lot area of our house.

Sources:
[1] Green, W. 2002. Edge Detection Tutorial. Retrieved June 25, 2013 from http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/edge.html
[2] Fermuller, C. and Pollefeys, M. Edge Detection. Electronic document. Retrieved June 25, 2013 from http://www.ics.uci.edu/~majumder/DIP/classes/EdgeDetect.pdf
[3] Fisher, R., Perkins, S., Walker, A. and Wolfart, E. 2003. Laplacian/Laplacian of Gaussian. Retrieved June 25, 2013 from http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm
[4] Electronic document. Retrieved June 25, 2013 from www.cs.haifa.ac.il/~dkeren/ip/lecture9.pdf‎
[5] Green, W. 2002. Canny Edge Detection Tutorial. Retrieved June 25, 2013 from http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/can_tut.html
[6] Wilson, J. 2013. Brahmagupta's Formula. Retrieved June 25, 2013 from http://jwilson.coe.uga.edu/emt725/brahmagupta/brahmagupta.html

No comments:

Post a Comment