Thursday, August 6, 2009

ACTIVITY 12 - Color Image Segmentation

In this activity, we are going to pick out a region of interest from the rest of the image through image segmentation.

A digital image of 3D objects, shown below, was utilized in this activity. Different brightly colored candies are shown in the image.
We cropped a monochromatic region of interest in the scene. In this case, we wanted the blue candies to be our region of interest.
We notice that these objects found in the image, as well as the cropped image have shading variations which is inherent in 3D objects. To separate brightness and chromaticity (pure color) information, it is better to represent the color space by the normalized chromaticity coordinates or NCC. The normalized chromaticity coordinates can be obtained by dividing each pixel from each channel by the summation of the pixel corresponding on each channel.
This was implemented by the code below.

image =imread('candy.jpg');
patch = imread('patch.jpg');

r = image(:,:,1);
g = image(:,:,2);
b = image(:,:,3);
rp = patch(:,:,1);
gp = patch(:,:,2);
bp = patch(:,:,3);
R = r./(r+g+b);
G = g./(r+g+b);
B = b./(r+g+b);
Rp = rp./(rp+gp+bp);
Gp = gp./(rp+gp+bp);
Bp = bp./(rp+gp+bp);


Parametric Segmentation

The Gaussian PDF in the r values was derived using the equation

where sigma is the standard deviation and mu is the average of the normalized chromaticity coordinate in red of the patch image. The same equation was used for g values.

Rmu = mean(Rp);
Gmu = mean(Gp);
Rsigma = stdev(Rp);
Gsigma = stdev(Gp);


pr = (1/(Rsigma*sqrt(2*%pi)))*exp(-((r-Rmu).^2)/(2*(Rsigma)^2));
pg = (1/(Gsigma*sqrt(2*%pi)))*exp(-((g-Gmu).^2)/(2*(Gsigma)^2));

Then, the joint probability was taken as the product of rho(r) and rho(g).

product = round(pr.*pg);

The resulting image was shown below.


Non-Parametric Segmentation

The 2D histogram of the ROI was obtained using the give code below.

BINS = 256;
rint = round( Rp*(BINS-1) + 1);
gint = round (Gp*(BINS-1) + 1);
colors = gint(:) + (rint(:)-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;
scf(1);
mesh(hist);











To test the correctness of the histogram, we compare the location of the peaks with the rg chromaticity diagram (at the right). Clearly, we can see that the histogram of the ROI peaks at the are corresponds to the blue color in the rg chromaticity diagram. This histogram willl be important in segmenting the image using histogram bakcprojection.

This histogram backprojection is similar to what has done in Activity 4 excepth that in this case, the histogram is in 2D. Backprojection was done by replacing the value of the pixel location by its histogram value in chromaticity space. This was implemented by the code below.

rib = R*255;
gib = G*255;

[a, b] = size(image);
np = zeros(a, b);

for i = 1:a
for j = 1:b
c = round(rib(i, j)) + 1;
d = round(gib(i, j)) + 1;
np(i, j) = hist(c, d);
end
end
scf(2);
imshow(np);

The resulting image was shown below.


By implementing two different segmentation techniques, different parts of the region of interest was segmented. Parametric segmentation enables us to segment the outer part of the blue candies while non-parametric segmentation enables us to segment the inner body of the blue candies. The non-parametric segmentation favors in segmenting the region of interest although not all ROI were segmented.

I give myself a grade of 10 for finishing this activity. I was able to implement the parametric and non-parametric segmentation on an image composed of an ensemble of colored objects.

I would not have finished my blogs without the overwhelming help of Gilbert and Rommel. Gilbert was the one who patiently discussed every activity with me. He help me a lot in understanding previous activities. Rommel, after finishing his blog was able to lend me his laptop. It was a big help considering that I did not work on my blog in a computer shop for almost 4 hours.

:-) I acknowledged all my classmates who made me feel their concerns for me. I want to extend my deepest gratitude for all of you guys.

No comments:

Post a Comment