Tuesday, June 30, 2009

Activity 4: Enhancement by Histogram Manipulation

In this activity, we were tasked to get a poor-contrast grayscale image from the web. Using the histogram manipulation technique, the goal is to make a high-contrast image out of the original image.
In histogram manipulation, the first step is to take a histogram of the pixel values in the image (histogram is plotted with pixel values on the x-axis and frequency of occurrence of the pixel value on the y-axis. The histogram gives an idea of how many of a pixel value is contained in the image. Once the histogram is made, a cumulative distribution function (CDF) can be plotted. CDF is just the cumulative sum of the values on the y-axis plotted with pixel values. (It is better to work with CDF than histogram as we would see later.)

To start with, below is the graysacle image taken from the web.

Figure 1. Grayscale image taken from http://homepages.inf.ed.ac.uk/rbf/HIPR2/images/wom1.gif. The figure is then converted to a jpeg file.

Using Scilab, the histogram, as well as the CDF as shown below, of the image in Fig.1 was obtained.


Figure 2. (above) Histogram and (below) CDF of the image in Fig. 1.

As observed from the plots in Fig.2, the CDF is cleaner to see than the histogram. Having the advantage, CDF is easier to manipulate than the histogram.
In histogram manipulation, a desired CDF is needed. This will be used to alter the pixel values contained in the original image. The method is illustrated below.

Figure 3. Data manipulation procedure

The desired CDF is of a uniform distribution.

Data manipulation procedure done in the following:
(Step 1) Take a pixel value from the CDF of the image in Fig.1
(Step 2) Obtain the value on the y-axis corresponding to the pixel value
(Step 3) Project the y-axis value onto the desired CDF
(Step 4) Take the pixel value from the desired CDF corresponding to the y-axis value.
The pixel value in the image is then replaced by the pixel value from the desired CDF.

The code implementation for a linear desired CDF is written below:

h1 = zeros(256,256);
for i=0:255
new = m(i+1)*255;
h1 = h1 + (im == i)*new;


Performing this procedure for all pixel values (0-255), a reconstructed image shown below was obtained.


Figure 4. Reconstructed image

As seen, the image highly contrasted compared to the previous image as a result of the procedure. We also characterize the image by taking its pixel histogram and CDF.



Figure 5. (above) Histogram and (below) CDF of the reconstructed image.

The histogram of the reconstructed image is different from the original image. It is also interesting to note that the spread of the histogram of the reconstructed image is larger than that of the original image indicative of the high contrast of the reconstructed image. Moreover, looking at the CDF of the reconstructed image, it resembles a straight line closer to the desired CDF.
To further explore the technique, a desired CDF that mimics the nonlinear response of the human eye was used. Below is the desired CDF used in reconstructing the image.

Figure 6. Exponential CDF used in reconstructing the image

The same procedure was done as before and a reconstructed image shown below was obtained.

The code implementation for a nonlinear desired CDF is written below:

h1 = zeros(256,256);
for i=0:255
new = int(50*log(m(i+1)*(exp(255/50)-1)+1));
h1 = h1 + (im == i)*new;



Figure 7. Reconstructed image using a nonlinear desired CDF

Compared to the previous reconstruction, the image in Fig.1 is brighter (many of the pixel have high values). we can readily attribute this to the desired CDF used. The latter favors high-valued pixels. As a result, a bright reconstructed image was obtained.


Figure 8. (above) Histogram and (below) CDF of the reconstructed image using nonlinear desired CDF

It can also be seen from Fig. 8 that the histogram of the constructed image has high amplitudes on high-value pixels which indicates a bright image.
Also the CDF of the reconstructed image followed the desired CDF.

In this work, i will give myself a grade of 10 for doing it right. Good job for myself.








Tuesday, June 23, 2009

Activity 2: Area Estimation for Images with Defined Edges

In this activity, we were tasked to draw shapes on MSPaint. The images are binary. The image themselves have pixel value of '1'. The background having '0'. The images drawn are shown below.


Figure 1. Circle (Area = )




Figure 2. Rectangle (Area = 252x140 sq.px)



Figure 3. Triangle


Figure 4. Irregular shapes

The main goal of the task is to take the area of the images comparing the measured area by just counting the 1's and by using Green's theorem given below.

The summation is over the contour of the image.
The results are shown below.

Table 1. Area calculations using pixel counting and Green's formula


Here, we can see that the Green's formula is accurately able to determine the actual area of the figure. Even when the shape is irregular as in Fig 4. as long as the contour is well-defined, the Green's theorem can correctly calculate the area enclosed by the contour.

In this activity, i will give myself a grade of 10 because i was able to correctly determine the area using Green's theorem.



Monday, June 22, 2009

Activity 3: Basic Types and Basic image enhancements

In first part of this activity, we are tasked to find different modes of images,


Figure 1. True color image

Dimensions: 504 x 390
Width: 504
Height: 390
Horizontal Resolution: 72 dpi
Vertical Resolution: 72 dpi
Bit Depth: 24



Figure 2. Grayscale image

Dimensions: 600 x 400

Width: 600
Height: 400
Horizontal Resolution: 230 dpi
Vertical Resolution: 230 dpi
Bit Depth: 8
Resolution Unit: 2


Figure 3. Indexed image

Dimensions: 600 x 410

Width: 600
Height: 410
Bit Depth: 8






Figure 4. Binary image

Dimensions: 600 x 400

Width: 600
Height: 400
Bit Depth: 1









The images were taken from the following sources:

1(left): http://www.thesunblog.com/gourmetgal/fruit.jpg
1(right): http://www.alexmorais.com/Blog%20images/cat-grayscale.jpg
2(left):http://www.gingerbreadartgallery.com/images/archi/large/Gingerbread-house.gif
2(right): http://www.codeproject.com/KB/GDI-plus/AnnotatedImageV2/Mask.png

The binary image was really hard to find.



For the second part of the activity, a scanned image was used. Here we are tasked to apply thresholding to the image to:
(1) enhance the contrast of the image and
(2) to remove unnecessary pixel values on the region of interest (ROI).

In the figure below, the ROI is the name "JAYA".


Figure 5. (left) Scanned image and (right)pixel histogram of the image

Scanned images are usually in RGB mode. In this figure, the mode of the image was changed to grayscale using GIMP. The histogram in Fig.5(left) shows the pixel value distribution on the image (from 0 -255 pixel values). Here we can see the distinct separation between the high value pixels (white) and the low value pixels (black). As such, our ROI is sufficiently separated from the background.

One can also use Scilab to plot the histogram of the image using this code:

g = imread('(path of the image)');
g = int(255*im2gray(g));
[x,y] = size(g);
b = 0:255;
a =[];
for i = 0:255,
a(i+1) = sum((g == i)*1);
end;


plot(b,a/(x*y));

Using this we obtain:

Figure 6. Pixel histogram of image in Fig.5(left) using Scilab


Now, we apply GIMP to threshold the image. In thresholding, we set a certain value (threshold) such that any value less than the threshold will be discarded (pixel value is set to zero when dealing with images) and anything above will be retained or set to a certain value depending on the purpose.


Figure 7. (left) Raw image and (right)the thresholded image

Using a threshold, we are now able to completely separate the ROI from the background.
We can area calculations learned in Activity 2 to Fig.7(right).



We have calculated the area of the ROI in pixels using Green's theorem and pixel counting. Below is the same image with an attached ruler alongside with it. Using this ruler, we can calibrate the pixel-mm ratio which allows us to determine the actual area of the ROI.


Figure 8. Original image with attached ruler

Using MS Paint, the pixel-mm ratio was calculated.

pixel-cm ratio = 313 pixels:(262 mm - 156mm)
= 1 pixel : 0.339 mm


Hence, the length of a pixel is equal to 0.35 cm. The area of a pixel is then:

A = (0.35 mm)2 = 0.115 mm2

Using the pixel counts earlier, we obtain these actual areas.



In this activity, I will give myself a grade of 8 for finishing this blog late.

Wednesday, June 17, 2009

Activity 1 - Digital Scanning
















Figure 1. Graph taken from Introduction to Physiological Chemistry by Meyer Bodansky, 1930.

In this activity, from the graph (Fig. 1) we found last meeting, we are to replicate it using Paint and Spreadsheet. From the pixel locations (x,y) of the points on the plot, we record it to the spreadsheet and perform scaling operations to the values to replicate the physical values found in the graph.

To scale the axis:
1. Take the x-pixel locations (x- if scaling the x axis, y- if scaling the y-axis) of the smallest value and largest value on the respective axis of the graph (i.e. 4.7 and 12.2).
2. Solve for the the average number of pixels in between grids:
pbg = (pixel location of max value on the axis - pixel location of min value on the axis)/[(max value -min value)/value increments]

For scaling on the x- axis:
physical value = min x value + x-pixel location* 0.5/pbg

For scaling on the y-axis:
physical value = max y value - y-pixel value*1/pbg
(Scaling on the y- axis is different from the x-axis since the origin (0,0) of the image is different from the origin of the Cartesian coordinate system.)

The numerical values on the above equations accounts for the physical value spacing between grids. Also note that pbg on x and y axis are not necessarily the same. The graph below is the superimpose plot of the original figure and the digitally scanned figure.

Figure 2. Superimposed plot of the original figure and the digitally scanned figure. It can be observed that the scanned figure fits the original one.

Th colored dots represent the digitally scanned points from the original graph. We can see that the points coincide with original one. I give myself 10 for doing a good job!

Monday, June 15, 2009

The search for the oldest plot...

Today, we were tasked to find the oldest plot we can get. Preferably a hand written one. I got a year-1930 plot. I found it in a chemistry book entitled Introduction to Physiological Chemistry by Meyer Bodansky PhD.