Tuesday, September 2, 2008

A16- Color Image Segmentation

According to wikipedia, segmentation refers to the process of partitioning a digital image into multiple region. The goal of segmentation is to simplify and/or change the representation of an image into something that is more meaningful and easier to analyze. Image segmentation is typically used to locate objects and boundaries (lines, curves, etc.) in images.

Note that for any colored image, each pixel color is a sum of the different contributions of red, blue and green. So if a pixel has a color value of I, then it can be written as:

I = R + G + B.

Alternatively, we can also normalize this color values so that,
r=R/I,
g=G/I,
b=B/I.
where r+g+b=1.

In this activity, I applied the concept of color segmentation on two images. There are two ways to do this namely, parametric and non-parametric.

For parametric segmentation, I took a patch from the region of interest or ROI. I then solve for p(r) and p(g). These are the probability density of the red and green pixels respectively. Assuming that the distribution of the pixels is Gaussian, we can write the PDF as:

where sigma and mu are the standard deviation and mean of the corresponding colors respectively. The joint PDF is then equal to p(r)*p(g). I then applied this resulting PDF to the image shown in Figure 1 and the result is Figure 3.

For the nonparametric segmentation. We obtained a three-dimensional histogram with the x- and y- axes as the red and green vaues. The code used is written below. This histogram will then be used to segment the image.

Figure 1. A blue ball will serve as an image

Figure 2. The ROI of the Image

Figure 3. Result of Parametric Segmentation

Figure 4. Result of Non-Parametric Segmentation

Figure 5. 3d Histogram and Chromacity Plane

I used the following codes:

im = imread("ball.jpg");
im1 = imread("ball3.jpg");
//To compute for the RGB values of each element in the segmented image
R = im(:,:,1);
G = im(:,:,2);
B = im(:,:,3);
R1 = im1(:,:,1);
G1 = im1(:,:,2);
B1 = im1(:,:,3);
I = R + G + B;
I1 = R1 + G1 + B1;
r = R./I;
g = G./I;
b = B./I;
r1 = R1./I1;
g1 = G1./I1;
b1 = B1./I1;

// For Parametric

// Computing for the Histogram
mured = mean(r1);
mugreen = mean(g1);
sigmared = st_deviation(r1);
sigmagreen = st_deviation(g1);
pr = exp(-(r-mured^2)/(2*sigmared))./(sigmared.*sqrt(2*%pi));
pg = exp(-(g-mugreen^2)/(2*sigmagreen))./(sigmagreen.*sqrt(2*%pi));
prg = pr.*pg;
imshow(prg);

// For Non-Parametric

// Computing for the 3D Histogram
binsize = 32;
RED1 = r1*binsize;
GREEN1 = g1*binsize;
PDF = zeros(binsize,binsize); // define a matrix for the PDF
[m1,n1] = size(RED1);
for i = 1:m1;
for j = 1:n1;
x1 = round(RED1(i,j))+1; // x gives the red value
y1 = round(GREEN1(i,j))+1; // gives the green value
PDF(x1,y1) = PDF(x1,y1)+1;
end;
end;
plot3d(1:binsize,1:binsize,PDF);
// To write a new image based on the histogram
RED = r*binsize;
GREEN = g*binsize;
[m,n] = size(RED);
Image = zeros(size(im,1),size(im,2));
for k=1:m;
for l=1:n;
x = round(RED(k,l))+1;
y = round(GREEN(k,l))+1;
Image(k,l) = PDF(x,y);
end;
end;
xbasc();
imshow(Image);

Since I found that non-parametric segmentation is better in comparison to parametric segmentation, I used this to the image of a hand that appears below.

Figure 6. Image 2: Bug

Figure 7. ROI for figure 6
Figure 8. 3d Histogram for Figure 6

Figure 9. Result for non-parametric segmentation

I rate myself 10/10 for my effort in this activity.

Reference:
Picture from:
http://charmed-charmedimsure.blogspot.com/2007/12/blue-ball.html
http://en.wikipedia.org/wiki/Segmentation_(image_processing)

No comments: