Thursday, September 8, 2016

Area Kidding Me

($Good \, Job \, Roland \, For \, The \, Title$)

What can I possible hate more than Scilab? MATH dammit.
The first thing I saw in the activity manual was this equation.
And that is how you get the area of a 2D shape. Yeah well goodluck with that (walks out)...

Well turns out twelve lines below that double/contour integral can be approximated to this

Which basically means  the area of a shape can be approximated by taking triangular slices.
Well look at that. It actually resembles the trapezoidal method for approximating integrals except it's in polar form.
All hail numerical approximations. YEY.

(hohoho a horizontal line. Been a long time since I've tried using html. oh look there's another one below)

Sooo anyway. The goal of this activity is to approximate the area of images. How do we do that?

Area estimation in images for dummies
  1. Go learn things. This isn't for dummies.
  2. Get an image.
  3. Make it black and white.
  4. Load it in Scilab.
  5. Get the edges of the shape using edge of the SIVP module.
  6. Remember the second equation above? Use the coordinates of the edge points as Xs and Ys.
  7. Now you have an area in $\rm{pixels^2}$ 
  8. Convert $\rm{pixels^2}$ to whatever unit.

Example.
Here is a cute little white triangol I made in paint with 200px base and 400px height.
Notice how now edges were detected at the bottom because the triangle ended with the frame.
$well \, my \, bad$

My amazing little program said cute little triangol has $39726.5 \, \rm{pixels}^2$. That gives us an amazing $0.6\%$ deviation. $Less \, than \, 1\% \, WOW!$

For a more comprehensive test I used the program to compute for the area of the UP Sunken Garden.
Why sunken? Because thats the only feature I could get from Google Maps without any text blocking the way. Yeah I'm too lazy to clean the picture.

The raw image of our little friend, the Sunken Garden from Google Maps.
Note the little bar scale on the lower right corner indicates that 60 pixels = 20 meters.



Converting the pixel values to meters, my amazing little program was able to obtain an area of $3.22 \rm{hectares}$. Using my subpar drawing skills and the tools provided by our friends over at Google Maps > My Maps, I got an area for the Sunken Garden to be $3.67 \rm{ha}$. Giving my approximation program an output with $12\%$ deviation. $not \, bad$

Remember that conversion I made between meters and pixels? Well now it's time to use ImageJ to do it for us instead of manually counting like a pleb. This is especially useful if the known length in the image is not perfectly horizontal/vertical.

See that yellow line? You should, I even put red arrow beside it. I just draw that over the 20cm ruler and ImageJ does its job and tells us this image has a scale of $118.0512$ pixels/cm. $much \, easy$

Now I just draw lines all over the place and press CTRL + M to get the measurements in centimeters.
It even has a neat table to keep all our data. I can even draw over the edges of not so regular shapes and get the area. $how \, nice$.
The standard Poker Size of Bicycle cards is $6.4 \rm{cm} \times 8.9 \rm{cm}$ 
The measurements of ImageJ are all within $2\%$ deviation from actual measurements.
Using ImageJ I was also able to obtain the area of UP Sunken Garden to be $3.43\rm{ha}$ by simply using the magic wand. No image editing needed. $ez$
This gives a $6.5\%$ deviation with Google Maps measurements.


Well everything worked as planned. I'm starting to adjust a bit with Scilab and didn't have as much difficulty as last activity. I appreciate the find function a bit more now. STILL. $PYTHON \, MASTER \, RACE$

The good thing about not writing a blog at 5:30 in the morning is that I now have a better grasp of HTML and my post does not look like an extended abstract if I must say so. () And I can now write cool $\LaTeX$ stuff. $because \, math \, font \, looks \, cool \, yay$

Well everythings' here but I'm late so.... I give myself a verdict of $7/10$

$good \, job \, wow$

Scilab Code For Dummies
nx = 800; ny = 800; //defines the number of elements along x and y
x = linspace(-400,399,nx); //defines the range
y = linspace(-400,399,ny);
[X,Y] = ndgrid(x,y); //creates two 2-D arrays of x and y coordinates

//im = imread('triangol.bmp');
im = imread('sunkenBW.bmp');
//im = rgb2gray(im); / because I didn't save cute little triangol as 2-channel
E = edge(im); //find em shape boundaries

//Finding edge coordinates with feelings
Xe = X(find(E==%T));
Ye = Y(find(E==%T));

r = sqrt(Xe.^2+Ye.^2); // turns out I didn't need this
angols = atan(Xe,Ye);

M = cat(2,angols,Xe,Ye); // put thetas, Xs, and Ys in a single matrix
Ms = gsort(M,'lr','i');  // for ez sorting

//Applying the Green's theorem approximation like a boss
A = 0
for i=1:size(Ms,1)-1
    A = A+(Ms(i+1,2)*Ms(i,3)-Ms(i,2)*Ms(i+1,3))
end
A = A/2
disp(A) // yeah I used "print A" here and got stuck for hours