Tuesday, July 7, 2009

Activity 5 | Fourier Transform Model of Image Formation

The Fourier Transform is one of the basic transforms used in imaging and is implemented in a fast and efficient way by the Fast Fourier Transform or FFT which is also mainly used in signal processing. The FFT is available in many signal and image processing software like the one used in this activity which is Scilab. The fft2() function in Scilab performs the Fast Fourier Transform for any two-dimensional signal or for this activity, images. Analyzing and processing images is explored using the the Fast Fourier Transform, convolution used in edge detection and correlation used in template matching.

5A - Familiarization with Discrete FFT

In the first part of the activity we get to familiarize the built-in function fft2() by applying it to a 128x128 image of a white circle on black background and the letter A that is constructed using Paint. Then we apply fftshift() to get the Fourier Transform of the image. The Fourier Transforms obtained agree with the analytical Fourier Transform of a circle which is an airy disk that becomes a airy point when the circle is large while for the letter A, it is like the Fourier Transform of a triangle that has three lines crossing each other at the center.

If we take the FFT of the resulting FFT of the images we get the intensity image or the original image that is inverted as shown by the last row of images. What is expected after getting the Fourier transform of the Fourier transform of the original image is the original image itself but inverted since an fftshift() is applied after the first FFT.



Original
image
FFT of
image
Shifted
FFT image
FFT of
FFT of
image


5B - Simulation of an Imaging Device

In this activity we simulate an imaging device - the lens - by using FFT and convolution where a function is operated on another function to get a new function. The object is a 128x128 image of the letters VIP in Arial which will serve as the first function and the aperture of the lens is a 128x128 image of a circle and its transfer function will be the second function. The convolution of these two functions will give the image produced by this system. The Fourier Transform is the lens in this system such that by doing a convolution between the Fourier Transforms of the object and the aperture you get the produced image of the system. Below are the images used and produced in this activity.


Different sizes of aperture are used as shown by the different sizes of circle images. It can be seen that as the aperture size becomes larger, the produced image becomes clearer and more defined. This agrees with the fact that small, finite apertures can't get or capture all light coming from the object thus a blurring is observed in the resulting image.

5C - Template Matching Using Correlation

Another method used in signal and image processing is correlation that is similar to convolution such that it is also related to the complex conjugation of the Fourier Transforms of the desired signals. Correlation measures the degree of similarity between two signals thus making it useful for checking patterns like for this activity where an image containing a phrase is correlated with another image containing the letter A. This letter A will be the template or the pattern.


The resulting image after the correlation shows the highest values, with black=0 and white=1, are at areas where the letter A can be found.

5D - Edge Detection Using the Convolution Integral

Edge detection is also like template matching only a pattern is used and that pattern is convolved with the VIP image using the built-in function imcorrcoef() in Scilab SIP Toolbox. Below are the resulting images using different patterns such as vertical, horizontal, diagonal, back diagonal, and spot pattern.

The images at the right side show the areas that are highly similar to the respective pattern. Say for example for the vertical and horizontal patterns, in the resulting image, the most pronounced areas are the edges that are vertical and horizontal respectively. The horizontal and vertical lines especially in the letters I and P are gone respectively for the vertical and horizontal pattern. Lastly, among the five patterns used, the spot pattern is the one that gave the best edge detection since it does not suggest any specific direction and it is like tracing the edge pixels of the letters V, I, and P.

I'd like to give myself a grade of 10 for this activity because it was fairly easy since the codes are already given but I give myself a grade of 9 for this activity because I did not finish the blog on time. Also I had a hard time exporting quality images from my results since I did not know anything about the command imwrite() but I thank Mr. Miguel Sison for mentioning it so I was able to apply it to the last part of the activity. It is a lot easier and the image quality is better.

Reference - UPNIP - AP186 - Activity Sheet 5 - Fourier Transform of Image Model.

Monday, July 6, 2009

Activity 4 | Histogram Backprojection

Images are comprised of pixels with values that represent a certain color. Getting the histogram of these pixel values give us information on the spread of the image's color from say black '0' to white '1' like grayscale images. In the previous activity, the histogram of a grayscale image was used to enhance the image by thresholding such that the desired region is well separated from the background.

In this activity, image enhancement is once again performed by tweaking the cumulative distribution function or the normalized histogram of a grayscale image into functions like the linear, parabolic, and logarithmic functions. Let us examine the PDF and CDF of a sample grayscale image as shown in Figure 1.



Figure 1. Original image with PDF and CDF respectively.


