Tuesday, June 29, 2010

Entry 10: Area Estimation of Images with Defined Edges

Hello guys! I'm back again.  This time, we are tasked to play around with a method for computing the area of closed curves with defined edges in images.  This exercise will make way for us to understand the capability and flexibility of image processing in useful applications like measuring the area of a lot defined in Google map.  Applying the computational method used in this activity, for example, in evaluating the extent of a catastrophic event such as forest fire using its aerial images; we can save a lot of resources as compared to actual evaluation of the place.


FAQ

What method can be used in evaluating the area of a figure with defined edge?
  • The Green's Theorem is the most utilized method in finding the area in this activity.

What is Green's Theorem?
  • Green's Theorem is a mathematical construct which relates the line integral of the contour of a figure to the area of the figure enclosed by that specific contour.  The Green's Theorem is mathematically represented by

                                          (1)


How can we use Green's Theorem to derive the area of a closed figure with defined edge? 
  • From the Green's Theorem, the area of a continuous curve can be evaluated by the following equation

                                                        (2)

 In computational approach, this formula will be helpful after applying discretization method.  The discretized form of (2) is given by
                                                     (3)

This equation can now be used to create a programming algorithm to derive the area of a closed figure with defined edge.


Are the numerical calculations of the area accurate using the Green's Theorem method?
Applying the method to a circle wherein we know the actual analytic area, we can estimate the error produced by the method.


First Test:
  • Circle: r = 70 px
  • Numerically computed area = 15, 071 px^2
  • Analytically computed area = 15, 394 px^2
  • Error = 2.10%

Second Test:

  • Circle: r = 140 px
  • Numerically computed area = 60, 865 px^2
  • Analytically computed area = 61, 575 px^2
  • Error = 1.15%

Third Test:

  • Circle: r = 280 px
  • Numerically computed area = 244, 905 px^2
  • Analytically computed area = 246, 301 px^2
  • Error = 0.57%


From the three tests, we can observe that as the pixel dimension of the object whose area is to be calculated is larger, then the error of the computed area using Green's Theorem decreases relative to its analytic area.


How can you apply the computation of area using Green's Theorem in daily life?

One of its vast applications include the determination of area of a certain region in a map.  As an example, I chose to measure the area of the College of Science Administration Building using this method.

Map Calculation (College of Science Area):
Computed Area in pixels = 20852 px^2

Scale:
0.934579439 ft/px
0.285714285 m/px

Using the scaling factor taken from the scale accompanying the image of the map, we can approximate the area of the CS by simply multiplying the scaling factor with the calculated value returned by the Green's method.

Approximated Land Area of CS = 5, 957.71 m^2
Actual Land Area  = ?



Image of the CS admin bulding taken from Google Maps!..

Summing up everything discussed in this How-To++, we can say that the Green's method offers a convenient way of calculating the area of a region shown in a map.  This is quite a resourceful and less rigorous approach to measuring area of regions with defined edges.

It must also be noted that the larger the size of the region in pixels the more accurate the result of the area measurement.  Thus, the converse of the event must be taken with high caution.  Take into consideration that if you dealt with a region with a small pixel area, the resulting measurement has a high margin of error which may lead to wrong conclusions.

In finality, I want to consider treating myself a 12/10 for this activity for the effort I expended in putting things together and tried to be creative (A simple FAQ outline) in presenting this blog... :)

Source:
    Activity Sheet for Activity 4 - Dr. Maricor Soriano
    Google Maps

Friday, June 25, 2010

Entry 9: Image Types and Formats

Images are all around us! When we open our beloved desktops, laptops, phones and other graphical devices, they are the first things that we can see.

Because of the ubiquitousness of images, we often neglect many of the attributes and the complexity of producing one. In this activity (Activity 3), we were tasked to devote some time on researching about the details of different image formats and types.

Amazingly, I was so surprised that many available image formats are available in the open yet I didn't know they exist at all (thanks a lot to this activity because I learned some new things again).  Indeed, there are lots of things that I would like to discuss regarding images after researching about its different file formats, types, and other technical yet useful and informative issues.

Images come in different (basic) types namely;  binary, grayscale, truecolor and indexed images.  These types of images are distinct in a way of how image pixels are represented.  Some types are discussed belong including a brief summary of their properties (this was derived using the command imfinfo() in scilab with SIP toolbox loaded.)

Image types (Basic):
  • Binary Image - characterized by a bit-sized pixel value (i.e. each pixel is allowed to take only a value of 0 or 1).  These pixel values only represent black and white and are commonly used in document images.
