File: crvskindetection.cpp

package info (click to toggle)
eviacam 2.1.3-4.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 12,324 kB
  • sloc: xml: 29,876; cpp: 18,220; ansic: 3,032; makefile: 337; sh: 46; sed: 16
file content (174 lines) | stat: -rwxr-xr-x 4,720 bytes parent folder | download | duplicates (3)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/////////////////////////////////////////////////////////////////////////////
// Name:        crvskindetection.cpp
// Purpose:  
// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
// Modified by: 
// Created:     23/02/2008
// Copyright:   (C) 2008 Cesar Mauri Loba - CREA Software Systems
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////
#include "crvmisc.h"
#include "crvskindetection.h"
#include <assert.h>
#include <string.h>

void CSkinRegionModel::crvBinarizeSkin_KToyama (IplImage *pIpl, float &krg_min, 
												  float &krg_max, float &krb_min, 
												  float &krb_max)
{
	int x, y, xIni, yIni, xLim, yLim;
	float krg, krb;
	unsigned char *pSrc;

	assert (pIpl);

	crvGetROILimits (pIpl, xIni, yIni, xLim, yLim);
	
	for (y= yIni; y< yLim; y++) {
		pSrc= (unsigned char *) crvImgOffset (pIpl, xIni, y);		
		for (x= xIni; x< xLim; x++) {
			krg= (float) pSrc[0] / (float) pSrc[1];
			krb= (float) pSrc[0] / (float) pSrc[2];

			if (krg_min<= krg && krg_max>= krg &&
				krb_min<= krb && krb_max>= krb) {
				pSrc[0]= pSrc[1]= pSrc[2]= 255;
			}
			else 
				pSrc[0]= pSrc[1]= pSrc[2]= 0;

			pSrc+= 4;
		}
	}
}

// 
// Skin Models Definition
//
#define min(x,y) (x< y? x : y)
#define max(x,y) (x> y? x : y)
#define min3(x,y,z) (min(min(x,y),z))
#define max3(x,y,z) (max(max(x,y),z))


// The skin colour at uniform daylight illumination
#define IS_SKIN_PEER(R,G,B)		(R> 95 && G> 40 && B> 20 && R> G && R> B && \
								(max3(R,G,B) - min3(R,G,B))> 15	&& \
								((R-G)> 15 || (G-R)> 15))

// The skin colour under flashlight or (light) daylight
// lateral illumination
#define IS_SKIN_PEER2(R,G,B)	(R> 220 && G> 210 && B> 170 && \
								((R-G)< 15 || (G-R)< 15) && \
								R> B && G> B)
								
								

inline bool IsSkin_GomezMorales_1 (int R, int G, int B)
{
	float r, g, b, rgb, rgb2;

	r= (float) R / 255.0f;	g= (float) G / 255.0f;	b= (float) B / 255.0f;
	rgb= r + g + b;
	rgb2= rgb * rgb;

	return ( ((r/g)> 1.185) && (((r*b)/rgb2)> 0.107) && (((r*g)/rgb2)> 0.112) );
}

inline bool IsSkin_GomezMorales_2 (int R, int G, int B)
{
	float r, g, b, rgb;

	r= (float) R / 255.0f;	g= (float) G / 255.0f;	b= (float) B / 255.0f;
	rgb= r + g + b;
	
	return ( (b/g)< 1.249 && (rgb / (3*r))> 0.696 && 
			 ((1.0/3.0) - (b/rgb))> 0.014 && (g / (3*rgb))< 0.108 );
}

void CSkinRegionModel::crvBinarizeSkin  (IplImage *pSrc, IplImage *pDst)
{
	int x, y, xIni, yIni, xLim, yLim, srcBytesPPixel, dstBytesPPixel;
	unsigned char *pSrcB, *pDstB;
	bool isBGR= false;
	
	assert (pSrc && pSrc->imageData && pDst && pDst->imageData);
	if (!strncmp (pSrc->channelSeq, "BGR", 4) || !strncmp (pSrc->channelSeq, "BGRA", 4))
		isBGR= true;
	else if (!strncmp (pSrc->channelSeq, "RGB", 4) || !strncmp (pSrc->channelSeq, "RGBA", 4))
		isBGR= false;
	else
		assert (false);
	assert (!strncmp (pDst->channelSeq, "GRAY", 4) || !strncmp (pDst->channelSeq, "BGRA", 4));

	// Get depth
	srcBytesPPixel= pSrc->nChannels;
	dstBytesPPixel= pDst->depth / 8;
	
	cvSetZero(pDst);
	
	crvGetROILimits (pSrc, xIni, yIni, xLim, yLim);

	if (isBGR)
		for (y= yIni; y< yLim; y++) {
			pSrcB= (unsigned char *) crvImgOffset (pSrc, xIni, y);
			pDstB= (unsigned char *) crvImgOffset (pDst, xIni, y);

			for (x= xIni; x< xLim; x++) {

				if (IS_SKIN_PEER(pSrcB[2],pSrcB[1],pSrcB[0]))			
				{
					// Color acceptat
					pDstB[0]= 255;
					if (dstBytesPPixel> 1)
					{
						pDstB[1]= 255;
						if (dstBytesPPixel> 2)
						{
							pDstB[2]= 255;
						}
					}
				}			

				pSrcB+= srcBytesPPixel;
				pDstB+= dstBytesPPixel;
			}
		}
	else
		for (y= yIni; y< yLim; y++) {
			pSrcB= (unsigned char *) crvImgOffset (pSrc, xIni, y);
			pDstB= (unsigned char *) crvImgOffset (pDst, xIni, y);

			for (x= xIni; x< xLim; x++) {

				if (IS_SKIN_PEER(pSrcB[0],pSrcB[1],pSrcB[2]))			
				{
					// Color acceptat
					pDstB[0]= 255;
					if (dstBytesPPixel> 1)
					{
						pDstB[1]= 255;
						if (dstBytesPPixel> 2)
						{
							pDstB[2]= 255;
						}
					}
				}			

				pSrcB+= srcBytesPPixel;
				pDstB+= dstBytesPPixel;
			}
		}
}