Monday, June 29, 2009

Activity 2 - Area Estimation of Images with Defined Edges

In this activity, area estimation is the main theme. This is a very common and useful task encountered in image processing, especially due to the very tedious work required in measuring irregular areas. Moreover, it has very important applications in a lot of fields, even in cancer research (Soriano, AP 186 Activity 2 Manual, 2008).
The principle applied here for area estimation is the Green's theorem. From the equation, the area (double integral) can be related to the its contour (line integral):
or in discrete form:
where Nb is the number of pixels in the contour of the area, and x and y are the coordinates on the contour.

For this activity, white shapes were created on a black background in Paint as sample images for area estimation. These images were loaded in Scilab (with SIP toolbox) using the imread function. The images were all generated in a truecolor image type. But for analysis, conversion to a binary (or black and white, 1 bit depth) image is necessary. The function im2bw was used for conversion using a threshold level of 0.5. This would produce an array of ones and zeros, where the ones would occupy the area of the shape.
From the binary image, the coordinates of the contour of the area must be determined. This can be done using follow. This function from the SIP toolbox outputs the x-y coordinates of the contour of the shape on the image. By obtaining this, the discrete formula for the area, shown above, can now be solved.
Now, the function follow obtains the coordinates on the shape, not the boundary of the shape, which is the one needed for area calculation in the formula. This can easily be corrected by adding 1/2 the number of the coordinates of the contour. The correction term can be justified because by considering the area of the coordinates of the contour itself, the formula above is simply adjusted by one, each for x and y. The adjustment translates to the pixels occupying the contour, which is actually the number of coordinates listed that the contour occupies.

The areas of the figures shown below were obtained and summarized in the following table. For comparison, the area of the shapes were obtained analytically, by taking the sum (sum function) of the binary image array of the shapes, since the area occupied by the shape in the array have values of 1, and others are 0. Moreover, the formula for the area of the regular shapes were also calculated based on the number of pixels defining the dimensions of the shapes.

Regular shapes drawn in Paint as samples for area estimation: Square, Circle, Rectangle, Triangle and Oblong.

Irregular shape with the form "3rdy" as a trial for area estimation.

Table of the summary of the area estimation calculations. Units are in pixels.

Notice the precision of the area estimation using Green's Theorem with that of the pixel count and shape area formula. For the regular shapes, the pixel count is exactly the same as the area from Green's theorem. The shape area formula have slight differences for some shapes perhaps due to the discrete image. The pixels have a rectangular (square) shape which is why for the square and rectangle shapes, the values are all the same. For the other shapes, the edges cannot be well defined so there are estimations resulting in the differences. Still, the results are very satisfactory. Even for the very irregular shape, a very accurate area estimate was exhibited.
The limitation for this technique mainly arises from the output of the function follow. The function assumes that there is only one object in the image that must be mapped, as stated in the Scilab SIP toolbox documentation. This is the reason why the irregular shape image presented does not enclose any object (such as inside the 'd') and all characters were connected. If this is not satisfied, only one object on the image would be mapped and a wrong area estimate would be obtained. Moreover, a digitized image also produces some error due to poor estimation of the edges of an object.
For this activity, I think I can give myself a grade of 10 since I think I did a good job in doing the activity and, together with some of my classmates, we determined some of the limitations and needed adjustments needed.
I would like to thanks Miguel Sison and Winsome Chloe M. Rara for significant discussions regarding this activity, especially with the limitations and adjustments for the technique. Also, this activity cannot be done without the guidance of our professors, Dr. Maricor Soriano and Dr. Gay Jane Perez.

Monday, June 22, 2009

Activity 3 - Image Types and Basic Image Enhancement

Here are some sample images of different image types. The image file information were obtained using the function imfinfo in Scilab. Some images were converted into GIF for upload purposes. Click on the link for the original.