Binary image

FileSize: 14756
Format: JPEG
Width: 400
Height: 333
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: inch
XResolution: 96.000000
YResolution: 96.000000

  • Grayscale Image - represented by pixel value ranging from 0-255 (8-bit in binary or x00-xFF in hexadecimal.)  This type of image can represent variations in intensity because of its available intermediate values which differs in the binary image which is restricted only to 2 values.
Grayscale Image

FileSize: 41126
Format: JPEG
Width: 400
Height: 391
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

  • Truecolor Images - they are images composed of 24-bit valued pixels.  Method on how this image is formed is by overlaying three 8-bit/pixel matrices representing the intensity distribution for the Red, Green and Blue (RGB) components of the image.  This results to a rich image having a range of up to 16,777,216 million colors.

FileSize: 189687
Format: JPEG
Width: 500
Height: 493
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: inch
XResolution: 72.000000
YResolution: 72.000000

  • Indexed Images - this type of image is characterized by the mapping of index values representing the image's colors to a colormap all contained in the image file.
 Indexed Image

FileSize: 33273
Format: GIF
Width: 290
Height: 360
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 72.000000
YResolution: 72.000000

Image Types (Complex):
  • High-Dynamic Range (HDR) Images - extended version of grayscale having a higher range of allowed pixel value ranging from 10-bit to 16-bit.
 

  • Multi or Hyperspectral Image -  this image type is more sophisticated than a truecolor because it is a result of overlaying not only 3 matrices of color values but at least 10 bands of colors and that gives it its eerie look (just joking).

  • 3D Images - this image type is practically complex starting from how its created until how 3D images are to be viewed.  The most common way of recording a 3D image is by stereoscopy and these images can be viewed using special 3D glasses.
 3D Images


This time, let's try to have some image processing using Scilab.  Honestly, I am amazed how easy Scilab deals with stuff regarding images.  You can do things in just a single command.  But Python is still the Number 1!  Going on, we processed a truecolor image by converting it into a binary image and grayscale image and checked whether how conversion affects the resulting image.

Original Image
 

//code snippet


I = imread('nature.jpeg');
size(I)


ans = 
        300.    400.    3.


Binary Image Version

BW = im2bw(I, 0.48); //converts truecolor to binary image
size(BW)

ans = 
        300.    400.


Grayscale Image Version


GR = im2gray(I); //converts truecolor to grayscale
size(GR)


ans = 
        300.    400.  


We can infer from the output of the command size() that the conversion from truecolor to either binary or grayscale flattens the image matrix.  Note that the returned value in truecolor is 3-dimensional array while after conversion, it only consisted of a 2-dimensional array.  The dimension which was lost during conversion represented the channels in the truecolor image.

Our next task was to manipulate the image used in Activity 1.  I visited my archive for Activity 1 to search for my digitized hand-made graph and tried to separate the data from the background plus other noise using image processing.
  • First I imported the image to Scilab.
                I = imread('old_graph.png');
                imshow(I, [])

  • I converted the image into grayscale.
                GR = im2gray(I);
                imshow(GR, [])



  • The histogram of pixel values was taken.
                histplot(linspace(0, 1, 256), GR)


  • I used the detail from the histogram to find a suitable threshold value in converting to binary image.
                BW = im2bw(I, 0.58);
                imshow(BW, [])



Now, I was able to separate and remove noise from the actual data curve.  The next thing to do is to search for image formats and discuss things about them.

Image formats:
  • JPEG or JPG (Joint Photographic Experts Group) - the most widely used of the available image formats.  Most digital cameras are set to have this image compression format.  This is highly flexible since the user can choose the compression level which determines the proper trade off between quality and size.
  • GIF (Graphics Interchange Format) - an 8-bit color image compression format widely used in the internet because of its portability and has a relatively acceptable quality but not so large file size.  This image format utilizes a lossless image compression.
  • PNG (Portable Network Graphics) - an improved version of the GIF format which supports 24-bit colors as opposed to GIF which only has 8-bits.  PNG utilizes a lossless compression method which preserves the image quality.  The PNG is celebrated for it is an open source image format.
  • BMP (Bitmap) - widely used in Microsoft Windows OS.  They are easy to handle but it's not compressed and as a consequence, it has a large file size.
  • TIFF (Tagged Image File Format) - this file format is highly accepted in photography.  It is commonly compressed in lossless type but can sometimes be lossy.  It is usually comprised of 24-bit or 48-bit color values.  This format is usually intended for images having important details (scientific and document imaging).

