1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include"f_SkinDetect.h"
#include"../TRGB2YCbCr.h"
#include"../TRGB2HSV.h"
#define MIN2(a, b) ((a) < (b) ? (a) : (b))
#define MAX2(a, b) ((a) > (b) ? (a) : (b))
#define CLIP3(x, a, b) MIN2(MAX2(a,x), b)
/*************************************************************************
*Function: Skin detection in RGB color space
*Params:
*srcData:32BGRA image buffer
*width: width of image
*height: height of image
*stride: Stride of image
*Return: 0-OK,other failed
*Refference: Human skin color clustering for face detection.
**************************************************************************/
int f_SkindetectionRGB(unsigned char* srcData, int width, int height, int stride)
{
int ret = 0;
unsigned char* pSrc = srcData;
int R, G, B;
for(int j = 0; j < height; j++)
{
for(int i = 0; i < width; i++)
{
B = pSrc[0];
G = pSrc[1];
R = pSrc[2];
if(!((R > 95) && (G > 40) && (B > 20) && (R > G) && (R > B) && (MAX2(R, G, B)-MIN2(R, G, B) > 15) && (abs(R - G) > 15)))
{
pSrc[0] = 0;
pSrc[1] = 0;
pSrc[2] = 0;
}
pSrc += 4;
}
}
return ret;
}
/*************************************************************************
*Function: Skin detection in hsv color space
*Params:
*srcData:32BGRA image buffer
*width: width of image
*height: height of image
*stride: Stride of image
*Return: 0-OK,other failed
*Refference: Skin color enhancement based on favorite skin color in HSV color space.
**************************************************************************/
int f_SkindetectionHSV(unsigned char* srcData, int width, int height, int stride)
{
int ret = 0;
unsigned char* pSrc = srcData;
int R, G, B;
float H, S, V;
for(int j = 0; j < height; j++)
{
for(int i = 0; i < width; i++)
{
B = pSrc[0];
G = pSrc[1];
R = pSrc[2];
RGB2HSV(R, G, B, &H, &S, &V);
if (!(((S >= 0.1) && (S <= 0.68)) && ((V >= 0.13 && V <= 0.25 && H >= ((0.4 - V) / 0.014) && H <= ((V + 0.062) / 0.01)) || ((V > 0.25) && (V <= 0.38) && (H >= (0.4 - V) / 0.014) && (H <= (0.67 - V) / 0.014)) || ((V > 0.38) && (V <= 0.46) && (H >= (V - 0.34) / 0.03) && (H <= (0.67 - V) / 0.014)) ||
((V > 0.46) && (V <= 0.6) && (H >= (V - 0.34) / 0.03) && (H <= (V - 0.31) / 0.009)) || ((V > 0.6) && (V <= 0.76) && (H >= (0.91 - V) / 0.14) && (H <= (V - 0.31) / 0.009)) || ((V > 0.76) && (V <= 0.91) && (H >= (0.91 - V) / 0.14) && (H <= (1.17 - V) / 0.0082)) || ((V > 0.91) && (V <= 1) && (H >= (V - 0.91) / 0.0041) && (H <= (1.17 - V) / 0.0082)))))
{
pSrc[0] = 0;
pSrc[1] = 0;
pSrc[2] = 0;
}
pSrc += 4;
}
}
return ret;
}
/*************************************************************************
*Function: Skin detection in ycgcr color space
*Params:
*srcData:32BGRA image buffer
*width: width of image
*height: height of image
*stride: Stride of image
*Return: 0-OK,other failed
*Refference: YCgCr��ɫ�ռ�ķ�ɫ����������ⷨ.
**************************************************************************/
int f_SkindetectionYCgCr(unsigned char* srcData, int width, int height, int stride)
{
int ret = 0;
unsigned char* pSrc = srcData;
int R, G, B;
float Cr, Cg, Cb;
for(int j = 0; j < height; j++)
{
for(int i = 0; i < width; i++)
{
B = pSrc[0];
G = pSrc[1];
R = pSrc[2];
Cg = 128 - 0.318f * R + 0.4392f * G - 0.1212f * B;
Cr = 128 + 0.4392f * R - 0.3677f * G - 0.0714f * B;
if (!((Cg >= 85) && (Cg <= 135) && ((Cr <= (280 - Cg)) && (Cr >= (260 - Cg)))))
{
pSrc[0] = 0;
pSrc[1] = 0;
pSrc[2] = 0;
}
pSrc += 4;
}
}
return ret;
}