Monday, June 29, 2009

ACTIVITY 2 Area Estimation for Images with Defined Edges

For this activity, the area of several shapes (shown below) with defined edges was calculated both analytically and using Green's theorem. The area was calculated analytically using the known formulas for computing the area of regular polygons, i.e. circle, rectangle, triangle and trapezoid. Some of the shapes below are formed by combining the different regular polygons. Arrow = triangle + rectangle; Heart = 3 triangles; letter T = 2 rectangles. The area of these shapes were calculated by simply adding the area of the regular polygons forming the shapes. The dimensions (length/width/height) of the shapes were determined by counting the number of pixels along the border. Consider the rectangle in Figure 2 as an example. By knowing the pixel coordinates of the corners, it would now be easy to determine the dimensions of the rectangle.

Length = (192-57) + 1
Width = (145-55) +1

Area = Length x Width = 136 x 91 = 12376

Figure 1. Arrow, circle, heart, triangle, letter T, rectangle, trapezoid.

Figure 2. This was done using Paint, zoomed to 800%.

Below is the programming code used to calculate the area of the shapes using Green's theorem. The Green's formula in discrete form used for computing the area of enclosed regions is given below. (x,y) are the coordinates of the points along the contour line bordering the region. If it is already the last coordinate, then it must be multiplied to first coordinate to be able to close the contour line, a requirement for Green's theorem.

Since the image used is in JPEG format, it has to be binarized first, i.e. in terms of 1's and 0's only (see line 2). Line 4 of the code traces the the contour of the shape and outputs the pixel coordinates along the contour line. Lines 6-13 is the code for the Green's formula. Another way of checking if the calculated area is correct is by adding the 1s in the binarized image (see line 14). However, it must be noted that the outermost pixels or portion of the white area is used as the boundary/contour line of the region. This means the two calculated areas, from Green's theorem (area 1) and by summing the 1s (area 2), can never be equal. To account for this error, the number of points along the boundary (size of the output of line 2) is added to area 1. When this is done, area 1 now becomes greater than area 2. But if only half of the size + 1 is added, the calculated areas are now equal. (+1) can be explained by looking at the plot of the points along the contour line (see Figure 3). It can be observed that the curve is not closed, lacking one point/pixel.

1 image = imread('F:\Documents\AP186\activity 2 & 3\pics\arrow.jpg');
2 binarized_image = im2bw(image,0.5);
3 imshow(binarized_image)
4 [x, y] = follow(binarized_image);
5 plot2d(x,y)
6 s = size([x,y]);
7 xi = [x(1:s(1)-1)];
8 xi1 = [x(2:s(1))];
9 yi = [y(1:s(1)-1)];
10 yi1 = [y(2:s(1))];
11 sum1 = sum(xi.*yi1) + x(s(1))*y(1);
12 sum2 = sum(xi1.*yi) + x(1)*y(s(1));
13 area 1 = 0.5*(sum1-sum2) + 0.5*s(1) + 1
14 area 2 = sum(binarized_image)


Figure 3.


The table above lists the shapes used and their area calculated analytically, using Green's theorem and by summing the 1s. The % error is very very small, so it means the code above is highly accurate in calculating the area of shapes with defined edges. There is still error probably because, although defined, the edges are still pixelated.

I would give myself a grade of 9 for this activity because I was able to calculate the area of 7 shapes. Not perfect score because I cannot explain why only half the size of the border points has to be added.

I would like to thank Mr. Luis Buno III, Mr. Miguel Sison and Ms. Jaziel Vitug for answering some of my questions.

No comments:

Post a Comment