First, we find the area of regular geometric images such as a circle and a square. Next, we find the area of irregular shapes such as a heart and an image resembling a “twitter bird”. Lastly, we apply the two techniques to determine the area of a certain place.
For the two techniques, the image must be converted into a binary image first. The area to be computed is the white part of the image, and the rest is black. This is highly important because the algorithm we will be using assumes this.
The first technique counts the number of white pixels in the image to determine the area. In the process of counting the white pixels, I thought that the value of white pixels in the image is 1 but when I read the image, it turns out to be 255. With this, the code snippet I used is:
e = imread(‘C:UsersdellDesktopAP 186Act 4Stuffbh.bmp’);
disp(length(find(e==255)));
I just “find” the 255 values in the image using the second line.
That’s all for the first technique. Easy isn’t? XD Well, that’s only the first part. The Green’s function is a lot more tedious.
The second technique uses the Green’s function which calculates the area using an ‘edge’. I really don’t want to dig deep much into the “nosebleed”/mathematical part of the Green’s theorem. :p Green’s theorem relates a double integral to a line integral (okay, too much jargons. xD). In layman terms, we use Green’s theorem to relate a ‘line’ to an ‘area’. When we have a closed line (the shape doesn’t matter as long as it does not ‘loop’), we can calculate the area it encloses using the Green’s theorem. For our purpose which uses discrete values, the discrete form is more useful.
So, the next step now is to implement it! And this is the part where most of us got stuck. I bet if you’ll check out some of my batchmates’ blog, you’ll read many problems. First one is there is no follow() function in the newest version of SciLab; you need to install the SciLab Image Processing (SIP) module first. However, there is no working SIP module for windows users as of now. The choice is to either use a lower version of SciLab or install it in a Linux or Ubuntu operating system. As for me, I did not do any of both. Luckily, my friend Ash have found a separate follow() function that is suitable for our version! Yay! Thanks to the blog of Kuya MP, we have solved our “follow” problem. You can check out the separate follow() SciLab function script here. And since we now have a follow function, we can use it readily!
First, I have used the edge function to get the border of the shape I will be using. Next is I execute the “follow()” function script. And then, I get the x and y values of the pixels of the outline (from the edge function) using the follow function. This gives me a clockwise “sweep” of the outline’s pixels. Note that this is important in using Green’s function. The third to the last line of the code above “closes” the contour of the integral. And now, after hours of contemplation, we test the code. First, we test it on geometric shapes: (1) circle and (2) square. Here is what we got: