Thursday, June 30, 2011

IV

Green's theorem was employed to determine the area of an image region. As we know, the theorem relates a double integral to a contour integral. The area is then now accessible from the data of the bounding contour.

Equation 1. Contour integral of an area bounded by a continuous function

The discrete form of Eq. 1 is then:

Equation 2. Discrete form of a contour integral

Scilab's follow() function extracts the parametric contour of a black and white image, assuming that it has only one definable shape. Using this and Eq. 2, I try it on various circles (generated with our A1 code):




Figure 1. Black and white circles. From biggest to smallest: radius 45, 35, 25, 15 and 5 pixels

Figure 2. Example code for the computation. Note that the if statement simulates a cylindrical symmetry for the indices. 


Comparing it with the theoretical A=Pi*(radius^2) computation, I obtain the following graph:


Figure 3. Radius versus percent deviation graph of the 5 circles

Fig. 3 shows that as the radius gets smaller, the % deviation becomes larger. This is expected; as the circle is only approximated by pixels which are ultimately square in shape. We can already see the imperfections get more noticeable as the radii gets smaller in Fig. 1.

Next, I picked a location on Googlemaps, and use the method to approximate the area in square kilometers. I chose the infamous Bermuda Triangle:

Figure 4. Bermuda Triangle, as seen from Googlemaps. Black lines were added by me to border the region

Now, employing the method used in A3, I converted the Fig. 4 to a black and white image then used Paint to convert it to a purely black and white image:

 Figure 5. Left: Result after using im2bw() with threshold of 0.0001. Right: Final image after cleaning the other parts with Paint and adding the black color fill

With this, I can now find the area using the same method applied on the circle figures as mentioned earlier. However, the result is in pixels. To convert it to the actual area of the region, I have used Photoshop Elements to find the scaling factor.

Figure 6. 500 km is equivalent to 114 pixels. 

With a scaling factor of 4386 meters per pixel, I can now convert the pixel area to actual square meter unit. How do I find for a comparative "theoretical" value for the area? Since the actual region of the legendary Bermuda Triangle varies per author, I can't find an actual official area size. I just settled with measuring the sides (in pixels), then used Heron's method and the scaling factor to find a comparative measurement for the area.

Figure 7. Heron's formula for finding the area of a triangle with sides a, b, and c

Now, Table 2 tabulates all the important data I've obtained from this experiment:

Table 2. Summary of the relevant data for Activity 4


It is evident that there is still a large discrepancy between the Green's and Heron's method of obtaining the area of Bermuda Triangle (2 million square meters). However, being a colossal region, this still came out pretty well with a 0.63% deviation.

Areas with defined edges are now easily computable with the methods I learned from this activity.

Self-Assessment: 10/10   


Thursday, June 23, 2011

III

This activity gives us an introductory course on different image types and formats. I used a black & white image to practice some SciLab image reading commands. The command size() gives the size of my image in its matrix form, giving binary values corresponding to the visible black and white color. It correctly gave the dimensions of 468 x 459 for my image. From this information alone, and without seeing the image itself, we can deduce that it has an almost square shape. Also, looking at the values given by I, we can quickly see if it is black and white or if it's not.

To get more acquainted with file types, I've collected images from the web based from the classification of Activity 3 and investigated the characteristics using Windows and SciLab's imfinfo().


  
Figure 1. Binary image properties 



Figure 2. Grayscale image properties



Figure 3. True Color image properties



Figure 4. Indexed image properties



 Figure 5. HDR image properties 

 There are some discrepancies with the bit-depth that is returned by Windows and SciLab. This could be due to the fact that I'm using an older version of SciLab. As such, image header reading could've changed and that's why SciLab differs from what Windows is showing.

The next figures show further SciLab works for image manipulation. Fig. 6 shows the converted image of my previous True Color (Fig. 3) image to a grayscale one.

Figure 6. Converted image using im2gray() and gray_imread(). Both yield similar results. 

Now, I've tried converting the image from True Color to black & white using im2bw()


Figure 7. Converted image using im2bw()

This command needs an argument for the baseline value that it will zero out. Notice the 0.3 value. Knowing the histogram could give further insight to a better value, but for my image, I just picked 0.3 such that I have a sufficient black & white image.


To give an insight on the application of image processing in data analysis, I try to do a background subtraction for my A1 graph. However, my graph is actually clean and thus the effect is not emphasized.


 Figure 8. Grayscale converted image [source


 Figure 9. histplot() result of above image


We see from Fig. 8 the negligible visual change from the original. I then continue to get the histogram using the histplot() (Fig. 9) and study the grayscale distribution. The aim is to maximize certain parts of the image histogram and threshold values to become either white or black. However, since my image is particularly "smooth", as we can see in it's histogram, there is little change to the final result.

Figure 10. Final black and white converted image 

 I use another scanned image that is visually noisier than my A1 graph image. Implementing the same steps mentioned above, I have improved the clarity of the graph:




Figure 11. Top to bottom: Original, im2bw() transforms with 0.8, 0.7, 0.6 and 0.5 thresholds, respectively.

Figure 12. histplot() of the original image 

 As we can see, lowering the threshold improves the clarity of the actual data. By looking at Fig. 12, most of the pixel values are at the 0.8 to 0.95 intensity. Thus, by examining the behavior of the graphs, I've further understood that im2bw() works by setting pixels with larger intensity values (as can be seen from the histplot()) than your threshold to 1 (white) and smaller values to 0 (black), hence producing the black and white image. Thus by getting farther away from the 0.8 to 0.95 range, we remove most of the data in the picture which is actually noise. We retain the small amount of data contained (area under the curve of 0 to ~0.65) that is our actual data lines.

Note that we also have losses since some pixels in our data graph correspond to pixel values white-d out by the thresholding. Also, one might wonder what happened to the darkening of the image produced with the 0.8 threshold. The conversion of the scanned image to a format with lower channels than the original produced this effect. Lastly, in scanned images, it is not necessary that the actual ink images are the blackest, as we can deduce from Fig. 12.

Lastly, to further elucidate their differences, I have done a side by side comparison of an image saved in the various file formats.





 
Figure 13. Top to bottom: Original [PNG, source],  zoomed-in shots in PNG, JPG, GIF and BMP formatting, accordingly. 


From Fig. 13, I barely notice differences between the PNG and BMP formats, except the filesize. The PNG format was well over 2 times smaller than its BMP counterpart. For the other 2 lossy formats, JPG is significantly better in terms of quality than GIF and marginally smaller in file size.

With our brief introduction to image formats, it seems that lossless formats only optimizes the storage of the image data to minimize size. Lossy formats on the other hand, employ various methods and algorithms to remove some data (with specific considerations, of course) such that the resulting file size is smaller. The amount of data removed, method and impact to the actual visual of the image varies with lossy formatting used.

Self-Assessment: 10/10 

Tuesday, June 21, 2011

II

Today, we tinker with the basics of image processing using SciLab and its modules. From the sample code snippet included in the A2 pdf file, I've plotted a circular aperture. It seems that 1=white and 0=black in pixel color.

 
 Figure 1. Example code and the circular aperture image it produces 

  Employing matrix subtraction, I have modified the example code to achieve the annulus.
 

 Figure 2. Code and image for the annulus

Next is the sinusoid along the x-direction (corrugated roof) image. I still used the layout of the example code for this. Now instead of generating A as a zero matrix, I have used the sine function to form a grayscale image.

Figure 3. Code and image for the corrugated roof image. Note the constant 5 in the argument of the sine function. (f=2/5)


For the grating along the x-direction image, I've just modified the code in Fig. 3 to have binary values. To facilitate the inhomogeneity due to rounding, I've modified the nx and ny and the frequency of the code.


Figure 4. Code and image for the grating along x-axis

For the circular aperture with Gaussian transparency, I've modified the circular aperture with a normal Gaussian function of r.

Figure 3. Code and image for the circular aperture with Gaussian transparency. Sigma = 0.9 

The last image I produced is the rectangular aperture at the center. To achieve this, I used
the A(xi:xn,yi:yn)=k to set the values of the middle segment of the matrix of width 20 pixels.

Figure 5. Code and image for the square aperture. Side = 20 pixels. 

 Due to time constraints and unfamiliarity with the SciLab, I haven't yet been able to code this in a general, precise and efficient form. I will improve on this.

  Self-Assessment: 10/10