Monday, June 29, 2009

ACTIVITY 2 Area Estimation for Images with Defined Edges

For this activity, the area of several shapes (shown below) with defined edges was calculated both analytically and using Green's theorem. The area was calculated analytically using the known formulas for computing the area of regular polygons, i.e. circle, rectangle, triangle and trapezoid. Some of the shapes below are formed by combining the different regular polygons. Arrow = triangle + rectangle; Heart = 3 triangles; letter T = 2 rectangles. The area of these shapes were calculated by simply adding the area of the regular polygons forming the shapes. The dimensions (length/width/height) of the shapes were determined by counting the number of pixels along the border. Consider the rectangle in Figure 2 as an example. By knowing the pixel coordinates of the corners, it would now be easy to determine the dimensions of the rectangle.

Length = (192-57) + 1
Width = (145-55) +1

Area = Length x Width = 136 x 91 = 12376

Figure 1. Arrow, circle, heart, triangle, letter T, rectangle, trapezoid.

Figure 2. This was done using Paint, zoomed to 800%.

Below is the programming code used to calculate the area of the shapes using Green's theorem. The Green's formula in discrete form used for computing the area of enclosed regions is given below. (x,y) are the coordinates of the points along the contour line bordering the region. If it is already the last coordinate, then it must be multiplied to first coordinate to be able to close the contour line, a requirement for Green's theorem.

Since the image used is in JPEG format, it has to be binarized first, i.e. in terms of 1's and 0's only (see line 2). Line 4 of the code traces the the contour of the shape and outputs the pixel coordinates along the contour line. Lines 6-13 is the code for the Green's formula. Another way of checking if the calculated area is correct is by adding the 1s in the binarized image (see line 14). However, it must be noted that the outermost pixels or portion of the white area is used as the boundary/contour line of the region. This means the two calculated areas, from Green's theorem (area 1) and by summing the 1s (area 2), can never be equal. To account for this error, the number of points along the boundary (size of the output of line 2) is added to area 1. When this is done, area 1 now becomes greater than area 2. But if only half of the size + 1 is added, the calculated areas are now equal. (+1) can be explained by looking at the plot of the points along the contour line (see Figure 3). It can be observed that the curve is not closed, lacking one point/pixel.

1 image = imread('F:\Documents\AP186\activity 2 & 3\pics\arrow.jpg');
2 binarized_image = im2bw(image,0.5);
3 imshow(binarized_image)
4 [x, y] = follow(binarized_image);
5 plot2d(x,y)
6 s = size([x,y]);
7 xi = [x(1:s(1)-1)];
8 xi1 = [x(2:s(1))];
9 yi = [y(1:s(1)-1)];
10 yi1 = [y(2:s(1))];
11 sum1 = sum(xi.*yi1) + x(s(1))*y(1);
12 sum2 = sum(xi1.*yi) + x(1)*y(s(1));
13 area 1 = 0.5*(sum1-sum2) + 0.5*s(1) + 1
14 area 2 = sum(binarized_image)


Figure 3.


The table above lists the shapes used and their area calculated analytically, using Green's theorem and by summing the 1s. The % error is very very small, so it means the code above is highly accurate in calculating the area of shapes with defined edges. There is still error probably because, although defined, the edges are still pixelated.

I would give myself a grade of 9 for this activity because I was able to calculate the area of 7 shapes. Not perfect score because I cannot explain why only half the size of the border points has to be added.

I would like to thank Mr. Luis Buno III, Mr. Miguel Sison and Ms. Jaziel Vitug for answering some of my questions.

Monday, June 22, 2009

ACTIVITY 3 Image Types and Basic Image Enhancement

There are four basic image types: truecolor, grayscale, indexed and binary. Below are examples of each of the image types. We checked the image type by looking at the bit depth in its properties when we right click the image. If the bit depth is 8 and the image is black and white, then it is a grayscale image. If the image has many colors, then it must be an indexed image. For truecolor images, their bit depth would be greater than 8. Moreover, the image is binary if the bit depth is 1.

Figure 1. Truecolor Image
http://home.earthlink.net/~richditch/images/burrowingowl20.jpg


Figure 2. Grayscale Image
http://fourier.eng.hmc.edu/e161/lectures/contrast_transform/node3.html


Figure 3. Indexed Image
http://www.creative-flow.net/tut5.gif


Figure 4. Binary Image
http://en.wikipedia.org/wiki/File:Neighborhood_watch_bw.png

Shown below in Figure 5 is a grayscale image and we want to determine the area of the apple in the image. Looking at the histogram in Figure 6, we can say that our region of interest (ROI) is well separated from its background. Therefore, we can use the programming code generated in activity 2 to calculate the area of the apple. The detailed description of the method can be found in the previous blog. Recall that it uses discrete Green's theorem for computing the area of any enclosed region. Before proceeding, we have to convert first the grayscale image into a binary image (in terms of 1's and 0's only) using the function im2bw(). The threshold value to be used in the conversion must be properly chosen to ensure that only the ROI will be in 1's (white). Looking again at the histogram of the image, we see that or ROI has pixel values ranging from ~150 to ~256. The ratio of this is 0.68, so we used 0.65 as our threshold value. Figure 7 shows the contour traced in the image, which was used in implementing Green's theorem. The area calculated by the program is 2871. We validated it by summing the 1s in the binary image and we got the same result. Therefore, we are successful in calculating the area of the apple. We have also shown in the previous blog that the method we used is accurate to about 1%. And for this, I would give myself a grade of 9. Just 9 because I was super late already in finishing this activity.