Binary Image
(http://svn.apache.org/repos/asf/xmlgraphics/fop/trunk/test/resources/images/fop-logo-mono-1bit.png)
FileName : fop-logo-mono-1bit.gif
FileSize :14185
Format: GIF
Width: 771
Height: 392
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
XResolution: 0
YResolution: 0



Grayscale Image
(http://graphics.stanford.edu/data/voldata/)
FileName: mrbrain-8bit060.GIF
FileSize: 42107
Format: GIF
Width: 256
Height: 256
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0

Truecolor Image
(http://www.flickr.com/photos/sarabetset/2788219273/in/datetaken/)
FileName: Lotus.jpg
FileSize: 32291
Format: JPEG
Width: 398
Height: 400
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0


Indexed Image
(http://wpcontent.answers.com/wikipedia/commons/f/ff/8_bit.png)
FileName: 8_bit.png
FileSize: 38455
Format: PNG
Width: 300
Height: 225
Depth: 8
StorageType: indexed
NumberOfColors: 255
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0


These ones are similar images converted into the different image types using GIMP (2.4.6). The original image was taken from http://www.flickr.com/photos/sarabetset/2788239981/.

Binary Image

FileName: My Umbrella-binary.GIF
FileSize: 3194
Format: GIF
Width: 500
Height: 425
Depth: 8
StorageType: indexed
NumberOfColors: 2
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0


Grayscale Image
FileName: My Umbrella-grayscale.GIF
FileSize: 95985
Format: GIF
Width: 500
Height: 425
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0


Truecolor Image
FileName: My Umbrella.jpg
FileSize: 15084
Format: JPEG
Width: 400
Height: 340
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0


Indexed Image
FileName: My Umbrella-indexed.GIF
FileSize: 64127
Format: GIF
Width: 500
Height: 425
Depth: 8
StorageType: indexed
NumberOfColors: 256
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0

For the other part of this activity, thresholding was done to obtain the region of interest from an image for which area estimation (from the previous activity) would be applied.

imfinfo()
FileName: finger.JPG
FileSize: 70206
Format: JPEG
Width: 640
Height: 480
Depth: 8
StorageType: truecolor
NumberOfColors: 0
ResolutionUnit: centimeter
Xresolution: 0
Yresolution: 0


First, the original image was converted into grayscale:

image = imread('finger.JPG');
grayimage = im2gray(image);


From here, only grayscale values are contained in the single image array. These grayscale values can easily be counted to obtain the histogram of the grayscale image.
Code from Scilab for computing and displaying the histogram:
grayimage = 255*grayimage;
graylevels = min(grayimage):max(grayimage);
histogram = zeros(length(graylevels));
for i = min(grayimage):max(grayimage);
histogram((i-min(graylevels))+1)=sum(1*(uint8(grayimage)==uint8(i)));
end;

scf(1);
plot(graylevels/255, histogram);
xlabel('Normalized Grayscale');
ylabel('Frequency');
title('Histogram of Scanned Image in Grayscale');


Notice from the figure that the x-axis was from a scale of 0 to 1. This would help identify the threshold or level at which to isolate the region of interest (the fingers) from the background and set this as white, and the background black. It can easily be seen that the threshold value can be around 0.6 because most of the image is from the background with low grayscale values and the signal from the fingers occurs above this range.

bwimage = im2bw(grayimage/255, 0.6);
imwrite(bwimage, 'bwfinger.jpg');
As seen in the image above, a black and white image is obtained, with the white signal coming from the region of interest: the fingers. The area estimation technique from the previous activity can now be applied for this image.

Area estimation:

[x, y] = follow(bwimage); //Obtain coordinates of the contour
m = size(x)+1;
n = size(y)+1;
x(m(1)) = x(1);
y(n(1)) = y(1);

a = x(1:m(1)-1).*y(2:n(1))-y(1:n(1)-1).*x(2:m(1));
Area = round(0.5*(sum(a)+m(1)))
//Formula from Green's theorem

If the coordinates of the contour is plotted, the result is:
The outline correspond well with the shape of the fingers in the image. The area taken up by the fingers from the area estimation algorithm above is 54732 (pixels). As a comparison, since the black and white image array have values of 1 for the region of the fingers (otherwise, 0), the sum of the array would also be equal to the area taken up by the fingers. The result is equal to the previous, which is 54732.
This gives a very accurate result and a powerful tool that can be used for measuring (or giving an estimate of) an area. It has very important applications that can be used in various fields (as stated in the previous activity).
For this activity, I think I can get a high grade (hopefully, 10) for doing a good job and also for creating my own code for computing and plotting the histogram.
I would like to acknowledge Winsome Chloe Rara, Miguel Sison, and Dr. Maricor Soriano for the assistance they have given me in doing this activity.

Wednesday, June 17, 2009

Activity 1

Original

Reconstruction

Overlay

This is the final product of the reconstruction of an old plot scanned from the paper: "Contributions to the Chemistry of Thallium I" by L. F. Hawley, compiled in The Journal of the American Chemical Society Volume XXIX, accepted in Jan. 11, 1907.

The reconstruction was done by first obtaining a ratio of the number pixels to the physical variables in the plot - cc KMnO4 for the x-axis and grams Tl per one cc KMnO4 (Thallium factor) for the y-axis. The pixel coordinate corresponding to the grid lines were taken. The increments of the grid lines for x-axis is 5 cc, while 0.00001 Thallium factor for the y-axis. The ratio was obtained by taking the average of the difference between pixel coordinates of successive grid lines for the corresponding axis. The obtained value for the x and y axes will be used as a factor for obtaining a relation of the pixel coordinates to the physical variables on the plot.
This method assumes that the x and y axes aligns with the horizontal and vertical, i.e., the y(x) value of the pixel coordinates along the x-axis (y-axis) does not change. If this was not attained upon scanning of the plot, the image can be rotated so that the mentioned criterion is satisfied. This can be done using image rotating softwares, such as one function in MATLAB (imrotate('image file', 'angle in degrees')).
After obtaining the ratios, points were sampled from the plot and the corresponding pixel coordinates were noted. An increment of 10 pixels along the x was done for sampling points on the curve to ensure that the shape obtained would be accurate. The pixel coordinate of the origin of the plot was also recorded.
An adjustment should be made by having an offset based on the pixel coordinate of the origin. The x-coordinate should be added to the x pixel coordinates while the y-coordinate should be subtracted since the y-pixel coordinates increases downward along an image. From the obtained parameters, the physical values on the plot were obtained by multiplying the increment of the grid lines of the axis to the corresponding pixel coordinate and then dividing this by the ratio for the axis. Another adjustment was also made since the origin of plot did not start at (0, 0). The initial values were simply added to the values obtained after applying the ratio.
The obtained physical values were plotted in Excel, adding major grid lines and adjusting the parameters of the plot (min and max values of the axis). The original plot was filled in as a background of the plot to have a better visual evaluation of the resulting reconstruction. The plot was simply adjusted to fit the features of the plot on the image.
From the image posted, the reconstruction fits the original plot accurately, and for that, I feel that I can evaluate my grade for this activity as 10. There still are limitations in the reconstruction, mainly due to inconsistent judgment upon sampling points concerning the exact pixel coordinates that would be taken because of the thick lines (or points), and also the imprecise pixel differences that correspond to the physical variable increments. Averaging can limit, but not eliminate, this error.
I would like to acknowledge Winsome Chloe M. Rara, for her help in doing this activity, Mark Jayson Villangca and Jica Monsanto for their insights in the blog layout, and also for the guidance of our professors, Dr. Maricor Soriano and Dr. Gay Jane Perez.

Monday, June 15, 2009

Unang Crcle

Ito ang aking unang blog...
Ito ay para sa klase namin sa Applied Physics 186...
Kumuha kami ng mga lumang hand-drawn plots at nagpaphotocopy...
Kaya lang, kelangan namin ng digital copies nito kaya kelangan pang ipa-scan ng aming lecturer na si Ma'am Gay...
Ito ay aming irereconstruct gamit ang paint at excel (pwede din ang spreadsheet)...
Makikita namin ang kaibahan sa nito sa orihinal...