Quatrième: Length + Area Estimation

Good morning/afternoon/evening reader! First and foremost I want to thank you for taking time to read this blog and I hope I that I could impart something with my work. Also, sorry for any lapses or mistakes I might accidentally mention in this blog, this has been a hell of week. Comments and suggestions regarding this work are highly appreciated!

The main topic of this blog is how to utilize image processing in the measurement of length and areas of actual subject. This is an extension of my previous blogs wherein I got the pixel to the axis unit conversion to recreate and enhance old hand-written plots. To begin this study, let us first look at Green’s theorem:

green

Essentially, if we choose F1 = 0, F2 =x for the left hand side and F1 = -y, F2 = 0 for the right hand side, we can obtain the area of a region R in discrete form:

triangle

Now, we apply this theorem via scilab to two of the polygon filters I generated in the previous blog to better understand it and test its validity. I first generated a 0.8 x 1.2 unit rectangle, using the set of codes from my previous blog and then added the following lines:

——-

im = imread(‘Rect.bmp’);
E = edge(im,’canny’);
imwrite(E,’Rect2.jpg’);
imshow(E);
[x1, y1]= find(E);
Rx = x(x1);
Ry = y(y1);
theta = atan(Ry,Rx)*180/%pi;
data = cat(1, theta, Rx, Ry);
[D,k] = gsort(data(1,:), ‘g’, ‘i’);
inc = data(:,k);
Area = 0 ;
for i = 1:length(inc(1,:)),
if i==length(inc(1,:)) then
triangle = 0.5*(inc(2,i)*inc(3,1)-inc(2,1)*inc(3,i));
else i!=length(inc(1,:));
triangle = 0.5*(inc(2,i)*inc(3,i+1)-inc(2,i+1)*inc(3,i));
Area = Area + triangle;
end
end

——-

The first three lines of the code open the specified file name ‘Rect.bmp’; and uses the function ‘edge’ to obtain the coordinates of the boundaries of the images and saves it as a .jpg file shown in figure 1. The next lines sets the pixel coordinates of the edges to the variables [x1, y1] and uses the functions x and y to adjust the coordinates to the linspace values.  After this, the function theta is specified, and then concatenated to a data set including its corresponding x and y coordinates. I sorted the the data set in increasing theta using gsort() and then implemented the Green’s theorem from lines 12 to 20. Note that the if and then statements were used to account for the case when the we find the area of the triangle formed by the x and y of the first and last theta in the sorted data set.

Rect2
Figure 1. Edged rectangle with dimension 0.8 unit by 1.2 unit

The theoretical area of the generated rectangle is 0.96 square units and the obtained area from the scilab code is 0.9673241 square units. This result gives our code a 0.76% error from the expected that may have been due to the pixelated corners of the edge image as shown in figure 2.

edge
Figure 2. Pixelated corners of the rectangle

I also implemented the same code to a circle of radius 0.5 unit to check for consistency. The edged circle, shown in figure 3 has an area equal to 0.5010732 square units and deviates from the theoretical value by 0.31%. The error from both polygons are below 1%; validating the effectivity of the code used.

Circle2
Figure 3. Edged circle

For the next part, we apply the program to real world applications: measurement of actual areas and object. For this part, I chose the province Batanes for I recall an amazing Milky Way picture taken from that location. At first I though that this would be simple since I only have to produce its silhouette image and then use in my program but I have never been so wrong in my life. The main challenge for me here the generation of the black and white image of Batanes. The first thing I did was to use the fill option in Paint but that did not work since the screenshot of Batanes from Google maps are contoured. I resorted to GIMP and used the free select tool to crop the outline of the province, deleted that part to obtain a white image of the outline and then went back to Paint to produce the final black and white image shown in figure 5.

Batanes
Figure 4. Batanes province
BatanesBW
Figure 5. Black and white image of Batanes

The most essential thing in implementing Green’s theorem in actual regions is that we have to make sure that the origin (0, 0) is at the image center. From the previous cases, I have done this by adjustment by putting the coordinate [x1, y1] in the linspace functions x and y; but for cases where the midpoint is not fixed, we replace lines 6 and y by:

[midx, midy] = size(E) //image center
[x1, y1] = find(E);
Rx = x1+midx/2;
Ry = y1+midy/2;

to place the origin at the center. I have edited the BW image of Batanes several times to make sure that the center of the province roughly coincides with the image center as some edge points may be neglected if I hadn’t done so. The edged Batanes is shown in figure 6.

BatanesBW
Figure 6. Edged Batanes

After putting the image file in my code, I obtained an area equal to 188864.5 pixels. Prior to the conversion of the region, I obtained the pixel to kilometer ratio; 29:1 giving us an area roughly equal to 224.57 square kilometers. From Wikipedia, the actual area of the mainland Batanes is 219 square kilometers giving us an error equal to 2.54%. This small deviation might have resulted from errors in cropping the outline of the regions. There might also be areas where the radial line intersected two edge points causing an increase in the total area computed.

Lastly, we study the effectiveness of ImageJ in obtaining the length and area measurements of objects. I scanned a KFC flyer shown in figure 7 with a resolution of 360 dpi and measured the actual length of the ‘P680 save P70’ box as a basis for the pixel to cm conversion.

EUD page006
Figure 7. KFC flyer

After setting the conversion factor, I measured the areas of some elements found in the flyer using the Measure option after obtaining the outline via the rectangular, oval and freehand selection tool:

Area of KFC box (until save P70 w/o the trianle on the right side): 6.573 square cm

Good for sharing: 4.58 square cm

Lid of the KFC bucket: 2.286 square cm

Green balloon: 3.132 square cm

Most of the regions measured are irregular, and the only way we can verify our measurement is through the area of the KFC box. The box’s actual width measured with a ruler is 2 cm, giving us a 0.41% error which is small enough to validate the accuracy of this technique in measuring the area. As with the computation of the area of Batanes or any other irregular space for that matter, the most important thing, which is simultaneously the most possible cause of error is the selection or the outlining of the region to be measured. This was actually the main challenge in the last two parts, since accidentally cutting a part of the region to be measured could greatly affect the area measured.

test
Figure 8. Sample irregularly shaped region

One limitation of the first technique, as I mentioned above, is the ‘extra’ triangle measured if the radial lines drawn intersect two edge points of the region to be measured. For example, we could look at the irregular region shown in figure 8. Assuming that the two pairs of points in the bases of the rectangles are consecutive points; if we apply Green’s theorem from the center of this region, we obtain an additional area from the space. One way to solve this is to divide the region to be measured into parts in such a way where no overlapping triangles will be produced. For someone like me who isn’t well versed (yet! 🙂 ) in photoshop though, additional cutting of regions might entail further errors if the cutting is perfected. For the case of ImageJ, I think the algorithm it uses counts the total number of pixels then uses the conversion factor to find the length or area in the desired unit. If this hunch is true, then the aforementioned problem with the Green’s theorem does not apply with ImageJ.

Overall, I really liked this activity since it could easily be applied in different areas of study such as in medicine and geography. And since we already have successfully implemented the Green’s theorem in a program, we could measure the area of any 2D region provided that we know the conversion factor. For this activity, I rate myself 10.5/10 or 11/10 if decimals are not allowed since I was able to do everything that was asked for and I tried to extend this work by mentioning a limitation of the process used and provided a way to solve it.

Cheers!

References:

[1] Soriano, M., “Length and Area Estimation in images,” 2015.

Leave a comment