Sunday, September 4, 2016

Activity 4 - Estimating lengths and areas

The fourth quest in our imaging processing journey involves estimating lengths and areas from an image. To do this, we make use of wholly familiar theorem encountered way back in the days of Math 55. Green's Theorem relates a double integral to a line integral. Physically, however, a double integral is equivalent to the area of region over which the integral is being performed. Thus, this theorem provides us with a powerful and succinct way of calculating the area of a large variety of regions given only knowledge of the enclosing contour. However, the theorem is limited only to shapes that do not have any concavities. 

To test the validity of this method, I used Scilab to generate images with rectangles of varying sizes. The analytical area was determined using the set dimensions of the rectangle (in pixels) and by the usual length times width formula for the area of the rectangle. To get the coordinates of the edges of the rectangles, I made use of the edge() function to delinate the figures using the Canny method (the other methods of edge detection weren't explored as they probably would need a whole other blog post to even scratch their surfaces). I then took the distances of the edge coordinates from the center of the grid and used this to get their angular positions with respect to this new "origin" in order to rearrange this list in terms of increasing theta. After this, the simulation was just a straightforward implementation of the Green's theorem using the discretized coordinates of the edge shapes. Figure 3 and Table 1 are the results of this simulation.
Figure 3. Rectangles with varying areas. Areas increase from top to bottom and left to right
Table 1. Analytical and experimental areas of rectangles in pixels squared


We see from Table 1 that the green's theorem method was a success! The slight deviation in the experimental area from the analytical may be due to the edge detection process. I noticed that changing the method used for this changed the edge shape created after edge detection and thus changed the output experimental area. The default Sobel method didn't even output a closed edge shape at all!

To test this theory on arguably more "interesting" shapes, I tried it on circles with the following results. 

Figure 2. Circles with varying areas. Areas increase from top to bottom and left to right


Table 2. Analytical and experimental areas of circles in pixels squared


Once again, the experimental area is pretty close to the analytical area! I had expected that the % deviations for the circles to be at least a little bit greater than those calculated in the rectangle case, but interestingly enough, they are fairly the same. Unlike in the rectangle case, an inherent deviation between the analytical and experimental areas of the circles exist due to the fact that the analytical area equation for a circle calculates it as if the circle is perfectly round. This simulation, of course, has to approximate the curve of a circle with elements in a matrix. It seems as if, however, that the deviation due to this factor may have been thoroughly minimized due to the grid being 500 by 500.

Lastly, I tried out the method on triangles. The difference from the other two, however, was that the "center" from which I calculated the edge pixel coordinates' angular positions did not correspond to the centroids of the shapes produced. This, however, should not change the relative angular positions (whether greater than or lower than) of the edge coordinates and thus the area should stay the same. The results were as follows,

Figure 3. Triangles with varying areas. Areas increase from top to bottom and left to right


Table 2. Analytical and experimental areas of triangles in pixels squared


This is where I was rather stumped. For some reason, the % deviations of the calculated areas of the triangles are much higher than the other two cases (although still relatively low). Just to eliminate possible factors, I had changed the "center" from which the theta coordinates were to be obtained but the areas calculated remained the same value. Changing the method of edge detection once again changed the result, but does not tell us anything about the apparent increase in percent deviation of the triangles case versus the other two. Nevertheless, the obtained % deviation is still relatively low.

To test this method on more complicated and irregular shapes, I decided to venture into Google Maps and find an interesting area to estimate. I chanced upon a golf course that one of my friends lived beside. It was reasonably easy to delineate the boundaries of the golf course given it's long straight sides. It, however, also contained curves and seemingly concave lines shapes (lower left corner of maps in Figure 4) that complicated matters slightly. It was interesting to see if the method would still work given these issues. The code implemented was wholly the same except for the inclusion of a meters to pixel scale value. 

Figure 4. Google maps area to be measured (left) and the outline of the lot to be mapped (right)

To apply the green's theorem method to this map, I had to create a bitmap image of the delineated area. Of all the things I worked on this activity, this took the LONGEST. I had thought that it would be easy to just edit the google maps screenshot in paint and "fill" in the colored portions with white/black. Unfortunately, it seems as if the colors that made up the map weren't all "solid" colors and I had to manually correct certain parts. Anyway, I made sure to take include the scale bar provided by google maps to facilitate the conversion from pixels to square meters. I calculated this to me 2.247 square meters/pixel. 

Figure 5. Bitmap image generated from the outline in Figure 4 and the outlined area using an online area estimator. 
Using an online area mapper [2], I found the area of the region of interest to be about 1159144 square meters. That's roughly about 1 hectare. Using the green's theorem method, the area was determined to be 1,103,699 square meters, resulting in a 4.78% deviation between both methods. In the context of the 1 hectare golf course, the deviation of 55,445 square meters is rather small (This is pretty small given that amateur golfers can drive golf balls out to about a length of 180 meters) . This slight deviation may be due to the fact that the area of interest had to be delineated separately for each method. Thus, the areas being measured in both cases are unlikely to ever be the same. The low percent deviation, however, gives considerable validation for the use of either of the two methods. 

Lastly, I'd like to try a a free image processing software named ImageJ to perform similar tasks above. The advantage of ImageJ (aside from it being free hehe) is that it allows you to set a scale (between pixels and actual distances) that places all subsequent measurements in "real world" units. To test this, I scanned a P20 bill.


Figure 6. Scanned P20 bill
Using a ruler, I measured the longer side of the 20 pesos bill to be 161.5mm. This value is what I entered into ImageJ (Draw line --> Analyze --> Set Scale) to come up with a conversion ratio of 5.8948 pixels/mm. After this, a rectangle drawn over the image led to the value of 10707.214 milimeters squared for the entire bill. Comparing this with the "actual" value measured physically using a ruler, we obtain the value of 10529.8 milimeters squared (161.5 mm by 65.2 mm). This gives us a percent deviation of 1.66%. This percent deviation is within the range of the percent deviations obtained for the Greens Method case, but is much easier to use due to the fact that a black and white bitmap image need not to be constructed. Moreover, results can be obtained immediately after delineating desired shapes. 

The question now is: why not just measure it with a ruler? Well, what if we were faced with the problem of measuring the area of a tiny object? For example, what if we were asked to measure the area of the palm civet's nose?

Figure 7. Palm Civet's nose on the P20 bill
Using ImageJ, this value is 0.777 milimeters squared! Imagine trying to measure that with your standard ruler!



CODE:

Based on my performance this activity, I would give myself a 12 out of 10 primarily because I was able to apply the Green's method to obtain areas with particularly high accuracy (less then ~2%). Moreover, having directly applying Green's method to a real world situation, I feel that I now have a much greater appreciation for this mathematical concept. Lastly, I've realized the power of applications like ImageJ in obtaining metric measurements from images. I've realized that ImageJ not only enables the accurate length and area measurement of very small figures, but also may be able to ease the measurement of very large objects which would otherwise take much longer to measure if done "manually". 

References:
[1] M. Soriano, "Length and area estimation in images," Applied Physics 186 Activity Hand-outs, 2015
[2] Google Maps Area Calculator Tool. (n.d.). Retrieved September 01, 2016, from https://www.daftlogic.com/projects-google-maps-area-calculator-tool.htm





No comments:

Post a Comment