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

Wednesday, June 17, 2009

Activity 1

Once we have the required data or information, making graphs, charts, or plots is never a problem for us. With the aide of our modern computers and the numerous calculating and graphing softwares such tasks are usually done efficiently and hassle free. But before the age of even the earliest computers fairly accurate graphs and plots already existed; which is, when you think of it, an amazing feat. I don't know how this plots were made back then, but by simply using the powers of wikipedia we can somewhat get an idea of how scientists back then made use of different types of graphing papers (http://en.wikipedia.org/wiki/Graphing_paper) and odd looking implements (http://en.wikipedia.org/wiki/French_curve) to generate their graphs. In this first activity we recreated a very old plot by using MS Paint and MS Excel.


We took very old handwritten plots from books and journals as far back as the 1920's ,or even older, and scanned them. Now using the select tool of MS paint we took note of the pixel coordinates of all the significant points of the plot. Specifically we took the pixel values of the axis of the plot along with their corresponding physical values indicated on the plot. From these data we obtained the conversion from the pixel values to their physical values. For each tick mark on both axes, we took the difference in their pixel values and also their physical values. From the ratio of these two values we would already have a conversion to use on the curve:

But since the plots were hand drawn these difference in pixel values were not constant, still the variations were not that large so I simply took the average. The more significant concern is that this particular doesn't have an origin with pixel coordinates (0,0) and the plot itself did not even start at 0,0 of the physical values. Also in the image file the direction of increasing pixel value on the vertical was opposite of that with the vertical axis of the plot. Still these problems can be easily addressed by using some basic algebra, point-slope or two-point form of a line, along with some concentration and patience. Doing this I arrived at a modified equation for conversion of the form:

Using the pixel coordinates of the points along the curve in this equation I tried to recreate this plot in MS Excel and compared it with the original. In comparing the two plots we simply use the original scanned plot as the background of the plot in Excel. But upon comparing the recreated curve with the original it was obvious that the two did not match. Then I found out that the x-axis of the plot I chose was not the same through out the plot.

It appears that the origin of the second segment coincided with the 1 value of the first. Also, I found out that the ratio of the pixel values and the physical values of the axes was not the same. I tried to add value of 1 to offset the second segment of the curve and I also used a different conversion for the two parts to improve the appearance of the recreated curve. But as you can see from the figure bellow, though the general shape and trend of the plot was replicated the corresponding points and tick marks did not coincide with the original plot.

After doing a lot of tweaking with the math and even more lucky trial-and-errors I found out that it is not possible to fully reconstruct this plot using this method. So with the permission of my professors I decided to break the curve into two plots and then do the reconstruction.

Finally the recreation is a success...!!! The recreated curve fits very well with the original and even the points and the tick marks are spot on. I would like to give myself a 10 for this but since I was late I guess a 9 would do... hahahahahahhah!!!!!

And since this was a success I have to thank all those who helped me... first my professors Dr. Soriano and Dr. Perez, Thirdy for helping me find this plot, Jica for sending me my files, and Winsome for telling me that I need to sign in before I can do anything with my blog.

Source of Plot: A. Wilmen Duff, et al, "Physics for Students of Science and Engineers", Maple Press Co., York PA, 1926