Thursday, June 25, 2009

Activity 2 - Area Estimation for Images with Defined Edges

In this activity, we are asked to obtain the area of some basic geometric shapes through the Green's theorem then this will be compared with its known analytic area.

Note that the first part of Activity 3 was done prior to this activity because the SIP Toolbox and ImageMagick needed for Scilab to handle images were not yet installed in our computer.

Green's theorem can compute the area of a closed contour through the relation of its double integral to the line integral of the closed curve. After manipulation of the Green's theorem, the resulting discrete or summation form for calculating the area of a closed contour is as follows.


Here, x and y are the pixel locations and N_b is the number of pixels along the contour.

All my geometric shapes under area estimation are created in Microsoft Paint. They are all 256 x 256 pixels in dimension and are saved as 24-bit Bitmap image. Then, using the programming software Scilab 4.1.2. with ImageMagick and SIP Toolbox installed, I managed to implement Green's method in area calculation.

It is important that the shapes created are white in color and black as their background since the follow command in Scilab gets the pixel locations along the border of the white before the transition to the black background. Incidentally, the default save format in Paint does not require to be converted into Binary-type because it already consists of only ones (white) and zeroes (black) when imported to Scilab using the command imread.

The first geometric shape is a rectangle.


Calculated Area using Green's Theorem = 13926.5 square pixels
Theoretical Area = 14016 square pixels
Percent Error = 0.64 %

The theoretical area is simply obtained by the formula A = lw (l = length, w = width) through subtracting the maximum pixel location in x to its minimum and multiplying it to the difference of extrema in y from the followed contour of the rectangle.

Next one is a right triangle.


Calculated Area using Green's Theorem = 6791.5 square pixels
Theoretical Area = 6960 square pixels
Percent Error = 2.42 %

The theoretical area is from A = (1/2)bh (b = base, h = height), where this is similar to the rectangle except for dividing it to 2. Of course, base and height are also from the differences of extrema in x and y of the followed contour of the triangle.

Finally, a circle is created for area calculation using Green's theorem.


Calculated Area using Green's Theorem = 16643.5 square pixels
Theoretical Area = 16741.547 square pixels
Percent Error = 0.59 %

From the theoretical equation A = pi*r^2 (pi = 3.14..., r = radius), the radius is determined from half of the difference of extrema in either x or y from the followed contour of the circle.

Since the shapes generated have pixelated edges, then errors cannot be prevented in area calculation using Green's theorem especially if they are diminutive which possess not-well-defined perimeter. However, I can say that the method is very reliable due to incredibly small percent errors I acquired hence I grade myself 10/10 for this activity.

I would like to thank Gary and Ed in helping me for loading the SIP Toolbox in Scilab of my desktop in our computer room. Raffy guided me for the correct syntax since I am not yet familiar with the Scilab environment.

Appendix
The Scilab code below is used in this activity.

rectangle = imread('rectangle.bmp');
triangle = imread('triangle.bmp');
circle = imread('circle.bmp');
[x_rectangle, y_rectangle] = follow(rectangle);
[x_triangle, y_triangle] = follow(triangle);
[x_circle, y_circle] = follow(circle);

n = length(x_rectangle);
o = length(x_triangle);
p = length(x_circle);

A_rectangle = [];
A_triangle = [];
A_circle = [];

for i = 1:n-1
A_rectangle(i) = x_rectangle(i)*y_rectangle(i+1) - y_rectangle(i)*x_rectangle(i+1);
end

for j = 1:o-1
A_triangle(j) = x_triangle(j)*y_triangle(j+1) - y_triangle(j)*x_triangle(j+1);
end

for k = 1:p-1
A_circle(k) = x_circle(k)*y_circle(k+1) - y_circle(k)*x_circle(k+1);
end

Theo_rectangle = (max(x_rectangle)-min(x_rectangle))*(max(y_rectangle)-min(y_rectangle))
Theo_triangle = (1/2)*(max(x_triangle)-min(x_triangle))*(max(y_triangle)-min(y_triangle))
Theo_circle = %pi*((max(x_circle)-min(x_circle))/2)^2

Area_rectangle = sum(A_rectangle)/2
Area_triangle = sum(A_triangle)/2
Area_circle = sum(A_circle)/2

percent_error_rectangle = abs(((Theo_rectangle - Area_rectangle)/Theo_rectangle))*100
percent_error_triangle = abs(((Theo_triangle - Area_triangle)/Theo_triangle))*100
percent_error_circle = abs(((Theo_circle - Area_circle)/Theo_circle))*100

No comments:

Post a Comment