23 Ekim 2016 Pazar

Skin Color Segmentation (MATLAB)



There are many researches that apply to skin color-based image classification. The main reason of detecting skin colors is building a mechanism in the classification of pixels whether they are skin or not. Many methods have been used in order to predict skin color in literature. One of the simplest and fastest among those is to classify by evaluating pixel values according to pre-determined ranges.  Range classification has been widely used due to being easy to deploy and producing instant results. Image pixels are classified by evaluating whether it is skin or not in different color spaces – normRGB, HSV, and YcbCr are used in this work.



A.    NormRGB

One of the most widely used methods to detect skin color is the normRGB. In this method, RGB values of each pixel are summed and each component is divided by this summation. As a result, new rgb values are generated (Equation 1). And then, the ranges that belong to skin colors containing skin are given using those rgb values (Equation 2). 

                        

Since sum of the normalized values equals to 1, b component can be omitted for the sake of simplicity and reducing space dimensionality.

B.    YCrCb
In YCrCb color space, while Y represents the luminance component of a pixel, the Cr and Cb components represents to the red and the chrominance respectively – showing how much each component deviate from gray. 

                        
C.    HSV

Three constituent components form HSV color space model: Hue, the color type ranging from 0 to 360, Saturation, the vibrancy of the color ranging from 0 to 100%, and the Brightness of the color ranging from 0 to 100% .
Below, the Equation 5 shows the pseudo-code for the transform from RGB color space into HSV color space:


 


                                          

For HSV color space, skin pixels are in the range shown in Equation 6.


0≤ H ≤ 50,
0.23 ≤ S ≤ 0.69                                                                    (6)


As an example, skin colors of the image shown in Figure-1 (a) are examined for each of the three color spaces mentioned above. In (b),(c), and (d), a white pixel represents skin for the image. It is evident that the best result is obtained by YCrCb color space for the image seen in Figure-1 (a). 




MATLAB FUNCTIONS 


Norm RGB

function skin = normRGBTest(P)
R = P(1);
G = P(2);
B = P(3);
nud = 0;
sumRGB = sum(P);
nR = R/sumRGB;
nG = G/sumRGB;

c1 = nR/nG;
c2 = (R*B)/(sumRGB*sumRGB);
c3 = (R*G)/(sumRGB*sumRGB);

if(c1>1.185 && c2>0.107 && c3>0.112)
    skin = 1;
end

end
YCrCb
function skin = function skin = YCbCrTest(P)
R = P(1);
G = P(2);
B = P(3);
skin = 0;

deltP = 128;
y = (0.299*R + 0.587*G + 0.114*B);

Cb = (B-y)*0.564 + deltP ;
Cr = (R-y)*0.713 + deltP ;

if(y>80 && (Cb>77 && Cb <127 r=""> 133 && Cr<173 code="" end="" skin="true;">
HSV


function nud = hsvTest(P)
tmp = min(P);
R = P(1);
G = P(2);
B = P(3);
nud = 0;
value = max(P);

if(value == 0)
    saturation = 0;
else
    saturation = (value-tmp)/value;
end
if (saturation == 0)
    
    hue = -1;
else
   Cr = (value-R)/(value-tmp);
   Cg = (value-G)/(value-tmp);
   Cb = (value-B)/(value-tmp);
   
   if(R==value)
       hue = Cb-Cg;
   end
   if(G==value)
       hue = 2 + Cr-Cb;
   end
   if(B==value)
       hue = 4 + Cg-Cr;
   end
   
   hue = hue * 60;
   
   if(hue<0 end="" hue="" if="">=0) && (hue<=50) && (saturation>=0.23) && (saturation<0 .69="" code="" end="" nud="true;">
Sample Usege of functions for an image


function Ifilter = skinDetector(I)

Ifilter = rgb2gray(I);
[x y z] = size(I);

s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;


for j=1:y

    for i=1:x
    
    A1 = double(I(i,j,1));
    A2 = double(I(i,j,2));
    A3 = double(I(i,j,3));
    s1 = YCbCrTest([A1,A2,A3]);
    s2 = hsvTest([A1,A2,A3]);
    s3 = normRGBTest([A1,A2,A3]);
    
    if(s1|| s2 || s3)
       Ifilter(i,j) = 255;
       
    else
       Ifilter(i,j) = 0;
      
    end
    
    
    end
end
end


Hiç yorum yok :

Yorum Gönder