Performing histogram backprojection on the image using a linear CDF shown in Figure 2, we get a new image, its PDF and CDF as shown in Figure 3. From the image, PDF and CDF of the new image we can see the presence of higher values making the image brighter and more even.


Figure 2. Linear CDF.




Figure 3. New image with PDF and linear CDF.


Performing histogram backprojection on the image using a parabolic CDF shown in Figure 4, we get another new image, its PDF and CDF as shown in Figure 5. Because of the shape of the parabolic CDF, the majority of the pixels have high values thus producing an image with the very bright areas that are not needed as seen in the new x-ray image making it saturated.


Figure 4. Parabolic CDF.




Figure 5. New image with PDF and parabolic CDF.


Now the human eye has a nonlinear response, specifically a logarithmic response so using a logarithmic CDF shown in Figure 6, we get a new image, its PDF and CDF as shown in Figure 7. Because of the logarithmic trend, the resulting image has high contrast than the linear CDF. The logarithmic response is repeated on another image as shown in Figure 8.


Figure 6. Logarithmic CDF.



Figure 7. Using a logarithmic CDF.





Figure 8. Simulation of human eye response using a logarithmic CDF with original image (left) and new image (right) using logarithmic CDF.


I give myself a grade of 9 for this activity since I was able to do the histogram back projection using different CDFs and was able to produce the PDFs and CDFs of the new images correctly.

Sunday, July 5, 2009

Activity 3 | Image Types and Basic Image Enhancement

Digitized images have four types. We can obtain sample images for each type by searching over the internet. The following four images obtained from the internet (Figure 1-4) show an example for each image type together with their properties obtained using the SIP Toolbox function imfinfo().

BINARY IMAGE

File Source : http://www.fmwconcepts.com/imagemagick/morphology/images/logo2_fuzz15_mask_binary_open_1.gif
Format: GIF | Size : 951 | Width: 240 | Height: 240
Depth: 8 //bits per pixel
Storage Type: Indexed
Number of Colors: 2
Resolution Unit: centimeter
X&Y Resolution: 0

GRAYSCALE IMAGE

File Source : http://mangs.multiply.com/photos/album/13/hellsing_volume_1_chapter_3#3
Format: JPEG | Size : 52017 | Width: 352 | Height: 499
Depth: 8 //bits per pixel
Storage Type: Indexed
Number of Colors: 256
Resolution Unit: inch
X&Y Resolution: 0

TRUECOLOR IMAGE

File Source : http://www.dcercado.tk
Format: JPEG | Size : 38668 | Width: 500 | Height: 350
Depth: 8 //bits per pixel
Storage Type: Truecolor
Number of Colors: 0
Resolution Unit: inch
X&Y Resolution: 0

INDEXED IMAGE

File Source : http://mangs.multiply.com/photos/album/37/album_covers#21
Format: GIF | Size : 7550 | Width: 100 | Height: 100
Depth: 8 //bits per pixel
Storage Type: Indexed
Number of Colors: 256
Resolution Unit: centimeter
X&Y Resolution: 0


Now knowing the four basic image types let us do some basic image enhancement of a grayscale image by thresholding to separate the background from the Region Of Interest or ROI. To do this we need to know the grayscale distribution which can help in knowing the gray values of the ROI and obtain it by thresholding.

Figures 4 and 5 shows the scanned images to be used for basic image enhancement and below them are their image properties again using imfinfo(). Similar properties of both images are, Format: BMP, Depth: 8bits/pixel, Storage Type: Indexed, No. of Colors: 256, Resolution Unit: centimeter, and X&Y Resolution: - 1.718D+09.


Figure 4. HEART | Size : 82174 | Width: 370 | Height: 218


Figure 5. BATMAN | Size : 78610 | Width: 363 | Height: 213


From the grayscale images we can get the histogram of the gray values by using the Scilab Statistics function tabul() which returns a matrix of the pixel values and the corresponding frequency of each value in an image. To get the Probability Density Function of PDF of the image we normalize the histogram by the total number of pixels in the image. Figures 6 and 7 shows the corresponding PDFs for HEART and BATMAN.


Figure 6. HEART PDF


Figure 7. BATMAN PDF


The PDFs shows clearly the pixel values of the background and the ROI. Based from both histograms we can decide what threshold value to use to get the ROI and compute its area just like what was done previously in Activity 2. The threshold value used for both images is 0.5 but the threshold value for both images can be anything between the pixel values between the two distinct peaks that has a frequency value of zero. Anything in this range separates the pixel values for the background and the ROI. Figures 8 and 9 show the corresponding black and white images for HEART and BATMAN.


Figure 8. HEART with threshold at 0.5


Figure 9. BATMAN with threshold at 0.5


