Sunday, June 28, 2009

Activity 2 Area Estimation for Images with Defined Edges

When I first heard or thought about it, area estimation did not strike me at all as something complicated or even useful. I was already aware of how area estimation played a role in agriculture, agrarian reform, and other endeavors involving large land areas (such as land grabbing and proffesional squating... haha). But as Ma'am Jing narrated and enumerated all the instances where estimating an area was the key task in the creater scheme of things then I started to realize what a powerful tool it can be. It was quite an eye opener when she shared how the study and treatment of skin cancer heavily relied on area estimation which was done manually. I also remembered that my cousin (a marine science student) once told me that in marine science measuring the total area of leaving corals is a very important task. This made me realize that area estimation is not as simple as it once seemed. Moreover measuring irregularly shaped areas is difficult enough but teaching a computer to do it is even more challenging. In this activity we utilize scilab and its SIP toolbox to write an area estimation code using Green's theorem.

Green's theorem has been and is still used in various area estimation algorithms. There is even an old area estimating instrument called the planimeter (http://en.wikipedia.org/wiki/Planimeter), used in irregularly shaped areas, that is justified by Green's theorem. Named after British mathematician George Green, it is a mathematical relationship of a double integral and the line integral along its boundary (http://en.wikipedia.org/wiki/Green%27s_theorem).
Given that the partial derivatives of F1 and F2 exists and are continuous within the region R we can take F1 = 0, F2 =x and F1 = y F2 = 0 and obtain two line integrals which both gives the area of R. The average of this two, equation 2, gives a better measure of R and equation 3 is the discreet form if this integral, where N is the number of points along the boundary contour.
Based from this concept I wrote a program in Scilab making use of the SIP toolbox to access the image and the required information from it. In this basic implementation we make use of the function follow() to obtain the boundary contour of the area of interest. But follow() can only handle binary images so the function im2bw() is utilized to convert non-binary images. Also, as we can see from equation 3, the descreet algorithm for Green's theorem makes use of one more point over the length of the contour. The simple adjustment I made was to simply add the starting point/coordinate as the end point of the contour. This is a valid adjsutment since there is no extraneous information added and in fact this last point necessarily closes the contour which was initially open. Also I used pixel counting as the basis for checking; that is, I was to compare the area calculated by the algorithm against the total number of white pixels. This made sense since in this case all of the images were created using paint and none were real photographs. As it is the program was fit enough to run and implement Greens theorem.

The trial run of the program aimed to calculate the area of a square with one side measuring 178 pixels but, contrary to what I expected, it returned the wrong value. At first I suspected that the difference was that the algorithm did not account for the boundary and Dr. Soriano shared this thought, but I already saw from the values that it was not the case. Then I immediatley noticed that the calculated area was that of a 177 pixel sided square. After some serious thinking and contemplating I had an indea of adding half of the boundary contour. For the square area this was obviously sufficient since the only difference of 177pixel and 178pixel on a side is half of the perimeter, still I had a feeling that this would be valid for all the other shapes. After modifying the code and testing it on sqaures, circles, triangles, and various irregular shapes I was amazed that the program was 100% accurate each time (table 1). Even very crooked curves were not a problem.
Then I tried the program for more irregular shapes and saw a limitation. once there is a discontinuity to the black background the program could not handle it. This means that if there are two seperate white areas then the program would only see one of them. Also if the shape was a donut or any other that had a whole or if the white areas were only connected by a white line of 1 pixel width then the program would return the wrong value. I traced this limitation back to the function follow(). This function can only follow a single contour that did not intersect itself, faced with any other case and it would not work for our purposes.

Overall I still consider this as a huge success and I have to give myself a pat on the back for such good work. For this I'm giving myself a grade of 10!!! woooohhooooo!!!!

I would first like to thank Dr. Soriano for all the information and insights she shared. I would also like to thank Thirdy for helping me in discovering the limitations of the follow command.

square
triangle

circle

tshirt 1

tshirt 2

irregular

irregular 2

irregular 3

Im = imread('circle.jpg');
Im = im2bw(Im, 0.5);

[x,y] = follow(Im);
len=length(x);

x(len+1)=x(1);

y(len+1)=y(1);


//theoretical area via pixel counting

A1 = sum(Im);

//the additive term 'round((len+1)/2)' accounts for the sections of the boundary that is not included in the sumation
A = round(sum((x(1:len).*y(2:len+1)-y(1:len).*x(2:len+1)))/2) + round((len+1)/2);


//area checking

A==A1

1 comment: