Monday, September 1, 2008

Activity 16: Color Image Segmentation

The task: to segment the Region of Interest(ROI) of an image by their color.

Two methods are investigated. parametric and non-parametric (histogram backprojection) probability distribution.

First though, for both methods, the RGB values of the region of interest are obtained and converted into normalized chromaticity coordinates (NCC), using the following equations:
[ncc.jpg]
since b (blue) can now be expressed as a function of r(red) and g(green) now though, we can discard it and only consider the latter 2.

The original image:
we want to segment the heart-shaped royal blue swarovski pendant.

The histogram of the crystal is shown below in chromaticity space:

Looking at this representation of the RG(b) color space,
[rgcc.jpg]
we can say that the above histogram is a valid representation of the colors in our region of interest.

Now we compare the results after using both methods:
Parametric Probability Distribution
Histogram Back-Projection
For my image, contrary to what expected from I've learned from my classmates' results, both methods gave about the same quality of image. One can clearly see the heart shape and even some hints of the 3D shape of the pendant after the application of both methods.

Code:
stacksize(4e7);

im1 = imread('act16c.jpg');
im2 = imread('act16b.jpg');
//imshow(im1);

R1 = im1(:,:,1);
G1 = im1(:,:,2);
B1 = im1(:,:,3);
I1 = R1 + G1 + B1;
R2 = im2(:,:,1);
G2 = im2(:,:,2);
B2 = im2(:,:,3);
I2 = R2 + G2 + B2;

r1 = R1./I1;
g1 = G1./I1;
b1 = B1./I1;
r2 = R2./I2;
g2 = G2./I2;
b2 = B2./I2;

//parametric
ur = mean(r2); sr = stdev(r2);
ug = mean(g2); sg = stdev(g2);

pr = 1.0*exp(-((r1-ur).^2)/(2*sr^2))/(sr*sqrt(2*%pi));
pg = 1.0*exp(-((g1-ug).^2)/(2*sg^2))/(sg*sqrt(2*%pi));

prob = pr.*pg;
prob = prob/max(prob);
//imshow(prob,[]);

//histogram
r = linspace(0,1,32);
g = linspace(0,1, 32);
P = zeros(32,32);
[x,y] = size(im2);
for i = 1:x
for j = 1:y
xr = find(r <= im2(i,j,1));
xg = find(g <= im2(i,j,2));
P(xr(length(xr)), xg(length(xg))) = P(xr(length(xr)), xg(length(xg)))+1;
end
end
P = P/sum(P);
//surf(P);

//backprojection
[x,y] = size(im1);
recons = zeros(x,y);
for i = 1:x
for j = 1:y
xr = find(r <= im1(i,j,1));
xg = find(g <= im1(i,j,2));
recons(i,j) = P(xr(length(xr)), xg(length(xg)));
end
end
recons1 = im2bw(recons, 0.5);
//imshow(recons1, []);


**I give myself a grade of 10 for this activity, the region of interest was successfully segmented using either method.

**Thank you to Jeric Tugaff.

No comments: