A14: Pattern Recognition

In this activity, we're to classify different objects based on features that may be extracted from images.

Features are first extracted from a training set (Figure 1) composed of images representative of each class or object type. In our example, in distinguishing between dry roasted sea salt and chocolate-coated macadamia nuts, the roughness of the surface and the color of the objects were determined.


Figure 1. Left: dry roasted sea salt macadamia. Right: Maltesers (eep we ran out of chocolate-coated macadamia nuts but they look the same)

To quantify surface roughness, the contour of each object was determined using Scilab's follow command. From the contour we determine the position of the object's centroid (x0,y0). We then determine the distance of each point in the contour from the centroid and take average of their variance as a measure of roughness. This is implemented in the following function, which takes in the raw image of the object:
function [r]=checkrough(image),
imagebw = im2bw(max(image)-image,0.2); //binarize
[imageconx,imagecony] = follow(imagebw); //determine contour points
sizecon = size(imageconx); //no. of contour points
//determine centroid: get major and minor "radii" by taking half of the distance between the maximum and minimum values (for x and y separately). It is assumed (and is true in my data set) that the objects are elliptical. The centroid coordinates then are the radii shifted by the minimum x or y value.
imagectrx(1:sizecon(1)) = min(imageconx)+(max(imageconx)-min(imageconx))/2;
imagectry(1:sizecon(1)) = min(imagecony)+(max(imagecony)-min(imagecony))/2;
//basic euclidean distance of each contour point from centroid
imagesurfpxs = sqrt((imagectrx-imageconx).*(imagectrx-imageconx) + (imagectry-imagecony).*(imagectry-imagecony));
r = mean(variance(imagesurfpxs)); //avarage of variances
endfunction;

For quantifying color
function [c]=checkcolor(image),
imagebw = im2bw(max(image)-image,0.2); //binarize
[imagecontourx,imagecontoury] = follow(imagebw); //determine contour points
ctr = 1;
for j = [min(imagecontourx):max(imagecontourx)], //only determine color for ROI
for i = [min(imagecontoury):max(imagecontoury)],
if imagebw(i,j)=1,
cs(ctr) = image(i,j,1)+image(i,j,2)+image(i,j,3); //compute for grayscale (pwede din convert to grayscale nalang the image before pa)
ctr = ctr + 1;
end;
end;
end;
c = mean(cs); //mean color within boundary
endfunction;

Figure 2 shows the color vs. roughness plot for the training set. The classes appear to be fairly distinguishable both in color and roughness.


Figure 2.

Features are also extracted from images in the test set and the distance of their values from the mean values from the training set allows us to classify the object in the image. The object gets tagged as a member of a class with minimum distance from it. Figure 3 shows the color vs. roughness plot for the test set. The classes appear distinguishable in color but not in roughness. Three of the chocolate-coated kind are closer to the dry roasted sea salt cluster or two of the choco are too far from its cluster.


Figure 3.

For my set, the method classified 70% of the test objects correctly. I checked the wrongly classified images and found that the objects there were almost as smooth as the other class but were still significantly higher in roughness than all test and train sets. Applying thresholding, 100% correct classification is attained.

I give myself a grade of 9 because the method implemented scored better than a random classifier.

I would like to thank my mom, Mrs. Adoracion Monsanto, for letting me use her stash of macadamia nuts and for reimbursing the Maltesers I bought (and ate) for this activity. :)


A13: Correcting Geometric Distortions

In this activity, we're to attempt to remove distortion in an image. One common type of distortion is the barrel distortion where the image looks like the face of a blown up balloon because the center appears bloated and the sides appears compressed (Figure 1). Another distortion commonly encountered is the pincushion distortion, which is just the opposite of barrel distortion in the sense that its center appears compressed and the sides seem stretched (Figure 2).


