September 2, 2008
Suppose I have a reference image Mpatch and an image I want to segment M, they have NCC (normalized chromaticidty coordinates) components rpatch, gpatch and bpatch, and r, g, and b
Code for non-parametric image segmentation by color:
delta_pix = 0.01;
val=[];
num_=[];
num=[];
counter=1;
for i=0:delta_pix:1;
[x,y]=find((rpatch>=i) & (rpatch<(i+delta_pix)));
val(counter)=i;
num_(counter)=length(x);
counter=counter+1;
end
num = num_./((size(rpatch,1))*size(rpatch,2));
plot(val, num_);
B = zeros(size(M,1),size(M,2),3);
delta_pix = 0.01;
val=[];
num_=[];
num=[];
counter=1;
for i=0:delta_pix:1;
[x,y]=find((gpatch>=i) & (gpatch<(i+delta_pix)));
val(counter)=i;
num_(counter)=length(x);
counter=counter+1;
end
num = num_./((size(gpatch,1))*size(gpatch,2));
plot(val, num_);
for i = 0:delta_pix:1
[x,y] = find((g>=i) & (g<(i+delta_pix)));
for k=1:length(x) for l=1:length(y)
B(x(k),y(l),2) = num_((i+delta_pix)/delta_pix);
end
end
i
end
B(:,:,3) = 1-B(:,:,1)-B(:,:,2);
Code for parametric segmentation:
delta_pix = 0.01;
val=[];
num_=[];
PDF = [];
counter=1;
for i=0:delta_pix:1;
[x,y]=find((rpatch>=i) & (rpatch<(i+delta_pix)));
val(counter)=i;
num_(counter)=length(x);
counter=counter+1;
end
PDF = num_./((size(rpatch,1))*size(rpatch,2));
plot(val, PDF);
mn = find(PDF==max(PDF));
mu_red = val(mn);
sigma_red = stdev(PDF);
val=[];
num_=[];
PDF = [];
counter=1;
for i=0:delta_pix:1;
[x,y]=find((gpatch>=i) & (gpatch<(i+delta_pix)));
val(counter)=i;
num_(counter)=length(x);
counter=counter+1;
end
PDF = num_./((size(gpatch,1))*size(gpatch,2));
plot(val,PDF);
mn = find(PDF==max(PDF));
mu_green = val(mn);
sigma_green = stdev(PDF);
x = [0:delta_pix:1];
pr = (exp(-((x-mu_red)^2)/(2*sigma_red)))/(sigma_red*sqrt(2*%pi));
pg = (exp(-((x-mu_green)^2)/(2*sigma_green)))/(sigma_green*sqrt(2*%pi));
K = zeros(size(M,1),size(M,2));
for i = 0:delta_pix:1
[x,y] = find((r>=i) & (r<(i+delta_pix)));
for k=1:length(x)
for l=1:length(y)
green = round(g(x(k),y(k)));
probability = pr((i+delta_pix)/delta_pix)*pg((green+delta_pix)/delta_pix);
K(x(k),y(l)) = probability;
end
end
i
end
Display
G = im2gray(B);
L = im2gray(M);
subplot(311)
imshow(L,[]);
subplot(312)
imshow(G,[]);
Figure 1. The raw image.
Figure 2. The reference patch which belongs to the hand.
Figure 3. The raw image (top) and the maps showing the probability that a pixel belongs to the region of interest which in this case is the skin. The center image is the result of non-parametric image segmentation while the bottom image is the result of parametric image-segmentation.
Self-grade: 10/10 because I was able to finish the activity on time and my parametric and non-parametric image segmentations result looks reasonable
Thank you, Lei for helping me with histogramming.