Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / legacy / src / face.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 ///////////////////////////////////////////////
42 //// Created by Khudyakov V.A. bober@gorodok.net
43 //////////////////////////////////////////////
44
45 #include "precomp.hpp"
46 #include "_facedetection.h"
47
48 Face::Face(FaceTemplate * lpFaceTemplate)
49 {
50     //init number of face elements;
51     m_lFaceFeaturesNumber = lpFaceTemplate->GetCount();
52
53     //init array of numbers of foundet face elements of each type
54     m_lplFaceFeaturesCount = new long[m_lFaceFeaturesNumber];
55     memset(m_lplFaceFeaturesCount,0,m_lFaceFeaturesNumber*sizeof(long));
56
57     //init array of ideal face features
58     m_lpIdealFace = new FaceFeature[m_lFaceFeaturesNumber];
59
60     //init array of founded features
61     m_lppFoundedFaceFeatures = new FaceFeature*[m_lFaceFeaturesNumber];
62
63     for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
64     {
65         m_lppFoundedFaceFeatures[i] = (new FaceFeature[3*MAX_LAYERS]);
66     }
67
68     //set start weight 0
69     m_dWeight = 0;
70
71 }//Face::Face(FaceTemplate * lpFaceTemplate)
72
73 Face::~Face()
74 {
75     for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
76     {
77         delete [] (m_lppFoundedFaceFeatures[i]);
78     }
79     delete [] m_lppFoundedFaceFeatures;
80
81
82     delete [] m_lplFaceFeaturesCount;
83     delete [] m_lpIdealFace;
84
85 }//Face::~Face()
86
87
88 #define UP_SCALE    1
89 #define DOWN_SCALE  2
90
91
92 ////////////
93 //class RFace(rect based face)
94 ////////////
95 RFace::RFace(FaceTemplate * lpFaceTemplate):Face(lpFaceTemplate)
96 {
97     //init ideal face
98     FaceFeature * lpTmp = lpFaceTemplate->GetFeatures();
99
100     for (int j = 0;j < m_lFaceFeaturesNumber;j ++)
101     {
102         CvRect * lpTmpRect = NULL;
103         lpTmpRect = new CvRect;
104         *lpTmpRect = *(CvRect*)lpTmp[j].GetContour();
105
106         m_lpIdealFace[j].SetContour( lpTmpRect );
107         m_lpIdealFace[j].SetWeight( lpTmp[j].GetWeight() );
108         m_lpIdealFace[j].SetFeature( lpTmp[j].isFaceFeature() );
109
110     }
111
112     m_bIsGenerated = false;
113 }//RFace::RFace(FaceTemplate * lpFaceTemplate)
114
115 RFace::~RFace()
116 {
117
118 }//RFace::~RFace()
119
120 inline bool RFace::isPointInRect(CvPoint p,CvRect rect)
121 {
122     if ( (p.x >= rect.x) && (p.y >= rect.y) && (p.x <= rect.x + rect.width) && (p.y <= rect.y + rect.height) )
123         return true;
124
125     return false;
126 }//inline bool RFace::isPointInRect(CvPoint,CvRect rect)
127
128 double RFace::GetWeight()
129 {
130     return m_dWeight;
131 }//double RFace::GetWeight()
132
133
134 bool RFace::CheckElem(void * lpCandidat,void * lpIdeal)
135 {
136
137     CvRect IdealRect = *(CvRect*)lpIdeal;
138     CvRect Rect = *(CvRect*)lpCandidat;
139
140     if (Rect.height > Rect.width)
141         return false;
142
143     long SizeIdeal = IdealRect.width*IdealRect.height;
144     long Size = Rect.width*Rect.height;
145
146     if ( (Size > SizeIdeal) || ( Size < (SizeIdeal/5) ) )
147         return false;
148
149 //  CvRect UpRect;
150 //  CvRect DownRect;
151 //  ResizeRect(IdealRect,&UpRect,UP_SCALE,7);
152 //  ResizeRect(IdealRect,&DownRect,DOWN_SCALE,7);
153
154     long x = Rect.x + cvRound(Rect.width/2);
155     long y = Rect.y + cvRound(Rect.height/2);
156
157     if ( isPointInRect(cvPoint(x,y),IdealRect) )
158         return true;
159
160 //  if ( isPointInRect(cvPoint(Rect.x,Rect.y),UpRect) &&
161 //       isPointInRect(cvPoint(Rect.x + Rect.width,Rect.y + Rect.height),UpRect ) &&
162 //       isPointInRect(cvPoint(DownRect.x,DownRect.y),Rect) &&
163 //       isPointInRect(cvPoint(DownRect.x + DownRect.width,DownRect.y + DownRect.height),Rect) )
164 //      return true;
165
166
167 //  if ( isPointInRect(cvPoint(Rect.x,Rect.y),IdealRect) &&
168 //       isPointInRect(cvPoint(Rect.x + Rect.width,Rect.y + Rect.height),IdealRect ) )
169 //      return true;
170
171     return false;
172 }//inline bool RFace::CheckElem(CvRect rect)
173
174
175
176 void RFace::CalculateError(FaceData * lpFaceData)
177 {
178     CvRect LeftEyeRect = lpFaceData->LeftEyeRect;
179     CvRect RightEyeRect = lpFaceData->RightEyeRect;
180     CvRect MouthRect = lpFaceData->MouthRect;
181
182     long LeftSquare = LeftEyeRect.width*LeftEyeRect.height;
183     long RightSquare = RightEyeRect.width*RightEyeRect.height;
184
185     long dy = LeftEyeRect.y - RightEyeRect.y;
186
187     long dx1 = LeftEyeRect.x + LeftEyeRect.width/2 - MouthRect.x;
188     long dx2 = RightEyeRect.x + RightEyeRect.width/2 - MouthRect.x - MouthRect.width;
189
190
191     lpFaceData->Error = (double)(LeftSquare - RightSquare)*(double)(LeftSquare - RightSquare)/((double)(LeftSquare + RightSquare)*(LeftSquare + RightSquare)) +
192                         (double)(dy*dy)/((double)(LeftEyeRect.height + RightEyeRect.height)*(LeftEyeRect.height + RightEyeRect.height)) +
193                         (double)(dx1*dx1)/((double)MouthRect.width*MouthRect.width) +
194                         (double)(dx2*dx2)/((double)MouthRect.width*MouthRect.width);
195
196 }//void RFace::CalculateError(FaceData * lpFaceData)
197
198 #define MAX_ERROR 0xFFFFFFFF
199
200 void  RFace::CreateFace(void * lpData)
201 {
202     FaceData Data;
203     memset(&Data, 0, sizeof(FaceData));
204
205     double Error = MAX_ERROR;
206     double CurError = MAX_ERROR;
207
208     FaceData * lpFaceData = (FaceData*)lpData;
209
210     int im = 0;//mouth was find
211     int jl = 0;//left eye was find
212     int kr = 0;//right eye was find
213
214     long MouthNumber = 0;
215     long LeftEyeNumber = 0;
216     long RightEyeNumber = 0;
217
218     for (int i = 0;i < m_lplFaceFeaturesCount[0] + 1;i ++)
219     {
220
221         if ( !m_lplFaceFeaturesCount[0] )
222             Data.MouthRect = *(CvRect*)m_lpIdealFace[0].GetContour();
223         else
224         {
225             if ( i != m_lplFaceFeaturesCount[0] )
226                 Data.MouthRect = *(CvRect*)m_lppFoundedFaceFeatures[0][i].GetContour();
227             im = 1;
228         }
229
230
231         for (int j = 0;j < m_lplFaceFeaturesCount[1] + 1;j ++)
232         {
233
234             if ( !m_lplFaceFeaturesCount[1] )
235                 Data.LeftEyeRect = *(CvRect*)m_lpIdealFace[1].GetContour();
236             else
237             {
238                 if (j != m_lplFaceFeaturesCount[1] )
239                     Data.LeftEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[1][j].GetContour();
240                 jl = 1;
241             }
242
243
244             for (int k = 0;k < m_lplFaceFeaturesCount[2] + 1;k ++)
245             {
246
247                 if ( !m_lplFaceFeaturesCount[2] )
248                     Data.RightEyeRect = *(CvRect*)m_lpIdealFace[2].GetContour();
249                 else
250                 {
251                     if (k != m_lplFaceFeaturesCount[2] )
252                         Data.RightEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[2][k].GetContour();
253                     kr = 1;
254                 }
255
256                 CalculateError(&Data);
257
258                 if ( (im + jl + kr) )
259                 {
260                     Error = Data.Error/(im + jl + kr);
261                 }else
262                     Error = MAX_ERROR;
263
264                 if (CurError > Error)
265                 {
266                     CurError = Error;
267                     MouthNumber = i;
268                     LeftEyeNumber = j;
269                     RightEyeNumber = k;
270                 }
271
272             }
273
274
275         }
276
277     }
278
279     if ( m_lplFaceFeaturesCount[0] )
280         lpFaceData->MouthRect = *(CvRect*)m_lppFoundedFaceFeatures[0][MouthNumber].GetContour();
281     else
282         lpFaceData->MouthRect = *(CvRect*)m_lpIdealFace[0].GetContour();
283
284     if ( m_lplFaceFeaturesCount[1] )
285         lpFaceData->LeftEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[1][LeftEyeNumber].GetContour();
286     else
287         lpFaceData->LeftEyeRect = *(CvRect*)m_lpIdealFace[1].GetContour();
288
289     if ( m_lplFaceFeaturesCount[2] )
290         lpFaceData->RightEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[2][RightEyeNumber].GetContour();
291     else
292         lpFaceData->RightEyeRect = *(CvRect*)m_lpIdealFace[2].GetContour();
293
294     lpFaceData->Error = CurError;
295
296 }//void * RFace::CreateFace()
297
298 void RFace::Show(IplImage * Image)
299 {
300     for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
301     {
302         if (m_lplFaceFeaturesCount[i])
303         {
304             for (int j = 0;j < m_lplFaceFeaturesCount[i];j ++)
305             {
306                 CvRect rect = *(CvRect*)m_lppFoundedFaceFeatures[i][j].GetContour();
307                 CvPoint p1 = cvPoint(rect.x,rect.y);
308                 CvPoint p2 = cvPoint(rect.x + rect.width,rect.y + rect.height);
309                 cvRectangle(Image,p1,p2,CV_RGB(255,0,0),1);
310             }
311         }
312     }
313
314 }//void RFace::Show(IplImage * Image)
315
316 void RFace::ShowIdeal(IplImage* Image)
317 {
318     for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
319     {
320         CvRect Rect = *(CvRect*)m_lpIdealFace[i].GetContour();
321         CvPoint p1 = cvPoint(Rect.x,Rect.y);
322         CvPoint p2 = cvPoint(Rect.x + Rect.width,Rect.y + Rect.height);
323         cvRectangle(Image,p1,p2,CV_RGB(0,0,255),1);
324     }
325 }//void RFace::ShowIdeal(IplImage* Image)
326
327
328 inline void RFace::ResizeRect(CvRect Rect,CvRect * lpRect,long lDir,long lD)
329 {
330     if (lDir == UP_SCALE)
331     {
332         lpRect->x = Rect.x - lD;
333         lpRect->y = Rect.y - lD;
334         lpRect->width = Rect.width + 2*lD;
335         lpRect->height = Rect.height + 2*lD;
336     }
337     if (lDir == DOWN_SCALE)
338     {
339         lpRect->x = Rect.x + lD;
340         lpRect->y = Rect.y + lD;
341         if (Rect.width - 2*lD >= 0)
342         {
343             lpRect->width = Rect.width - 2*lD;
344         }else
345             lpRect->width = 0;
346
347         if (Rect.height - 2*lD >= 0)
348         {
349             lpRect->height = Rect.height - 2*lD;
350         }else
351             lpRect->height = 0;
352     }
353
354 }// inline void RFace::ResizeRect(CvRect * lpRect,long lDir,long lD)