Figure 5. Grayscale image

Figure 6. Histogram of the grayscale image.

Figure 7. Contour of our ROI, i.e. apple.


I would like to thank Mr. Luis Buno III and Mr. Miguel Sison for their help..

Wednesday, June 17, 2009

ACTIVITY 1: DIGITAL SCANNING

This activity is designed to experience how measurement of the actual dimension of the object is being done given only the image of the object. By knowing the relationship between the pixel location in the image and the physical variable, one can already tell the object's actual length. This method is not easy and erroneous but I guess it is the simplest technique for a rough estimation.

I got the graph I used from a paper entitled Electrolytic Determination and Separations with the Use of a Rotating Anode from The Journal of American Chemical Society Volume XXIX of 1907 (January 25 1907). Thanks to the author of the paper, Julia Langness, for this graph.

Thanks to Mr. Luis C. Buno III for helping me in rotating the image using MATLAB and answering some of my questions. The image had to be rotated since the scanned image is tilted.

Hand-drawn graphs are indeed difficult to reconstruct. The gridlines of the graph are unevenly thick and not aligned, thus, unevenly spaced. Due to this problem, the determination of the the equivalent pixel location in the image of a point in the graph is very much prone to error. The table below shows the pixel location of the ticks in the X-axis (Minutes) and the Y-axis (Grams). It can be seen that the difference between two consecutive ticks in the X-axis greatly varies ranging from 103 to 115, a very large deviation by 12. The same deviation in the Y-axis can be observed. To minimize the error due to the large deviation in reconstructing the gridlines of the graph, the mean of the differences was used.

By the way, the pixel location of the ticks in both axes was determined using the Paint program and then tabulated using Microsoft Excel. Instructions for determining the pixel location can be found in the manual for this activity.

Equations relating the pixel location to the physical variable in both axes were established. The proportionality factor was obtained by getting the ratio of the increment per division to the mean difference. It must be noted that the origin of the graph does not correspond to the (0,0) pixel location of the image. To account for this offset, the (X,Y) pixel location of the origin was subtracted from/to the pixel location of the points in the graph. Hence, the equations are the following:

X-axis: minutes = 2*(1355-x)/113
Y-axis: grams = 0.05*(y-67)/109

Several points to represent the best fit line, including the data points, were selected to be able to create a smooth reconstructed curve. The pixel location of each of these points were determined. The reconstruction of the graph was done by converting the pixel locations back into their equivalent physical values using the generated equations.
Note that the best fit line is also unevenly thick, so this introduces additional error.

The fun part in this activity was determining the pixel locations in the image of the points in the graph. Wait, was it really fun? hhhhmmmm... Yeah, I had fun especially because this is the first activity. But, it was also tiresome.. :( and painful to my eyes since I had to stare for so long at the computer. The frustrating part, on the other hand, was the next thing to be done. That is, overlaying the image of the graph on the reconstructed graph It wasn't supposed to be frustrating but I wasn't able to find out immediately how to overlay the image. And my seatmate was done already. Time was past 11am. Our class is until 11:30am only. So, I became a 'lil bit' fidgety. Thanks to the power of Google, I figured it out how to overlay an image as a background on a chart.

To overlay an image in a chart using Excel, the following must be done:
Go to Format Plot Area. Probably by pointing the mouse to the Plot Area and then right click.
Then, in the Fill tab, choose the option Picture or texture fill. Insert the image you want to overlay.

Below is the figure of the reconstructed graph with the original graph overlaid on it. The gridlines, as well as the reconstructed curve, were made thicker to compensate the errors (uneven thickness of the lines in the original graph) that were introduced above. It must be noted that the original graph has to be cropped properly so that it fits, more or less, the reconstructed graph.

Looking at the figure above, it can be seen that the graph is not perfectly reconstructed. Some of the reconstructed and original gridlines do not coincide. Same also with the curve and the data points. The reasons for this are already explained above. It is because the gridlines are unevenly thick and unevenly spaced.
The author of this paper is probably better in drawing horizontal lines than vertical lines. :) As you can see above, more vertical gridlines are inclined.

Although I was not able to perfectly reconstruct the graph, which isn't my fault (hehehe), I think I still deserve a grade of 9. :) Why? I didn't finish it on time but I believe I did this activity correctly as I have explained what I did above.

Guys, please comment!!! :) Tell me what you think about what I did and what I said above.

Monday, June 15, 2009

why WANNA EAT SIX KAMATIS????

WANNA - 1na
EAT - 8
SIX - 6
KAMATIS - wala lang, yun kasi naisip ni thirdy

===>
ito ang title ko dahil ang blog na 'to ay para sa Applied Physics 186 class namin...