Thursday, August 6, 2009

Activity 12: Color Image Segmentation

For this activity, we are to select a region in the image with a particular color. First we have to transform the color space into a normalized chromaticity coordinates. To do this the following transformations are used:
Since 1=r+g+b, b=1-r-g. Therefore we can transform the color space (3-D) into two coordinate space (2-D), with the brightness associated with I. The image and patch used is shown below:
(Image taken from http://www.magicbob2000.com/resources/Cups%20&%20Balls%20Blue.jpg)

Patch


Two methods were used in this activity - parametric and nonparametric. For parametric a Gaussian Probability Distribution is used. The equation of which is given by
for the red values. The same is true for the green values (p(g)). The probability that a given pixel is inside the region of interest is dictated by prob=p(r)*p(g). For the nonparametric method, histogram backprojection is used. The pixel value in the histogram is used to backproject the value for a particular pixel. The histogram , result of parametric and nonparametric estimation of the image used is shown below:
Histogram
Parametric
Nonparametric

Although both estimations would suffice, a better segmentation is achieved with parametric estimation. It is also interesting that even the red "thingy" is well seen for parametric estimation. For the image used, the objects were separated from the background and from each other. Note however that we are only interested in segmenting the blue cups. Strictly speaking we did not segment the red "thingy". It just so happened that the background is violet (both with red and blue) which are the colors of the object that is why a gray color can be observed. Recall that when segmenting, what happens is that the region of interest becomes white. Any other color has different value.

Code:
stacksize(4e7);
chdir("C:\Documents and Settings\mimie\Desktop\186-12"); patch = imread("patch.jpg"); image = imread("cups.jpg"); ave = patch(:,:,1)+patch(:,:,2)+patch(:,:,3)+1e-7;
R = patch(:,:,1)./ave;

G = patch(:,:,2)./ave;
B = patch(:,:,3)./ave;


r = R*255;

g = G*255;


ave2 = image(:,:,1)+image(:,:,2)+image(:,:,3)+1e-7;

r2 = image(:,:,1)./ave2;

g2 = image(:,:,2)./ave2;
b2 = image(:,:,3)./ave2;
f = zeros(256,256);

for i=1:size(r,1)

for j=1:size(r,2)
x = abs(round(r(i,j)))+1;
y = abs(round(g(i,j)))+1;
f(x,y) = f(x,y)+1;
end

end
//imshow((frequency+0.0000000001));

//mesh(frequency);
//xset("colormap",jetcolormap(256));

\\\\parametric

rmean = mean(R);

rdev = stdev(R);
gmean = mean(G);
gdev = stdev(G);

rprob = (1/(rdev*sqrt(2*%pi)))*exp(-((r2-rmean).^2)/2*rdev);
gprob = (1/(gdev*sqrt(2*%pi)))*exp(-((g2-gmean).^2)/2*gdev);

prob = rprob.*gprob;
prob = prob/max(prob);
scf(0); imshow(image);
scf(1); imshow(prob,[]);

\\\\nonparametric
R2 = r2*255;

G2 = g2*255;

s = zeros(size(image,1),size(image,2));

for i = 1:size(R2,1)

for j = 1:size(R2,2)
x = abs(round(R2(i,j)))+1;
y = round(G2(i,j))+1;

s(i,j) = f(x,y);
end

end
scf(1); imshow(log(s+0.000000000001),[]);

scf(2); imshow(image);

Note:
Comment the parametric to use nonparametric


For this activity, I give myself an 8/10 since I'm not sure if I fully understand the activity.

No comments:

Post a Comment