Wednesday, August 5, 2009

A12 Color Image Segmentation

Color segmentation can be performed by determining the probability that a pixel belongs to a color distribution of interest.This is how we can do just that.

1.The histogram of the region of interest (ROI) must first be extracted.

2.Tag a pixel as belonging to the ROI or not is to find its probability of belonging to the color of the ROI

3. We used the sample code in the manual to show just how histogram backprojection is implemented to tag the pixel and get its histogram value in chromaticity space.




Figure 1.Normalized chromaticity
space. X-axis is r and y-axis is g.[manual]


Figure 2. Image 1-Christmas balls





Figure 3. The top 3 RGB patches are taken from the RGB regions in figure 2. The 2D histograms at the bottom illustrate that these ROI coincide with the small region in their individual R G B space in the NCC shown on the left with the variation corresponding to the pixel chromaticity.



As expected, images of the 3D objects such as bright colored balls have shading variations . Given that its shading variation due to brightness is small, for the red green and blue ROI pathces its pixel chromaticities for RGB will occupy a small portion in its region in NCC as in Figure 3.


The next step was to see which method was more effective in the tagging of the pixels RGB value with the first using use of the red and green joint PDF by parametric segmentation with a Gaussian Distribution function and the other using non parametric segmenatation which takes the 2D histogram of the ROI that is then used segment the image using histogram backprojection. For both technique the result should yield white or high peaks (red in hotcolormap) for matches in the range of red and green of the ROI to that image pixel's RGB.


The results and code are shown below:


Figure 4. The ROI was the red patch in the left middle of the red ball.


Figure 5. The ROI was a blue patch in the left middle of the blue ball.

Figure 6. The ROI was a green patch in the left middle of the green ball.


Figure 7. Plots of Gaussian Probability Distribution of the red, blue and green ROI

It can be noted that both the parametric method and non parametric method were indeed effective in highlighting the image pixels with the same RGB probability distribution as seen in the rendered image.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code:

1st part:
//////1. Capture a digital image of a 3D object with a single color (e.g. colored mugs,faces, hands or any exposed skin, brightly colored balls, etc.). You may also
//////use colored pictures you've captured in previous activities.
//
//


stacksize(4e7);
I=imread('red.jpg');



//
//R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
//Int= R + G + B;
//r=R./I;
//g=G./I;
//b=B./I;


I = double(I); //I is the image of the ROI
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
Int= R + G + B;
Int(find(Int==0))=100000;
r = R./ Int; g = G./Int;
BINS = 32;
rint = round( r*(BINS-1) + 1);
gint = round (g*(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);
subplot(221);
imshow(I)
subplot(222);
imshow(hist,[]);xset('colormap',jetcolormap(64));
//imwrite(hist,'redh.jpg');

/////////////////////////////////////////

I=imread('green.jpg');

//
//R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
//Int= R + G + B;
//r=R./I;
//g=G./I;
//b=B./I;


I = double(I); //I is the image of the ROI
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
Int= R + G + B;
Int(find(Int==0))=100000;
r = R./ Int; g = G./Int;
BINS = 32;
rint = round( r*(BINS-1) + 1);
gint = round (g*(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(2);
subplot(221);
imshow(I);
subplot(222);
imshow(hist,[]);xset('colormap',jetcolormap(64));

//imwrite(hist,'greenh.jpg');

/////////////////////////////////////////

I=imread('blue.jpg');

//
//R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
//Int= R + G + B;
//r=R./I;
//g=G./I;
//b=B./I;


I = double(I); //I is the image of the ROI
R = I(:,:,1); G = I(:,:,2); B = I(:,:,3);
Int= R + G + B;
Int(find(Int==0))=100000;
r = R./ Int; g = G./Int;
BINS = 32;
rint = round( r*(BINS-1) + 1);
gint = round (g*(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(6);
//subplot(221);
//imshow(hist,[]);
scf(3);
subplot(221);
imshow(I)
subplot(222);
imshow(hist,[]);xset('colormap',jetcolormap(64));
//imwrite(hist,'blueh.jpg');

2nd part:

stacksize(50000000);

//parametric segmentation



//ROI,image patch
ip=imread('red.jpg');//Imagepatches: red,green blue.jpg
Rp = ip(:,:,1); Gp= ip(:,:,2); Bp = ip(:,:,3);
Ip=Rp+Gp+Bp; Ip(find(Ip==0))=100000;

//normalize
Rp=Rp./Ip;Gp=Gp./Ip;Bp=Bp./Ip;

//for the probability destribution
//mean-nu
rm=mean(Rp);gm=mean(Gp);bm=mean(Bp);

//stdev-sigma
rs=stdev(Rp);gs=stdev(Gp);bs=stdev(Bp);

//whole image
wi=imread('balls.jpg');
R=wi(:,:,1);G=wi(:,:,2);B=wi(:,:,3);

I=R+G+B;I(find(I==0))=100000;

//normalize
R=R./I;G=G./I;B=B./I;//

//probability in r and g

pr=(1.0/(rs*sqrt(2*%pi))) *exp(-1.0*(R-rm).^2/(2*rs^2));

pg=(1.0/(gs*sqrt(2*%pi))) *exp(-1.0*(G-gm).^2/(2*gs^2));

p=pr.*pg;//joint prob.
//normalize
p=p/max(p);

scf(1);
imshow(p,[]);xset('colormap',hotcolormap(256));//parametric in Gaussian
imwrite(p,'prredpatch.jpg');xset('colormap',hotcolormap(256));

//hm=histogram of the patch


[row,col]=size(ip);

BINS=32;

r=0:1.0/32:1; g=0:1.0/32:1;


ht = zeros(BINS,BINS);//EMPTY

for i=1:row //of imagepatch
for j=1:col
//get
fr=find(r<=ip(i,j,1)); fg=find(g<=ip(i,j,2)); //find frr=size(fr,2); fgg=size(fg,2); ht( fr(frr), fg(fgg) )=ht( fr(frr) ,fg(fgg))+1 ; end end //the backprojection to the whole image [xi,yi]=size(wi);//whole image tag=zeros(xi,yi);// EMPTY for i=1:xi for j=1:yi //get fr=find(r<=wi(i,j,1)); fg=find(g<=wi(i,j,2)); //find frr=length(fr); fgg=length(fg); tag(i,j)=ht(fr(frr), fg(fgg)); end end scf(2); imshow(tag, []);xset('colormap',hotcolormap(256));// non paramertric imwrite(tag,'tagred.jpg');xset('colormap',hotcolormap(256));

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acknowledgements: Mandee salamat sa pagturo ng parametric segmanetation :)


No comments:

Post a Comment