Now let us apply what we have learned from Activity 2 and compute the area of the ROI for each of our images. Figure 10 shows the obtained contour of the ROI. The computed area of the ROI using Green's Theorem in pixels for HEART is 8418px while for BATMAN is 12552.5px. But summing up the number of pixels with values equal to 1 the total area in pixels for HEART is 8598px and for BATMAN, 12885px. The physical area can be computed since the images were taken along with a ruler beside them. By computing the physical area of one pixel, we could estimate the physical area of the ROI. Using GIMP we observed that 1mm covers 3px and thus we can estimate the physical area of the ROI. A summary on the area measurements is provided below the ROI contours.


Figure 10. HEART and BATMAN contours.


HEART | Green's: 8418px > 935.33mm2 | PxCount: 8598px > 955.33mm2
BATMAN | Green's: 12552.5px > 1394.72mm2 | PxCount: 12885px > 1431.66mm2

From the obtained area measurements there is a great difference observed. Possible sources of error are the contour used as mentioned in Activity 2, the thresholding and the estimation of the area of 1 pixel. Still I give myself a 9 or a 10 for this activity since I have fully understood the concepts used and was able to get estimated area measurements of scanned images of irregularly shaped ROIs.

I thank Jay Samuel Combinido, Jica Monsanto and Miguel Sison for providing the scanned images that I used in this activity. I also thank Jica Monsanto for referring me to the function tabul() in getting the histogram of the gray values for the scanned images.

Wednesday, July 1, 2009

Activity 2 | Area Estimation for Images with Defined Edges

Computing the areas of images is a very important tool in different technologies nowadays. Here in this activity the area of a binary image is computed by using Green's Theorem on computing the area of an image by using the contour of the image. From the activity manual itself, using Green's Theorem is getting the 'Area from edge.'

Green's Theorem [1] relates a double integral to a line integral as shown by the following equation where F1 and F2 are continuous functions with continuous partial derivatives that encloses the desired region or image.

This equation is then reduced into a simple summation that uses the contour of the desired region or image and is given by the next equation.

Below is generated and used code in this activity using Scilab. The procedure on what and how it is done is also shown here.

1. Load image.
rect = imread('C:\Users\Cindy\Documents\physics\ap186\A2\rectangle.jpg');
//circle = imread('C:\Users\Cindy\Documents\physics\ap186\A2\circle.jpg');

2. Convert Image from RGB to binary format using the SIP Toolbox function im2bw. Two binary images are used in this activity where one is an image of a rectangle and another is the image of a circle. Both images are created using Paint. Their analytical area is computed from the pixel dimensions given by Paint.
bw = im2bw(rect, 0.5);

3. Get the contour of the image using the SIP Toolbox function follow
[x,y] = follow(bw);

4. Implement GREEN'S THEOREM using the contour obtained.
len = length(x); //length of contour array
Aa=51012; //analytical area for rectangle
//Aa=53820; //analytical area for circle
Ac=0; //computed area

for i=1:len-1
a = (x(i)*y(i+1)) - (x(i+1)*y(i));
Ac = Ac+a;
end

5. But since the generated contour is not closed as shown in #3, we add the first value of the contour to close it to a loop.
a = (x(len)*y(1)) - (x(1)*y(len));
Ac = (Ac+a)/2

6. Check if the contour fits the original image by superimposing the contour on the original image.
imshow(rect)
plot(x,y)

7. COMPUTE PERCENT ERROR
perr = (abs(Aa - Ac)/Aa)*100

The results for each image is shown below. The analytical and computed area using Green's Theorem is given below each image.

Image 1: Rectangle
Analytical Area: 51012 px
Computed Area : 49220 px
Percent Error: 3.51%

Image 2: Circle
Analytical Area: 47143.52 px
Computed Area : 45575 px
Percent Error: 3.33%

Based on the results obtained, we can say that the method used gave a good approximation in computing the area of a region in an image. One possible source of error is the contour used which was generated by a built-in function of the SIP Toolbox. As seen from the superimposed images, the contour does not exactly fit the outline of the polygon leaving out spaces that should be included in the area.

I give myself a grade of 9 for this activity since the computed area is a close approximation of the analytically obtained area of the image as shown also by the small percent error. It is obvious enough that a major source of error is the contour used in the computation.

I thank Ms. Kaye Vergel for helping me on the basic functions of the SIP Toolbox such as the help function, imread, follow, im2bw. :D :D :D Thank you also for pointing me to the SIP Functions Reference.

Reference:
[1] UPNIP - AP186 - Activity Sheet 2 - Area Estimation for Images with Defined Edges.