Human Skin Color Classification Using The Threshold Classifier: RGB, YCbCr, HSV(Python Code)

The goal of this article is to explain an algorithm for human skin recognition.

Mahmoud Harmouch
The Startup
8 min readMay 24, 2020

--

Skin Segmentation

1 — Abstract

Human skin color detection, for the most part, is used for human face detection. It is one of the best approaches for finding and extracting a person’s face in a given picture. Often it is understood that, for individuals of different races, the fragments skin tones change for some degree. To feature the color of human skin, use different color spaces are accessible.

The three primary spaces for recognizing a skin are RGB (Red, Green, Blue), YCbCr (Luminance, Chrominance) and HSV (Hue, Saturation, Value) color models.

A color model is a term that denotes an abstract model for describing the representation of colors as tuples of numbers, usually of three or four values, called color components or color coordinates. Together with the method of interpreting this data, the many colors of the color model determine the color space.

1.1 — RGB

First of all, you may ask the following question:” how many different colors a modern computer monitor can generate(display)?”

To answer that, we need to be precise because it depends on the color depth for each monitor and the color space. If we take the RGB space for example,16.7 million colors are displayed. Such a large palette is obtained by mixing only three colors in different proportions: red, blue, and green. In graphic editors, each of them is represented by 256 shades (256×256×256 = 16.7 million). The number 256 came from the fact that each color is represented by 8 bits (discrete levels of color per channel), and each resulting color pixel is made up of a combination of three different colors.

RGB values ​​are given by an integer from 0 to 255. For example, RGB(0,0,255) is displayed as blue, since the blue parameter is set to its highest value (255), and the rest are set to 0.

RGB is a color model, named after the three capital letters of the names of the colors underlying it: Red , Green , Blue , or red, green, blue. The same colors form all the intermediate ones. The scientific name is the additive model (from the English word add — “add”). It is used to display images on monitors and other electronic devices. It has a large color gamut.

The image in this color model consists of three channels. When mixing primary colors, for example:

  • Blue ( B ) and Red ( R ) turns into Magenta ( M, Magenta),
  • Green ( G ) and Red ( R ) turns into Yellow ( Y, Yellow ),
  • Green ( G ) and Blue ( B ) turns into Cyan ( C, Cyan )

When all three primary colors are mixed, white color is obtained ( W, White).

1.2 — YCbCr

YCbCr-model

The name of this color model is decrypted as Y — luminance, U or Cb — Chrominance-blue, V or Cr — Chrominance-red, which translates as “luminance — Blue color — Red color” (format for representing color video image data)

The YCbCr signals are generated from the corresponding gamma-configured RGB (red, green, and blue) source as follows(JPEG conversion):

Y = 0.299×R + 0.587×G + 0.114×B

Cb = — 0.1687×R — 0.3313×G + 0.5×B + 128

Cr = 0.5×R 0.4187×G — 0.0813×B + 128

the range of each input (R, G, B) is the full 8-bit range of [0…255]

Formulas for the inverse transformation of the YCbCr color model to RGB:

R = Y + 1.402×(Cr-128)

G = Y — 0.34414×(Cb — 128) — 0.71414×(Cr — 128)

B = Y + 1.772×(Cb — 128)

The Y component shows the same picture, only in black(Brightness low) and white(Brightness high).
Usually, according to the formula for calculating component Y, the image is converted to shades of gray and, the picture is clear, although in gray tones.
Images of color components Cb and Cr carry the blue and red components of the image.

1.3 — HSV

HSV is a model, which in principle is an analog of RGB, it is based on its colors, but differs in the coordinate system. HSV presents colors in a more intuitive and readable way than typical RGB. The model is often used in graphic applications, in color palettes, and for image analysis.

HSV stands for Hue (color / hue), Saturation (saturation), Value /Luminance (lightness / luminosity / luminosity, not to be confused with brightness).