To sum things up, I would rate myself a 10 because I have properly perform all the required stuff.  I learned a lot also from this activity, indeed I really do appreciate images now!

References:
  • Dr. Maricor Soriano - Image Types and Formats (Handout)
  • Wikipedia
    • http://en.wikipedia.org/wiki/Image_format
    • http://en.wikipedia.org/wiki/JPEG
    • http://en.wikipedia.org/wiki/Tagged_Image_File_Format
    • http://en.wikipedia.org/wiki/Portable_Network_Graphics
    • http://en.wikipedia.org/wiki/Graphics_Interchange_Format
    • http://en.wikipedia.org/wiki/BMP_file_format

Tuesday, June 22, 2010

Entry 8: Digital Aperture, Scilab, Python and Diffraction

The activity's objectives are generating digital versions of physical apertures and familiarize with the capabilities of Scilab for image generation and manipulation.

When I was doing this activity, an idea came to me. I remembered that the Fraunhofer diffraction of an aperture is the fourier transform of the transmittance function. In context, the transmittance function in real world experiments is simply the same as the things we're making (only digital versions).

But before going too much about fourier transform, the task was to make 5 patterns, namely; square, sinusoid along the x-axis, a grating in the x-axis, an annulus and an aperture with gaussian transmittance.

Dr. Soriano gave us a gist of how things should be done. She wrote for us a code to generate a circular pattern.

Fig 1: Circular aperture
//circle
nx = 500; ny = 500;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);[X,Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2);
A = zeros(nx,ny);
A(find(r<0.5))=1;
imshow(A,[]);
imwrite(A, 'circle.png')


 So the other apertures I created are the following (Fig 2.):

Square Aperture (Upper Left)
Sinusoidal Filter (Upper Right)
Grating (Center)
Annulus (Lower Left)
Gaussian Filter (Lower Right)


Fig 2: Digital apertures and filters



After generating the images above, I tried to find their resulting fourier transforms. The resulting image after taking the fourier transform of the original image would have a physical equivalent of the aperture's diffraction pattern at large distances (Fraunhofer Diffraction).

The resulting patterns are the following (Fig. 3):

Diffraction Pattern of a Square Aperture (Upper Left)
Diffraction Pattern of a Sinusoidal Filter (Upper Right)
Diffraction Pattern of a Grating (Center)
Diffraction Pattern of a Annulus (Lower Left)
Diffraction Pattern of a Gaussian Filter (Lower Right)


Fig. 3: Fraunhofer Diffraction Patterns for each aperture/filter



For this activity, I would rate myself a 12. I think, I have attained the objectives of this activity satisfactorily. I have finished all the required outputs and I extended the study to cover its applications (simulation of Fraunhofer Diffraction for each aperture/filter). I also wrote the corresponding Python codes for the generation of the same images (Appendix 2).


Appendix 1 (Scilab Codes):

//square
nx = 500; ny = 500;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
del = 0.05;
xr = (X>-del). * (X-del). * (Y<0.1))=0;>0.2))=0;
imshow(A,[]);
imwrite(A, 'annulus.png')

//gaussian
nx = 500; ny = 500;
x = linspace(-10,10,nx);
y = linspace(-10,10,ny);
[X,Y] = ndgrid(x,y);
r = sqrt(X.^2 + Y.^2);
r(find(r>=10))= 0;
A = exp(-(r.^2)/2);
A(find(A==1))= 0;
imshow(A,[]);
imwrite(A, 'gaussian.png')


Appendix 2 (Python Codes):

#circle
nx = 500; ny = 500; r = 0.5
X, Y = meshgrid(linspace(-1,1,nx),linspace(-1,1,ny))
A = zeros([nx, ny])
circ = sqrt(X**2+Y**2)
A[where(circ < r)] = 1 

imshow(A) 
savefig('circle.png') 

#square nx = 500; ny = 500; cx = nx/2; cy = ny/2; r = 10 
square = zeros([nx,ny]) 
square[cx-r:cx+r, cy-r:cy+r] = 1 
imshow(square) 
savefig('square.png') 

#sinusoid nx = 500; ny = 500 
sx = sin(linspace(-6*pi, 6*pi, nx)) 
X,Y = meshgrid(sx,linspace(-1,1,ny)) 
imshow(X) 
savefig('sinusoid.png') 

