Thursday, June 27, 2013

Err, yeah... Activity 5, Area Estimation

Just to get things moving along quickly, I'll just go straight into describing what I did based on mam's instructions.

I made bitmap images from Paint shown below. 
Don't judge mah Paint skilz..

Then I started to code. A few days later, I have this:

//area finding code of Joshua Beringuela, June 2013
//discrete solution found using green's theorem
//analytic solution by counting white pixels

//read image and find edge
oi = imread('C:\Users\Shua\Desktop\untitled2.bmp');
im = bool2s(oi==255);
ie = edge(oi,'canny');

//find center
[ix,iy] = find(ie);
cx = mean(ix);
cy = mean(iy);
//disp(cx,cy);
im(cx,cy) = 0;
ie(cx,cy) = 1;
ie = bool2s(ie);
a = [im ie];
imshow(a);

//sort x,y by increasing theta
nx = ix - cx;
ny = iy - cy;
theta = atan(ny, nx);
txy = [theta; nx; ny];
stxy = gsort(txy,'lc','i');
//disp(stxy);
sx = stxy(2,:);
sy = stxy(3,:);
zsx = cat(2,0,sx);
zsy = cat(2,0,sy);
sxz = cat(2,sx,0);
syz = cat(2,sy,0);

//2 solutions and error
diar = 0.5*sum(zsx.*syz-sxz.*zsy);
anar = length(find(oi==255));
e = 100*(anar-diar)/anar
disp(e, '% error', diar, 'discrete area =', anar, 'analytic area = ');


Basically, what it does is it reads a black and white bitmap image and returns the area using the Green's method and a pixel counting method and the %error between the two. I used it for the square from the first image and got this:

analytic area = 2091.
computed area = 2077.
%error = 0.64.

I also tried it for the triangle and got
analytic area = 96.
computed area = 88.9.
%error = 7.4.


part of the code shows the original image and the outline as a result of the edge function and the center of both indicated by the dot.


A few notes. I used canny for edge detection. I noticed that error increased for a smaller resolution image.

Before any further results, let's get back to what the code does.
After reading the image, we find the edge using the edge function in Scilab.
It would be nice to explore the effects of using the different methods of edge detection when I have time.
The centroid of the image is found by getting the mean value of the points. It is a crude method but it is the average position of the points of the outline. This could be modified to find the average position of the area by simply getting the mean of the white points in the image. The center was subtracted from the outline and the angle was computed. Then, the angle was sorted from least to greatest along with the corresponding x and y values of the outline. Finally, the equation coming from Green's theorem was used and the rest of the lines are for the output.

I would like to brag about how my code did not use any loops at all. I really put an effort to make a nice code. Credits to Floyd for helping me understand what to do and helping me debug at times. I'd also like to point out that this code really took me a long time to finish. I think it would be reasonable to give it 3 class meetings. As it turns out, the code really isn't that difficult. It's just that I am not used to Scilab yet. In fact, this is probably my second real Scilab code, the first one being the Scilab basics. Nonetheless, I'm proud of my code even if it's nothing special, really. Aww. emo. haha. Anyway, I learned a lot of new stuff from Scilab. I got to use imread, imshow, imwrite, edge, gsort among other stuff. I guess, that's what matters.


Now, I start to wonder what mam is really looking for in the blog. To the results.
For the second part of the activity, the code was applied to find the area of the acad oval.
jog jog din dito pag may time.

It was first taken from Google maps and then processed to a black and white bitmap image using photoshop. Finally, the code returns this for the acad oval.

analytic area = 157403.
computed area = 157182.
%error = 0.14.

It would be nice to have the real value of the area of the acad oval and compare it to the result of this code. Actually, a pixel to actual size conversion can be done by following activity 2. However, the acad oval is an irregular shape and using the scale would still be an approximation. I'm pretty sure that somewhere out there is the land area of the academic oval. Anyway, using the code on the acad oval just proves that the code can also be used for irregular shapes with good agreement between the two methods.


 The 'acad not really an oval'

In the end, I would have to admit that I had some fun trying to make the code in an efficient way. There are a few more things that could be done here but I just feel like I've spent so much time on this na. I give myself a 9.

No comments:

Post a Comment