Hue sets the position of the color on the color wheel (from 0' to 360'). Saturation is a percentage of saturation (0% to 100%). Value is the percentage of luminosity (0% to 100%).

2 — Skin thresholding Algorithm

The following techniques separate skin and non-skin tone using a linear separation. These express skin group techniques propose a lot of fixed skin limits in a given shading space. Some color spaces permit looking through skin color pixels in the 2D chromatic space, diminishing reliance on lighting variety, others, for example, the RGB space, address the lighting issue by presenting various guidelines relying upon enlightenment conditions(uniform daylight…).

2.1 — RGB (Rule A)

Kovac et al work inside the RGB color space and manage the brightening conditions under which the picture is taken. In this way, they classify skin color by heuristic standards that consider two unique conditions: uniform light and flash or lateral enlightenment. ( ‘,’ means ‘AND’ operation)

  • Uniform daylight illumination:(Rule1)

R > 95 , G > 40 , B > 20,

(Max { R , G , B } − min { R , G , B } )> 15,

|R − G| > 15 , R > G , R > B

  • Flashlight or daylight lateral illumination:(Rule2)

R > 220 , G > 210 , B > 170,

|R − G| ≤ 15 , B < R , B < G .

we need a logical OR to combine both Rule1 and Rule2. The final rule is defined as follows: ( ‘U’ means ’OR’ )

RGB_Rule = (Rule_1) U (Rule_2)

In python, you can implement these equations with the help of the NumPy library:

Uniform daylight illumination:(Rule1)
Flashlight or daylight lateral illumination:(Rule2) and RGB_Rule

as you can see, I am using the reduce method to compute the logical operations(OR, AND) of multiple matrices.

the output of this mask (RGB mask) may look like this:

RGB-Mask

2.2 — YCbCr (Rule B)

A skin color map is determined and utilized on the chrominance segments of the input picture to distinguish pixels that seem, by all accounts, to be skin. The calculation at that point utilizes a lot of regularization procedures to fortify those areas of skin color pixels that are bound to have a place with the facial locales. We utilize just their color division step here. Working in the YCbCr space, the creators of this algorithm find that the scopes of Cb and Cr generally delegate for the skin-color reference map were:

Cr ≤ 1.5862 × Cb + 20
Cr ≥ 0.3448 × Cb + 76.2069
Cr ≥ -4.5652 × Cb + 234.5652
Cr ≤ -1.15 × Cb + 301.75
Cr ≤ -2.2857 × Cb + 432.85

this can be implemented in python as follows:

where line1–5 is the output of this method:

we can visualize these lines in the Cb-Cr space like this:

What I have noticed here that, the slope of line3(red line) should not be equal to -4.5652 as the paper suggests in [1], it should be equal to something around -1.(typo)

When applying this mask on a given image, it will produce the next picture:

2.3 — HSV (Rule C)

In the HSV space, the hue values perform the most important separation between the skin and non-skin regions. based on my experiments, we can estimate the hue value as:

H < 50
H > 150

The output of the Hue mask looks like:

2.4 — RGB-YCbCr-HSV

If we combine all of the previous masks, the algorithm will generate the RGB-YCbCr-HSV mask, and if we apply this mask on a given image it will generate this output:

skin extraction

3 — Conclusion

As a final remark, I have noticed that, in order to have a good output, you need to slightly change the coefficients of the previous equations to better suit your expectation in your results.

for more information, feel free to clone/fork my repo on GitHub.

if you want to improve my code, you can send me a pull request providing your changes.

Happy Reading!!!

Reference

[1] N.Rahman, K.Wei and J.See, RGB-H-CbCr Skin Colour Model for Human Face Detection(2006)Faculty of Information Technology, Multimedia University.

--

--

Mahmoud Harmouch
The Startup

Senior Blockchain Rust Enjoyer at GigaDAO - I occasionally write articles about data science, machine learning and Blockchain in Rust - Currently Writing Books