#grating nx = 500; ny = 500 inc = 20 
A = zeros([nx, ny]) 
A[:,arange(0,nx,inc)] = 1 
imshow(A) 
savefig('grating.png') 

#annulus nx = 500; ny = 500; r1 = 0.1; r2 = 0.2 
X, Y = meshgrid(linspace(-1,1,nx),linspace(-1,1,ny)) 
A = zeros([nx, ny]) 
circ = sqrt(X**2+Y**2) 
A[where(circ < r2)] = 1 
A[where(circ < r1)] = 0 
imshow(A) 
savefig('annulus.png') 

#gaussian nx = 500; ny = 500; r = 5 
X, Y = meshgrid(linspace(-10,10,nx),linspace(-10,10,ny)) 
A = zeros([nx, ny]) 
circ = sqrt(X**2+Y**2) 
A[where(circ < r)] = 1 
Z = exp(-circ**2/2)*A 
imshow(Z) 
savefig('gaussian.png')

Thursday, June 17, 2010

Entry 7: Applied Physics 186 - Activity 1 (Digital Scanning and Evaluation)

The rationale of this activity is to identify the actual physical values from digital graphs in scientific journals. The advantage of performing this method is that it gives you a very close approximation to what the real/physical values shown in the graphs. Also, with proper computational tools, one can derive the functional form of the curve itself; this further helps you in the analysis of the physical quantity you are concerned with.

The procedure starts by choosing a graph from a printed journal and followed by the digital conversion of the raw image (Fig. 1).


Fig. 1: Digitized copy of the raw image from a printed journal

To know the corresponding physical value of any point on the curve, it is essential to determine the scaling factor of the pixel dimensions in x- and y-axis. The scaling factor is equal to the span of two successive tick marks in pixel dimension relative to the absolute value of the difference between the physical values represented by the two tick marks.

$\color{white}SF_i\ =\ \frac{\Delta (pv)_i}{\Delta (pd)_i}$

where $\color{white}SF_i$ is the scaling factor, $\color{white}\Delta (pv)_i$ is the absolute value of the difference of the physical value at the tick marks and $\color{white}\Delta (pd )_i}$ is the absolute value of the difference of the pixel locations of the tick marks.

Example:
In the x-axis:
Two successive tick marks are located at 2740 and 2273 and the physical value associated to the two are 550 and 500, respectively. Therefore, the scaling factor $\color{white}SF_x$ = 0.107nm/px

Now to compute for the physical values in the graph, it must be considered that the location of the origin of the graph is not located at the origin of the image array. Thus, find a strategic pixel location which will be your reference for further computation of the values and note this as X and Y. In my case, I choose the location X = 876.946 px, and Y = 2851.77 px which represents the location for (350nm, -2).

Arbitrarily get sufficient number of points from the curve by noting their pixel locations (x, y). Use the equation below to generate the physical quantity of the point.

$\color{white}P_x\ =\ R_x\ + SF_x (x\ -\ X)$
$\color{white}P_y\ =\ R_y\ + SF_y (Y\ -\ y)$

where $\color{white}P_x$ and $\color{white}P_y$ are the computed physical value, $\color{white}R_x$ and $\color{white}R_y$ are the physical value of the reference point (X , Y). The x and y are the values of the pixel locations as noted beforehand.

Using Microsoft Excel, I plotted the results of the conversion of the point values and generated Fig. 2.

Fig. 2: Reconstruction of the derived physical quantities from pixel values

To verify the "validity" of the conversion used, I superimposed the derived curve with the actual digitized image. Ideally, when the two curves are nicely fitted; qualitatively, we can infer that the conversion of values is correct. The superimposed image is shown in Fig. 3.


Fig. 3: Nicely fitted curves of the raw image and the graph of the converted data

Basing from Fig. 3, the reconstruction made was good if not better, since it can be observed that the two curves almost overlap each other.

Things to do: Use interpolation methods to find the polynomial form of the curve.


Self Evaluation:

The activity was very refreshing and it made me realize that I have missed a lot of useful capabilities Microsoft Excel and OpenOffice Spreadsheet have to offer. Now I have learned that you can embed an image in a graph as a background and use it as a reference.
Again, I was reminded that reading the instructions first will help you a lot; especially in saving time.
After everything, I would bravely rate myself a 12 because of the new things that I have learned and that I was able to accomplish essential things to finish this activity.
If I would have enough time, I will try to derive the polynomial relationship of the curve. It could be done by using polynomial interpolation.