Figure 1. Barrel distorted image. (source: http://www.photos-of-the-year.com/barrel-distortion/2.jpg)


Figure 2. Pincushion distorted image. (source: http://www.dpreview.com/learn/Articles/Glossary/Optical/images/VBO_PIN01.jpg)

Correcting these distortions will require an ideal image or an image that does not have pinched up or bloated regions that has points we can easily match up with points on the distorted image. The method also requires determining the correct pixel coordinates and tagging pixels with the correct graylevel values.

In this activity, we're to attempt to correct barrel distortion of an image. But first...


(http://farm3.static.flickr.com/2338/3541975650_4490991892.jpg)

Really pretty, eh? Pretty breath-taking. :) And in the photographer's description for this picture:
"Yes, there is barrel distortion... It's easy to get rid of in post processing."
Yeah. Real easy. No sweat. (!!!!!!)

Anyway, I'm not correcting that image. Let's try to correct a simple distorted grid first (Figure 1).


Figure 1.

First, to determine the ideal image, I get pixel coordinates of a box that looks flat and lies near the optical axis using Scilab's locate command. These pixel coordinates give me the estimated no. of pixels between the horizontal lines of the grid and between the vertical lines of the grid. I found both distances to be approximately 22 pixels; as expected, they're the same because the grid boxes were squares. Then we generate the ideal grid from this and note the vertex points (Figure 3).


Figure 3.

The next step should be to compute for c1 to c8 for each cell pair (ideal and distorted grid) BUT our distorted image is still in grid form. We need its vertices for computation. So as an application of past activities, I used imcorrcoef on the distorted grid and a patch from the grid with least noticeable distortion (Figure 4).


Figure 4.

Then we use skel and arrive with the following:


Figure 5.

It was made sure that the vertices at the centers of the ideal and distorted images approximately coincide:


Figure 6.

There are still missing vertices on the left, which I think may be recreated and there are extra vertices everywhere (the white spots are not single pixels), which I think may be eliminated through distance calculations. But because actual coding attempts at these have failed, I have decided to identify vertex locations with locate. I was bored (more like desperate). Moving on, I calculated c1 to c8 for each cell so we can determine the location of each pixel in the ideal form of the image. We arrive at the following set of pixels:

Figure 7.

Note that outer cells are more crooked than those near the center because the image is more distorted in those parts. The kink at center bottom is just due to a mistake in vertex selection.

Then we assign grayscale value or color of each pixel based on the still distorted image (since calculated pixel locations are non-integers, I rounded them off to be used as matrix indices). Figure 8 shows part of the less distorted image.

Figure 8.

It seems a bit less distorted. A better way of assigning graylevel values would be to. check values from neighboring pixels.

I give myself a grade of 8 because of efforts for this activity (in the initial and final attemts at extracting vertices especially) although an undistorted version of the image was not produced.

I would like to thank Mr. Luis Buno III and Ms. Kaye Vergel for the humongous help. Seriously, thank you guys. :D

A12: Color Image Segmentation

In this activity, we're to extract a region of interest (ROI) through image segmentation. This technique makes use of properties unique to the ROI in separating the ROI from the rest of the scene or image.

In grayscale images, simple thresholding may be done to separate lighter colored objects from a dark background, for example. However, when the ROI or the object's pixel values (we're usually interested in extracting an object from a scene) do not differ much from the background, segmentation will not be successful. In colored images, we have three values (RGB) to consider, hence, more unique features may be extracted from the ROI. So, although most image processing done on 2D grayscale matrices are easier than those for 3D matrices of colored images, the amount of information present in colored images is an advantage in image processing.

We demonstrate two segmentation procedures: parametric and non-parametric segmentation. Both techiniques involve three steps essentially:
  1. picking out a patch that defines the color histogram of the ROI;
  2. converting RGB values of each pixel (of the image and of the chosen patch) to normalized chromaticity coordinates (NCC), which are just the RGB values normalized with respect to the sum I=R+G+B: r=R/I, g=G/I, b=B/I; and
  3. tagging each pixel of the image as a member or a non-member of the ROI based on the patch.
The difference between the two methods lie in the third step. We note that, since r, g, and b are normalized, their values will be between 0 and 1 and, per pixel, r+g+b=1. Hence, we can just describe the chromaticity of pixels with just the first 2 coordinates, r and g, because we know that b is just 1-r-g. The code snippet below shows the Scilab implementation for calculating r, g, and b values for an image:

img = imread(ImageDirectory+Filename+'.jpg'); //import image
img = double(img); //for flexibility in matrix operations

imgR = img(:,:,1);

imgG = img(:,:,2);

imgB = img(:,:,3);

imgI = imgR + imgG + imgB;

imgI(find(imgI==0)) = 100000; //to avoid division by zero later

//normalized chromaticity coordinates

imgr = imgR./imgI;

imgg = imgG./imgI;

imgb = imgB./imgI;


We now demonstrate both methods by attempting to extract the each of the colored hearts individually from Figure 1. Figure 2 shows the different 5x5 pixel apparently monochromatic patches for extracting each heart.


Figure 1. Scene with three objects of interest with different colors.
(source: http://www.pepperspollywogs.com/news/feb09/visual_editor_preview_data/155.jpg)


Figure 2. "Monochromatic" patches for extracting (left to right) the red, green, and blue hearts individually.

Parametric Segmentation

Here we need to recalculate the probability distribution function (PDF) of the ROI based on the patch. We can assume a Gaussian distrubution:

where and are the mean and standard deviation of r values from the patch, respectively, and r values are from the image in Figure 1. The PDF for g is calculated the same way.

Figure 3 shows the results of extraction using the monochromatic patches in Figure 2. We note that only the red heart is recognizable. So, I tried using other patches (Figure 4), this time not necessarily monochromatic and I got significantly much better results for the blue heart (Figure 5, right). The green heart (Figure 5, left), however, did not improve much. I think it's because its surface was too shiny on the picture. We note that the reflection of the objects on the surface was also extracted.


Figure 3. Results of parametric segmentation using monochromatic patches in Figure 2 for the (left to right) red, green, and blue hearts.


Figure 4. Non-monochromatic patches for green and blue hearts.


Figure 5. Results of parametric segmentation using non-monochromatic patches in Figure 4 for the green and blue hearts.


Non-Parametric Segmentation

This involves getting the 2D histogram (r on the x-axis and g on the y-axis) of the patch. For ROI membership tagging, the image r and g values are matched with histogram x and y values (patch r and g) and the histogram value becomes the tag or the new pixel value of the image. We note that to be able to match pixel values, we bin r and g for both patch and image in Scilab:

BINS = 32;

patrint = round(patr*(BINS-1)+1);
patgint = round(patg*(BINS-1)+1);


To create the 2D histogram:

colors = patgint(:) + (patrint(:)-1)*BINS;

hist = zeros(BINS,BINS);

for row = 1:BINS,
for col = 1:(BINS-row+1),

hist(row,col) = length(find(colors==(((col+(row-1)*BINS)))));
end;
end;


Finally, to tag:

imgsize = size(img);
imgmod = zeros(imgsize(1),imgsize(2));

for row = 1:imgsize(1),
for col = 1:imgsize(2),

imgmod(row,col) = hist(imgrint(row,col),imggint(row,col));

end;
end;


Figure 6 shows the resulting images. The extracted red heart (Figure 6, leftmost) is similar to the one extracted through parametric segmentation (Figure 3, leftmost). The extracted green and blue hearts are better than their parametric counterparts. In an attempt again to improve the extracted images, we use the patches in Figure 4 and we get those in Figure 6. Images were indeed improved but not as improved as those from parametric segmentation.


Figure 6. Results of non-parametric segmentation using monochromatic patches in Figure 2 for the (left to right) red, green, and blue hearts.


Figure 7. Results of parametric segmentation using non-monochromatic patches in Figure 4 for the green and blue hearts.

We then can conclude that patch selection is important in segmentation. Monochromatic patches are good for extracting objects that are not shiny but non-monochromatic patches that accounts for most of the RGB values present in the object of interest. It has also been observed that non-parametric segmentation does better than parametric segmentation when the patches are monochromatic but the latter does better when the patches are non-monochromatic.

I give myself a grade of 9 because parametric and non-parametric segmentation was demonstrated.

I would like to thank Ms. Cherry May Palomero and Ms. Irene Crisologo for useful discussions.

A11: Color Image Processing

So far, we've only processed and enhanced grayscale and black and white images that are only represented by 2D nxm matrices. Colored images are represented by three of such matrices, one for each color channel (red, green, and blue) and hence are treated as 3D nxmx3 matrices and they require different enhancement techniques.

White balancing is done on images to fix image discoloration due to different lighting conditions. Most cameras give users the option to pick the white balancing constants to used on the images captured. Properly white-balanced images should have white appear white and other colors correctly captured. Choosing the wrong white balancing constants can cause image discoloration. There are white balancing techniques that choose constants automatically based on the illumination detected (from colors detected). Two popular techniques are the White Patch Algorithm (WP) and the Gray World Algorithm (GW).

In the White Patch Algorithm, we simply divide the RGB of the image with the RGB values of the image for white. In the Gray World algorithm, it is assumed that the average of the colors of the world is gray, hence we divide the RGB values of the image with its average RGB for each color channel.

In this activity, we are to attempt enhancement of colored images using WP and GW and demonstrate the advantages and disadvantages of each algorithm.

Figure 1 shows images of an ensemble of objects of different colors at different camera white balancing settings. This is to test rendering of different colors. Figure 2 shows images of an ensemble of objects of different hues. This is to test rendering of different hues.


Figure 1. Images of an ensemble of different colored objects taken with camera white balancing set to (left to right): cloudy, day, fluorescent, and bulb.


Figure 2. Images of an ensemble of objects of different hues of red taken with camera white balancing set to (left to right): cloudy, day, fluorescent, and bulb.

Figures 3 and 4 show a summaries of results of PW and GW. From Figure 3, we find that both are successful at lightening the scene and showing colors nearer the actual colors of the objects as seen by the eye. However, in the 'bulb' camera setting, both techniques leave white parts as green. We note that for 'cloudy', WP gives brighter colors while for 'fluorescent', GW gives brighter colors. WP and GW give similar results for 'day.'




Figure 3. WP (top) and GW (bottom) white balancing of images of different colored objects (middle) taken with camera WB set to (left to right): cloudy, day, and fluorescent.

From Figure 4, we find that only the color of Kaye's bag and Jaziel's blouse show obvious enhancement. Also for all camera settings except 'day', WP produces better results.




Figure 4. WP (top) and GW (bottom) white balancing of images of objects with different hues of red (middle) taken with camera WB set to (left to right): cloudy, day, and fluorescent.

I give myself a grade of 9 because the effects of using WP and GW were demonstrated and compared.

I would like to thank Ms. Kaye Vergel for taking the pictures with her camera and for lending some of her things for the two sets of pictures along with Ms. Winsome Chloe Rara and Ms. Cherry May Palomero. I would also like to thank Ms. Jaziel Vitug for patiently posing for us as we took pictures of her pink blouse. :)