Tuesday, August 13, 2013

Activity 9: Color Image Segmentation


I've finally been able to understand this Activity after hearing its discussion in the subject AP 187.Weird huh?

Anyway, when converting colored images (RGB) to grayscale, we lose valuable information. How do we minimize such losses? We can process the said image via "segmentation [1]."  The color of an object is actually a good description of its brightness and chromaticity. As such, instead of using RGB color space, we use NCC or normalized chromaticity coordinates.



There are two ways to do so, one is parametric and the other is non-parametric.
What are the steps in doing both? Simple, for parametric:
1. Crop a region of interest on the image.
2. Obtain the NCC of both the cropped image and whole image.
3. Compute the probability of the r and g values of the NCC in the cropped image and obtain its joint probability using:

4.  Compute the probability of the whole image.

For non-parametric:
1. Crop a region of interest on the image.
2. Obtain its 2-d histogram (which is essentially its PDF)
3.Back-project the binned histogram upon the whole image

Parametric has many assumptions. Its advantage is that it's useful for colors with different shadings. Non-parametric give finer control depending on the binning.




Parametric Image


Non-parametric

Code snippet:
ROI = double(imread('C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity 9\ROI.png'));
Orig = double(imread('C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity 9\Self.png'));
R = ROI(:,:,1);
G = ROI(:,:,2);
B = ROI(:,:,3);
I = R+G+B;
I(find(I==0))=100000;

Ri = Orig(:,:,1);
Gi = Orig(:,:,2);
Bi = Orig(:,:,3);
OI = Ri+Gi+Bi;
OI(find(OI==0))=100000;

ri = Ri./OI;
bi = Bi./OI;
gi = Gi./OI;

r = R./I;
g = G./I;

//Parametric 
meanr = mean(r);
meang = mean(g);
sigmar = stdev(r);
sigmag = stdev(g);

rp = (1/(sigmar*sqrt(2*%pi)))*exp(-((ri - meanr).^2)/(2*sigmar^2));
gp = (1/(sigmag*sqrt(2*%pi)))*exp(-((gi - meang).^2)/(2*sigmag^2));

rpgp = round(rp.*gp);
//imshow(rpgp);
imwrite(rpgp,'C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity 9\Parametric.bmp')


//Non-Parametric

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;
//imshow(hist);

//Backprojection
Nonpara = zeros(size(Orig,1),size(Orig,2));
for i=1:size(Orig,1)
    for j = 1:size(Orig,2)
        X = round(ri(i,j)*(BINS-1))+1;
        Y = round(gi(i,j)*(BINS-1))+1;
        Nonpara(i,j) = hist(X,Y);
    end   
end
imwrite(Nonpara,'C:\Users\Phil\Desktop\Academic Folder\Academic Folder 13-14 First Sem\AP 186\Activity 9\Non-parametric.bmp')

References:
[1]  Soriano, Jing, Activity 9 - Color Image Segmentation, 2013


No comments:

Post a Comment