1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 /****************************************************************************************\
43 Contour-based face feature tracking
44 The code was created by Tatiana Cherepanova (tata@sl.iae.nsk.su)
45 \****************************************************************************************/
48 #include "_cvvectrack.h"
50 #define _ASSERT assert
51 #define NUM_FACE_ELEMENTS 3
61 const double pi = 3.1415926535;
64 struct CvTrackingRect;
67 void ThresholdingParam(IplImage *imgGray, int iNumLayers, int &iMinLevel, int &iMaxLevel, float &step, float& power, int iHistMin /*= HIST_MIN*/);
68 int ChoiceTrackingFace3(CvFaceTracker* pTF, const int nElements, const CvFaceElement* big_face, CvTrackingRect* face, int& new_energy);
69 int ChoiceTrackingFace2(CvFaceTracker* pTF, const int nElements, const CvFaceElement* big_face, CvTrackingRect* face, int& new_energy, int noel);
70 inline int GetEnergy(CvTrackingRect** ppNew, const CvTrackingRect* pPrev, CvPoint* ptTempl, CvRect* rTempl);
71 inline int GetEnergy2(CvTrackingRect** ppNew, const CvTrackingRect* pPrev, CvPoint* ptTempl, CvRect* rTempl, int* element);
72 inline double CalculateTransformationLMS3_0( CvPoint* pTemplPoints, CvPoint* pSrcPoints);
73 inline double CalculateTransformationLMS3( CvPoint* pTemplPoints,
75 double* pdbAverageScale,
76 double* pdbAverageRotate,
77 double* pdbAverageShiftX,
78 double* pdbAverageShiftY );
91 CvTrackingRect() { memset(this, 0, sizeof(CvTrackingRect)); };
92 int Energy(const CvTrackingRect& prev)
94 int prev_color = 0 == prev.iColor ? iColor : prev.iColor;
95 iEnergy = 1 * pow2(r.width - prev.r.width) +
96 1 * pow2(r.height - prev.r.height) +
97 1 * pow2(iColor - prev_color) / 4 +
101 + 0 * nRectsOnRight +
102 + 0 * nRectsOnBottom;
109 CvTrackingRect face[NUM_FACE_ELEMENTS];
110 int iTrackingFaceType;
111 double dbRotateDelta;
112 double dbRotateAngle;
115 CvPoint ptTempl[NUM_FACE_ELEMENTS];
116 CvRect rTempl[NUM_FACE_ELEMENTS];
120 CvMemStorage* mstgContours;
127 iTrackingFaceType = -1;
136 if (NULL != imgThresh)
138 if (NULL != mstgContours)
139 cvReleaseMemStorage(&mstgContours);
141 int Init(CvRect* pRects, IplImage* imgGray)
143 for (int i = 0; i < NUM_FACE_ELEMENTS; i++)
145 face[i].r = pRects[i];
146 face[i].ptCenter = Center(face[i].r);
147 ptTempl[i] = face[i].ptCenter;
148 rTempl[i] = face[i].r;
150 imgGray = cvCreateImage(cvSize(imgGray->width, imgGray->height), 8, 1);
151 imgThresh = cvCreateImage(cvSize(imgGray->width, imgGray->height), 8, 1);
152 mstgContours = cvCreateMemStorage();
153 if ((NULL == imgGray) ||
154 (NULL == imgThresh) ||
155 (NULL == mstgContours))
159 int InitNextImage(IplImage* img)
161 CvSize sz = {img->width, img->height};
162 ReallocImage(&imgGray, sz, 1);
163 ReallocImage(&imgThresh, sz, 1);
164 ptRotate = face[MOUTH].ptCenter;
166 CvMat mat = cvMat( 2, 3, CV_32FC1, m );
168 if (NULL == imgGray || NULL == imgThresh)
171 /*m[0] = (float)cos(-dbRotateAngle*CV_PI/180.);
172 m[1] = (float)sin(-dbRotateAngle*CV_PI/180.);
173 m[2] = (float)ptRotate.x;
176 m[5] = (float)ptRotate.y;*/
177 cv2DRotationMatrix( cvPointTo32f(ptRotate), -dbRotateAngle, 1., &mat );
178 cvWarpAffine( img, imgGray, &mat );
180 if (NULL == mstgContours)
181 mstgContours = cvCreateMemStorage();
183 cvClearMemStorage(mstgContours);
184 if (NULL == mstgContours)
194 CvMemStorage* m_mstgRects;
196 CvTrackingRect m_trPrev;
197 inline CvFaceElement()
206 inline int Init(const CvRect& roi, const CvTrackingRect& prev, CvMemStorage* mstg = NULL)
212 if (NULL == m_mstgRects)
214 if (NULL == m_seqRects)
215 m_seqRects = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvTrackingRect), m_mstgRects);
217 cvClearSeq(m_seqRects);
218 if (NULL == m_seqRects)
222 void FindRects(IplImage* img, IplImage* thresh, int nLayers, int dMinSize);
224 void FindContours(IplImage* img, IplImage* thresh, int nLayers, int dMinSize);
225 void MergeRects(int d);
227 }; //class CvFaceElement
229 int CV_CDECL CompareEnergy(const void* el1, const void* el2, void*)
231 return ((CvTrackingRect*)el1)->iEnergy - ((CvTrackingRect*)el2)->iEnergy;
232 }// int CV_CDECL CompareEnergy(const void* el1, const void* el2, void*)
234 void CvFaceElement::FindRects(IplImage* img, IplImage* thresh, int nLayers, int dMinSize)
236 FindContours(img, thresh, nLayers, dMinSize / 4);
237 if (0 == m_seqRects->total)
240 cvSeqSort(m_seqRects, CompareEnergy, NULL);
241 CvTrackingRect* pR = (CvTrackingRect*)cvGetSeqElem(m_seqRects, 0);
242 if (m_seqRects->total < 32)
244 MergeRects(dMinSize / 8);
246 cvSeqSort(m_seqRects, CompareEnergy, NULL);
248 pR = (CvTrackingRect*)cvGetSeqElem(m_seqRects, 0);
249 if ((pR->iEnergy > 100 && m_seqRects->total < 32) || (m_seqRects->total < 16))
251 MergeRects(dMinSize / 4);
253 cvSeqSort(m_seqRects, CompareEnergy, NULL);
255 pR = (CvTrackingRect*)cvGetSeqElem(m_seqRects, 0);
256 if ((pR->iEnergy > 100 && m_seqRects->total < 16) || (pR->iEnergy > 200 && m_seqRects->total < 32))
258 MergeRects(dMinSize / 2);
260 cvSeqSort(m_seqRects, CompareEnergy, NULL);
263 }// void CvFaceElement::FindRects(IplImage* img, IplImage* thresh, int nLayers, int dMinSize)
265 void CvFaceElement::FindContours(IplImage* img, IplImage* thresh, int nLayers, int dMinSize)
270 cvSetImageROI(img, roi);
271 cvSetImageROI(thresh, roi);
273 int colors[MAX_LAYERS] = {0};
274 int iMinLevel = 0, iMaxLevel = 255;
276 ThresholdingParam(img, nLayers / 2, iMinLevel, iMaxLevel, step, power, 4);
277 int iMinLevelPrev = iMinLevel;
278 int iMaxLevelPrev = iMinLevel;
279 if (m_trPrev.iColor != 0)
281 iMinLevelPrev = m_trPrev.iColor - nLayers / 2;
282 iMaxLevelPrev = m_trPrev.iColor + nLayers / 2;
284 if (iMinLevelPrev < iMinLevel)
286 iMaxLevelPrev += iMinLevel - iMinLevelPrev;
287 iMinLevelPrev = iMinLevel;
289 if (iMaxLevelPrev > iMaxLevel)
291 iMinLevelPrev -= iMaxLevelPrev - iMaxLevel;
292 if (iMinLevelPrev < iMinLevel)
293 iMinLevelPrev = iMinLevel;
294 iMaxLevelPrev = iMaxLevel;
297 n -= (iMaxLevelPrev - iMinLevelPrev + 1) / 2;
298 step = float(iMinLevelPrev - iMinLevel + iMaxLevel - iMaxLevelPrev) / float(n);
301 for (level = (float)iMinLevel; level < iMinLevelPrev && j < nLayers; level += step, j++)
302 colors[j] = int(level + 0.5);
303 for (level = (float)iMinLevelPrev; level < iMaxLevelPrev && j < nLayers; level += 2.0, j++)
304 colors[j] = int(level + 0.5);
305 for (level = (float)iMaxLevelPrev; level < iMaxLevel && j < nLayers; level += step, j++)
306 colors[j] = int(level + 0.5);
308 for (int i = 0; i < nLayers; i++)
310 cvThreshold(img, thresh, colors[i], 255.0, CV_THRESH_BINARY);
311 if (cvFindContours(thresh, m_mstgRects, &seq, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE))
314 for (CvSeq* external = seq; external; external = external->h_next)
316 cr.r = cvContourBoundingRect(external);
317 Move(cr.r, roi.x, roi.y);
318 if (RectInRect(cr.r, m_rROI) && cr.r.width > dMinSize && cr.r.height > dMinSize)
320 cr.ptCenter = Center(cr.r);
321 cr.iColor = colors[i];
322 cvSeqPush(m_seqRects, &cr);
324 for (CvSeq* internal = external->v_next; internal; internal = internal->h_next)
326 cr.r = cvContourBoundingRect(internal);
327 Move(cr.r, roi.x, roi.y);
328 if (RectInRect(cr.r, m_rROI) && cr.r.width > dMinSize && cr.r.height > dMinSize)
330 cr.ptCenter = Center(cr.r);
331 cr.iColor = colors[i];
332 cvSeqPush(m_seqRects, &cr);
339 cvResetImageROI(img);
340 cvResetImageROI(thresh);
341 }//void CvFaceElement::FindContours(IplImage* img, IplImage* thresh, int nLayers)
343 void CvFaceElement::MergeRects(int d)
345 int nRects = m_seqRects->total;
346 CvSeqReader reader, reader2;
347 cvStartReadSeq( m_seqRects, &reader );
349 for (i = 0; i < nRects; i++)
351 CvTrackingRect* pRect1 = (CvTrackingRect*)(reader.ptr);
352 cvStartReadSeq( m_seqRects, &reader2 );
353 cvSetSeqReaderPos(&reader2, i + 1);
354 for (j = i + 1; j < nRects; j++)
356 CvTrackingRect* pRect2 = (CvTrackingRect*)(reader2.ptr);
357 if (abs(pRect1->ptCenter.y - pRect2->ptCenter.y) < d &&
358 abs(pRect1->r.height - pRect2->r.height) < d)
361 rNew.iColor = (pRect1->iColor + pRect2->iColor + 1) / 2;
362 rNew.r.x = min(pRect1->r.x, pRect2->r.x);
363 rNew.r.y = min(pRect1->r.y, pRect2->r.y);
364 rNew.r.width = max(pRect1->r.x + pRect1->r.width, pRect2->r.x + pRect2->r.width) - rNew.r.x;
365 rNew.r.height = min(pRect1->r.y + pRect1->r.height, pRect2->r.y + pRect2->r.height) - rNew.r.y;
366 if (rNew.r != pRect1->r && rNew.r != pRect2->r)
368 rNew.ptCenter = Center(rNew.r);
369 cvSeqPush(m_seqRects, &rNew);
372 CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader2 );
374 CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader );
376 // delete equal rects
377 for (i = 0; i < m_seqRects->total; i++)
379 CvTrackingRect* pRect1 = (CvTrackingRect*)cvGetSeqElem(m_seqRects, i);
381 for (j = j_begin; j < m_seqRects->total;)
383 CvTrackingRect* pRect2 = (CvTrackingRect*)cvGetSeqElem(m_seqRects, j);
384 if (pRect1->r == pRect2->r)
385 cvSeqRemove(m_seqRects, j);
391 }//void CvFaceElement::MergeRects(int d)
393 void CvFaceElement::Energy()
395 CvSeqReader reader, reader2;
396 cvStartReadSeq( m_seqRects, &reader );
397 for (int i = 0; i < m_seqRects->total; i++)
399 CvTrackingRect* pRect = (CvTrackingRect*)(reader.ptr);
400 // outside and inside rects
401 cvStartReadSeq( m_seqRects, &reader2 );
402 for (int j = 0; j < m_seqRects->total; j++)
404 CvTrackingRect* pRect2 = (CvTrackingRect*)(reader2.ptr);
407 if (RectInRect(pRect2->r, pRect->r))
408 pRect->nRectsInThis ++;
409 else if (pRect2->r.y + pRect2->r.height <= pRect->r.y)
410 pRect->nRectsOnTop ++;
411 else if (pRect2->r.y >= pRect->r.y + pRect->r.height)
412 pRect->nRectsOnBottom ++;
413 else if (pRect2->r.x + pRect2->r.width <= pRect->r.x)
414 pRect->nRectsOnLeft ++;
415 else if (pRect2->r.x >= pRect->r.x + pRect->r.width)
416 pRect->nRectsOnRight ++;
418 CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader2 );
421 pRect->Energy(m_trPrev);
422 CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader );
424 }//void CvFaceElement::Energy()
426 CV_IMPL CvFaceTracker*
427 cvInitFaceTracker(CvFaceTracker* pFaceTracker, const IplImage* imgGray, CvRect* pRects, int nRects)
429 _ASSERT(NULL != imgGray);
430 _ASSERT(NULL != pRects);
431 _ASSERT(nRects >= NUM_FACE_ELEMENTS);
432 if ((NULL == imgGray) ||
434 (nRects < NUM_FACE_ELEMENTS))
437 int new_face = FALSE;
438 CvFaceTracker* pFace = pFaceTracker;
441 pFace = new CvFaceTracker;
446 pFace->Init(pRects, (IplImage*)imgGray);
448 }//CvFaceTracker* InitFaceTracker(IplImage* imgGray, CvRect* pRects, int nRects)
451 cvReleaseFaceTracker(CvFaceTracker** ppFaceTracker)
453 if (NULL == *ppFaceTracker)
455 delete *ppFaceTracker;
456 *ppFaceTracker = NULL;
457 }//void ReleaseFaceTracker(CvFaceTracker** ppFaceTracker)
461 cvTrackFace(CvFaceTracker* pFaceTracker, IplImage* imgGray, CvRect* pRects, int nRects, CvPoint* ptRotate, double* dbAngleRotate)
463 _ASSERT(NULL != pFaceTracker);
464 _ASSERT(NULL != imgGray);
465 _ASSERT(NULL != pRects && nRects >= NUM_FACE_ELEMENTS);
466 if ((NULL == pFaceTracker) ||
469 pFaceTracker->InitNextImage(imgGray);
470 *ptRotate = pFaceTracker->ptRotate;
471 *dbAngleRotate = pFaceTracker->dbRotateAngle;
474 double dx = pFaceTracker->face[LEYE].ptCenter.x - pFaceTracker->face[REYE].ptCenter.x;
475 double dy = pFaceTracker->face[LEYE].ptCenter.y - pFaceTracker->face[REYE].ptCenter.y;
476 double d_eyes = sqrt(dx*dx + dy*dy);
477 int d = cvRound(0.25 * d_eyes);
483 CvFaceElement big_face[NUM_FACE_ELEMENTS];
486 for (elem = 0; elem < NUM_FACE_ELEMENTS; elem++)
488 CvRect r = pFaceTracker->face[elem].r;
492 r.x -= (4*d - r.width) / 2;
493 r.width += 4*d - r.width;
497 r.y -= (3*d - r.height) / 2;
498 r.height += 3*d - r.height;
504 if (r.x + r.width > pFaceTracker->imgGray->width - 2)
505 r.width = pFaceTracker->imgGray->width - 2 - r.x;
506 if (r.y + r.height > pFaceTracker->imgGray->height - 2)
507 r.height = pFaceTracker->imgGray->height - 2 - r.y;
508 if (!big_face[elem].Init(r, pFaceTracker->face[elem], pFaceTracker->mstgContours))
512 for (elem = 0; elem < NUM_FACE_ELEMENTS; elem++)
513 big_face[elem].FindRects(pFaceTracker->imgGray, pFaceTracker->imgThresh, 32, dMinSize);
515 CvTrackingRect new_face[NUM_FACE_ELEMENTS];
517 int found = ChoiceTrackingFace3(pFaceTracker, nElements, big_face, new_face, new_energy);
523 if (new_energy > 100000 && -1 != pFaceTracker->iTrackingFaceType)
525 else if (new_energy > 150000)
528 for (int el = 0; el < NUM_FACE_ELEMENTS; el++)
530 if (big_face[el].m_seqRects->total > 16 || (big_face[el].m_seqRects->total > 8 && new_face[el].iEnergy < 100))
543 if (-1 != pFaceTracker->iTrackingFaceType)
559 if (-1 != pFaceTracker->iTrackingFaceType)
560 noel = pFaceTracker->iTrackingFaceType;
561 int found2 = ChoiceTrackingFace2(pFaceTracker, nElements, big_face, new_face, new_energy, noel);
562 if (found2 && new_energy < 100000)
564 pFaceTracker->iTrackingFaceType = noel;
576 // angle by mouth & eyes
577 double vx_prev = double(pFaceTracker->face[LEYE].ptCenter.x + pFaceTracker->face[REYE].ptCenter.x) / 2.0 - pFaceTracker->face[MOUTH].ptCenter.x;
578 double vy_prev = double(pFaceTracker->face[LEYE].ptCenter.y + pFaceTracker->face[REYE].ptCenter.y) / 2.0 - pFaceTracker->face[MOUTH].ptCenter.y;
579 double vx_prev1 = vx_prev * cos(pFaceTracker->dbRotateDelta) - vy_prev * sin(pFaceTracker->dbRotateDelta);
580 double vy_prev1 = vx_prev * sin(pFaceTracker->dbRotateDelta) + vy_prev * cos(pFaceTracker->dbRotateDelta);
583 for (elem = 0; elem < NUM_FACE_ELEMENTS; elem++)
584 pFaceTracker->face[elem] = new_face[elem];
585 double vx = double(pFaceTracker->face[LEYE].ptCenter.x + pFaceTracker->face[REYE].ptCenter.x) / 2.0 - pFaceTracker->face[MOUTH].ptCenter.x;
586 double vy = double(pFaceTracker->face[LEYE].ptCenter.y + pFaceTracker->face[REYE].ptCenter.y) / 2.0 - pFaceTracker->face[MOUTH].ptCenter.y;
587 pFaceTracker->dbRotateDelta = 0;
588 double n1_n2 = (vx * vx + vy * vy) * (vx_prev * vx_prev + vy_prev * vy_prev);
590 pFaceTracker->dbRotateDelta = asin((vx * vy_prev - vx_prev * vy) / sqrt(n1_n2));
591 pFaceTracker->dbRotateAngle -= pFaceTracker->dbRotateDelta;
595 pFaceTracker->dbRotateDelta = 0;
596 pFaceTracker->dbRotateAngle = 0;
598 if ((pFaceTracker->dbRotateAngle >= pi/2 && pFaceTracker->dbRotateAngle > 0) ||
599 (pFaceTracker->dbRotateAngle <= -pi/2 && pFaceTracker->dbRotateAngle < 0))
601 pFaceTracker->dbRotateDelta = 0;
602 pFaceTracker->dbRotateAngle = 0;
607 for (int i = 0; i < NUM_FACE_ELEMENTS && i < nRects; i++)
608 pRects[i] = pFaceTracker->face[i].r;
611 }//int FindFaceTracker(CvFaceTracker* pFaceTracker, IplImage* imgGray, CvRect* pRects, int nRects, CvPoint& ptRotate, double& dbAngleRotate)
613 void ThresholdingParam(IplImage *imgGray, int iNumLayers, int &iMinLevel, int &iMaxLevel, float &step, float& power, int iHistMin /*= HIST_MIN*/)
615 _ASSERT(imgGray != NULL);
616 _ASSERT(imgGray->nChannels == 1);
619 int histImg[256] = {0};
620 uchar* buffImg = (uchar*)imgGray->imageData;
621 CvRect rROI = cvGetImageROI(imgGray);
622 buffImg += rROI.y * imgGray->widthStep + rROI.x;
623 for (j = 0; j < rROI.height; j++)
625 for (i = 0; i < rROI.width; i++)
626 histImg[buffImg[i]] ++;
627 buffImg += imgGray->widthStep;
630 for (i = 0; i < 256; i++)
632 if (histImg[i] > iHistMin)
636 for (i = 255; i >= 0; i--)
638 if (histImg[i] > iHistMin)
642 if (iMaxLevel <= iMinLevel)
650 for (i = iMinLevel; i < (iMinLevel + iMaxLevel) / 2; i++)
652 for (i = (iMinLevel + iMaxLevel) / 2; i < iMaxLevel; i++)
654 power = float(black) / float(2 * white);
656 step = float(iMaxLevel - iMinLevel) / float(iNumLayers);
659 }// void ThresholdingParam(IplImage *imgGray, int iNumLayers, int &iMinLevel, int &iMaxLevel, int &iStep)
661 int ChoiceTrackingFace3(CvFaceTracker* pTF, const int nElements, const CvFaceElement* big_face, CvTrackingRect* face, int& new_energy)
663 CvTrackingRect* curr_face[NUM_FACE_ELEMENTS] = {NULL};
664 CvTrackingRect* new_face[NUM_FACE_ELEMENTS] = {NULL};
665 new_energy = 0x7fffffff;
666 int curr_energy = 0x7fffffff;
669 CvSeqReader reader_m, reader_l, reader_r;
670 cvStartReadSeq( big_face[MOUTH].m_seqRects, &reader_m );
671 for (int i_mouth = 0; i_mouth < big_face[MOUTH].m_seqRects->total && i_mouth < nElements; i_mouth++)
673 curr_face[MOUTH] = (CvTrackingRect*)(reader_m.ptr);
674 cvStartReadSeq( big_face[LEYE].m_seqRects, &reader_l );
675 for (int i_left = 0; i_left < big_face[LEYE].m_seqRects->total && i_left < nElements; i_left++)
677 curr_face[LEYE] = (CvTrackingRect*)(reader_l.ptr);
678 if (curr_face[LEYE]->r.y + curr_face[LEYE]->r.height < curr_face[MOUTH]->r.y)
680 cvStartReadSeq( big_face[REYE].m_seqRects, &reader_r );
681 for (int i_right = 0; i_right < big_face[REYE].m_seqRects->total && i_right < nElements; i_right++)
683 curr_face[REYE] = (CvTrackingRect*)(reader_r.ptr);
684 if (curr_face[REYE]->r.y + curr_face[REYE]->r.height < curr_face[MOUTH]->r.y &&
685 curr_face[REYE]->r.x > curr_face[LEYE]->r.x + curr_face[LEYE]->r.width)
687 curr_energy = GetEnergy(curr_face, pTF->face, pTF->ptTempl, pTF->rTempl);
688 if (curr_energy < new_energy)
690 for (int elem = 0; elem < NUM_FACE_ELEMENTS; elem++)
691 new_face[elem] = curr_face[elem];
692 new_energy = curr_energy;
703 for (int elem = 0; elem < NUM_FACE_ELEMENTS; elem++)
704 face[elem] = *(new_face[elem]);
707 } // int ChoiceTrackingFace3(const CvTrackingRect* tr_face, CvTrackingRect* new_face, int& new_energy)
709 int ChoiceTrackingFace2(CvFaceTracker* pTF, const int nElements, const CvFaceElement* big_face, CvTrackingRect* face, int& new_energy, int noel)
711 int element[NUM_FACE_ELEMENTS];
712 for (int i = 0, elem = 0; i < NUM_FACE_ELEMENTS; i++)
722 CvTrackingRect* curr_face[NUM_FACE_ELEMENTS] = {NULL};
723 CvTrackingRect* new_face[NUM_FACE_ELEMENTS] = {NULL};
724 new_energy = 0x7fffffff;
725 int curr_energy = 0x7fffffff;
728 CvSeqReader reader0, reader1;
729 cvStartReadSeq( big_face[element[0]].m_seqRects, &reader0 );
730 for (int i0 = 0; i0 < big_face[element[0]].m_seqRects->total && i0 < nElements; i0++)
732 curr_face[element[0]] = (CvTrackingRect*)(reader0.ptr);
733 cvStartReadSeq( big_face[element[1]].m_seqRects, &reader1 );
734 for (int i1 = 0; i1 < big_face[element[1]].m_seqRects->total && i1 < nElements; i1++)
736 curr_face[element[1]] = (CvTrackingRect*)(reader1.ptr);
737 curr_energy = GetEnergy2(curr_face, pTF->face, pTF->ptTempl, pTF->rTempl, element);
738 if (curr_energy < new_energy)
740 for (int elem = 0; elem < NUM_FACE_ELEMENTS; elem++)
741 new_face[elem] = curr_face[elem];
742 new_energy = curr_energy;
750 face[element[0]] = *(new_face[element[0]]);
751 face[element[1]] = *(new_face[element[1]]);
752 // 3 element find by template
753 CvPoint templ_v01 = {pTF->ptTempl[element[1]].x - pTF->ptTempl[element[0]].x, pTF->ptTempl[element[1]].y - pTF->ptTempl[element[0]].y};
754 CvPoint templ_v02 = {pTF->ptTempl[element[2]].x - pTF->ptTempl[element[0]].x, pTF->ptTempl[element[2]].y - pTF->ptTempl[element[0]].y};
755 CvPoint prev_v01 = {pTF->face[element[1]].ptCenter.x - pTF->face[element[0]].ptCenter.x, pTF->face[element[1]].ptCenter.y - pTF->face[element[0]].ptCenter.y};
756 CvPoint prev_v02 = {pTF->face[element[2]].ptCenter.x - pTF->face[element[0]].ptCenter.x, pTF->face[element[2]].ptCenter.y - pTF->face[element[0]].ptCenter.y};
757 CvPoint new_v01 = {new_face[element[1]]->ptCenter.x - new_face[element[0]]->ptCenter.x, new_face[element[1]]->ptCenter.y - new_face[element[0]]->ptCenter.y};
758 double templ_d01 = sqrt((double)templ_v01.x*templ_v01.x + templ_v01.y*templ_v01.y);
759 double templ_d02 = sqrt((double)templ_v02.x*templ_v02.x + templ_v02.y*templ_v02.y);
760 double prev_d01 = sqrt((double)prev_v01.x*prev_v01.x + prev_v01.y*prev_v01.y);
761 double prev_d02 = sqrt((double)prev_v02.x*prev_v02.x + prev_v02.y*prev_v02.y);
762 double new_d01 = sqrt((double)new_v01.x*new_v01.x + new_v01.y*new_v01.y);
763 double scale = templ_d01 / new_d01;
764 double new_d02 = templ_d02 / scale;
765 double sin_a = double(prev_v01.x * prev_v02.y - prev_v01.y * prev_v02.x) / (prev_d01 * prev_d02);
766 double cos_a = cos(asin(sin_a));
767 double x = double(new_v01.x) * cos_a - double(new_v01.y) * sin_a;
768 double y = double(new_v01.x) * sin_a + double(new_v01.y) * cos_a;
769 x = x * new_d02 / new_d01;
770 y = y * new_d02 / new_d01;
771 CvPoint new_v02 = {int(x + 0.5), int(y + 0.5)};
772 face[element[2]].iColor = 0;
773 face[element[2]].iEnergy = 0;
774 face[element[2]].nRectsInThis = 0;
775 face[element[2]].nRectsOnBottom = 0;
776 face[element[2]].nRectsOnLeft = 0;
777 face[element[2]].nRectsOnRight = 0;
778 face[element[2]].nRectsOnTop = 0;
779 face[element[2]].ptCenter.x = new_v02.x + new_face[element[0]]->ptCenter.x;
780 face[element[2]].ptCenter.y = new_v02.y + new_face[element[0]]->ptCenter.y;
781 face[element[2]].r.width = int(double(pTF->rTempl[element[2]].width) / (scale) + 0.5);
782 face[element[2]].r.height = int(double(pTF->rTempl[element[2]].height) / (scale) + 0.5);
783 face[element[2]].r.x = face[element[2]].ptCenter.x - (face[element[2]].r.width + 1) / 2;
784 face[element[2]].r.y = face[element[2]].ptCenter.y - (face[element[2]].r.height + 1) / 2;
785 _ASSERT(face[LEYE].r.x + face[LEYE].r.width <= face[REYE].r.x);
788 } // int ChoiceTrackingFace3(const CvTrackingRect* tr_face, CvTrackingRect* new_face, int& new_energy)
790 inline int GetEnergy(CvTrackingRect** ppNew, const CvTrackingRect* pPrev, CvPoint* ptTempl, CvRect* rTempl)
793 CvPoint ptNew[NUM_FACE_ELEMENTS];
794 CvPoint ptPrev[NUM_FACE_ELEMENTS];
795 for (int i = 0; i < NUM_FACE_ELEMENTS; i++)
797 ptNew[i] = ppNew[i]->ptCenter;
798 ptPrev[i] = pPrev[i].ptCenter;
799 energy += ppNew[i]->iEnergy - 2 * ppNew[i]->nRectsInThis;
801 double dx = 0, dy = 0, scale = 1, rotate = 0;
802 double e_templ = CalculateTransformationLMS3(ptTempl, ptNew, &scale, &rotate, &dx, &dy);
803 double e_prev = CalculateTransformationLMS3_0(ptPrev, ptNew);
804 double w_eye = double(ppNew[LEYE]->r.width + ppNew[REYE]->r.width) * scale / 2.0;
805 double h_eye = double(ppNew[LEYE]->r.height + ppNew[REYE]->r.height) * scale / 2.0;
806 double w_mouth = double(ppNew[MOUTH]->r.width) * scale;
807 double h_mouth = double(ppNew[MOUTH]->r.height) * scale;
809 int(512.0 * (e_prev + 16.0 * e_templ)) +
810 4 * pow2(ppNew[LEYE]->r.width - ppNew[REYE]->r.width) +
811 4 * pow2(ppNew[LEYE]->r.height - ppNew[REYE]->r.height) +
812 4 * (int)pow(w_eye - double(rTempl[LEYE].width + rTempl[REYE].width) / 2.0, 2) +
813 2 * (int)pow(h_eye - double(rTempl[LEYE].height + rTempl[REYE].height) / 2.0, 2) +
814 1 * (int)pow(w_mouth - double(rTempl[MOUTH].width), 2) +
815 1 * (int)pow(h_mouth - double(rTempl[MOUTH].height), 2) +
820 inline int GetEnergy2(CvTrackingRect** ppNew, const CvTrackingRect* pPrev, CvPoint* ptTempl, CvRect* rTempl, int* element)
822 CvPoint new_v = {ppNew[element[0]]->ptCenter.x - ppNew[element[1]]->ptCenter.x,
823 ppNew[element[0]]->ptCenter.y - ppNew[element[1]]->ptCenter.y};
824 CvPoint prev_v = {pPrev[element[0]].ptCenter.x - pPrev[element[1]].ptCenter.x,
825 pPrev[element[0]].ptCenter.y - pPrev[element[1]].ptCenter.y};
826 double new_d = sqrt((double)new_v.x*new_v.x + new_v.y*new_v.y);
827 double prev_d = sqrt((double)prev_v.x*prev_v.x + prev_v.y*prev_v.y);
828 double dx = ptTempl[element[0]].x - ptTempl[element[1]].x;
829 double dy = ptTempl[element[0]].y - ptTempl[element[1]].y;
830 double templ_d = sqrt(dx*dx + dy*dy);
831 double scale_templ = new_d / templ_d;
832 double w0 = (double)ppNew[element[0]]->r.width * scale_templ;
833 double h0 = (double)ppNew[element[0]]->r.height * scale_templ;
834 double w1 = (double)ppNew[element[1]]->r.width * scale_templ;
835 double h1 = (double)ppNew[element[1]]->r.height * scale_templ;
837 int energy = ppNew[element[0]]->iEnergy + ppNew[element[1]]->iEnergy +
838 - 2 * (ppNew[element[0]]->nRectsInThis - ppNew[element[1]]->nRectsInThis) +
839 (int)pow(w0 - (double)rTempl[element[0]].width, 2) +
840 (int)pow(h0 - (double)rTempl[element[0]].height, 2) +
841 (int)pow(w1 - (double)rTempl[element[1]].width, 2) +
842 (int)pow(h1 - (double)rTempl[element[1]].height, 2) +
843 (int)pow(new_d - prev_d, 2) +
849 inline double CalculateTransformationLMS3( CvPoint* pTemplPoints,
851 double* pdbAverageScale,
852 double* pdbAverageRotate,
853 double* pdbAverageShiftX,
854 double* pdbAverageShiftY )
857 double dbAverageScale = 1;
858 double dbAverageRotate = 0;
859 double dbAverageShiftX = 0;
860 double dbAverageShiftY = 0;
863 _ASSERT( NULL != pTemplPoints);
864 _ASSERT( NULL != pSrcPoints);
866 double dbXt = double(pTemplPoints[0].x + pTemplPoints[1].x + pTemplPoints[2].x) / 3.0;
867 double dbYt = double(pTemplPoints[0].y + pTemplPoints[1].y + pTemplPoints[2].y ) / 3.0;
868 double dbXs = double(pSrcPoints[0].x + pSrcPoints[1].x + pSrcPoints[2].x) / 3.0;
869 double dbYs = double(pSrcPoints[0].y + pSrcPoints[1].y + pSrcPoints[2].y) / 3.0;
871 double dbXtXt = double(pow2(pTemplPoints[0].x) + pow2(pTemplPoints[1].x) + pow2(pTemplPoints[2].x)) / 3.0;
872 double dbYtYt = double(pow2(pTemplPoints[0].y) + pow2(pTemplPoints[1].y) + pow2(pTemplPoints[2].y)) / 3.0;
874 double dbXsXs = double(pow2(pSrcPoints[0].x) + pow2(pSrcPoints[1].x) + pow2(pSrcPoints[2].x)) / 3.0;
875 double dbYsYs = double(pow2(pSrcPoints[0].y) + pow2(pSrcPoints[1].y) + pow2(pSrcPoints[2].y)) / 3.0;
877 double dbXtXs = double(pTemplPoints[0].x * pSrcPoints[0].x +
878 pTemplPoints[1].x * pSrcPoints[1].x +
879 pTemplPoints[2].x * pSrcPoints[2].x) / 3.0;
880 double dbYtYs = double(pTemplPoints[0].y * pSrcPoints[0].y +
881 pTemplPoints[1].y * pSrcPoints[1].y +
882 pTemplPoints[2].y * pSrcPoints[2].y) / 3.0;
884 double dbXtYs = double(pTemplPoints[0].x * pSrcPoints[0].y +
885 pTemplPoints[1].x * pSrcPoints[1].y +
886 pTemplPoints[2].x * pSrcPoints[2].y) / 3.0;
887 double dbYtXs = double(pTemplPoints[0].y * pSrcPoints[0].x +
888 pTemplPoints[1].y * pSrcPoints[1].x +
889 pTemplPoints[2].y * pSrcPoints[2].x ) / 3.0;
891 dbXtXt -= dbXt * dbXt;
892 dbYtYt -= dbYt * dbYt;
894 dbXsXs -= dbXs * dbXs;
895 dbYsYs -= dbYs * dbYs;
897 dbXtXs -= dbXt * dbXs;
898 dbYtYs -= dbYt * dbYs;
900 dbXtYs -= dbXt * dbYs;
901 dbYtXs -= dbYt * dbXs;
903 dbAverageRotate = atan2( dbXtYs - dbYtXs, dbXtXs + dbYtYs );
905 double cosR = cos(dbAverageRotate);
906 double sinR = sin(dbAverageRotate);
907 double del = dbXsXs + dbYsYs;
910 dbAverageScale = (double(dbXtXs + dbYtYs) * cosR + double(dbXtYs - dbYtXs) * sinR) / del;
911 dbLMS = dbXtXt + dbYtYt - ((double)pow(dbXtXs + dbYtYs,2) + (double)pow(dbXtYs - dbYtXs,2)) / del;
914 dbAverageShiftX = double(dbXt) - dbAverageScale * (double(dbXs) * cosR + double(dbYs) * sinR);
915 dbAverageShiftY = double(dbYt) - dbAverageScale * (double(dbYs) * cosR - double(dbXs) * sinR);
917 if( pdbAverageScale != NULL ) *pdbAverageScale = dbAverageScale;
918 if( pdbAverageRotate != NULL ) *pdbAverageRotate = dbAverageRotate;
919 if( pdbAverageShiftX != NULL ) *pdbAverageShiftX = dbAverageShiftX;
920 if( pdbAverageShiftY != NULL ) *pdbAverageShiftY = dbAverageShiftY;
926 inline double CalculateTransformationLMS3_0( CvPoint* pTemplPoints, CvPoint* pSrcPoints)
930 _ASSERT( NULL != pTemplPoints);
931 _ASSERT( NULL != pSrcPoints);
933 double dbXt = double(pTemplPoints[0].x + pTemplPoints[1].x + pTemplPoints[2].x) / 3.0;
934 double dbYt = double(pTemplPoints[0].y + pTemplPoints[1].y + pTemplPoints[2].y ) / 3.0;
935 double dbXs = double(pSrcPoints[0].x + pSrcPoints[1].x + pSrcPoints[2].x) / 3.0;
936 double dbYs = double(pSrcPoints[0].y + pSrcPoints[1].y + pSrcPoints[2].y) / 3.0;
938 double dbXtXt = double(pow2(pTemplPoints[0].x) + pow2(pTemplPoints[1].x) + pow2(pTemplPoints[2].x)) / 3.0;
939 double dbYtYt = double(pow2(pTemplPoints[0].y) + pow2(pTemplPoints[1].y) + pow2(pTemplPoints[2].y)) / 3.0;
941 double dbXsXs = double(pow2(pSrcPoints[0].x) + pow2(pSrcPoints[1].x) + pow2(pSrcPoints[2].x)) / 3.0;
942 double dbYsYs = double(pow2(pSrcPoints[0].y) + pow2(pSrcPoints[1].y) + pow2(pSrcPoints[2].y)) / 3.0;
944 double dbXtXs = double(pTemplPoints[0].x * pSrcPoints[0].x +
945 pTemplPoints[1].x * pSrcPoints[1].x +
946 pTemplPoints[2].x * pSrcPoints[2].x) / 3.0;
947 double dbYtYs = double(pTemplPoints[0].y * pSrcPoints[0].y +
948 pTemplPoints[1].y * pSrcPoints[1].y +
949 pTemplPoints[2].y * pSrcPoints[2].y) / 3.0;
951 double dbXtYs = double(pTemplPoints[0].x * pSrcPoints[0].y +
952 pTemplPoints[1].x * pSrcPoints[1].y +
953 pTemplPoints[2].x * pSrcPoints[2].y) / 3.0;
954 double dbYtXs = double(pTemplPoints[0].y * pSrcPoints[0].x +
955 pTemplPoints[1].y * pSrcPoints[1].x +
956 pTemplPoints[2].y * pSrcPoints[2].x ) / 3.0;
958 dbXtXt -= dbXt * dbXt;
959 dbYtYt -= dbYt * dbYt;
961 dbXsXs -= dbXs * dbXs;
962 dbYsYs -= dbYs * dbYs;
964 dbXtXs -= dbXt * dbXs;
965 dbYtYs -= dbYt * dbYs;
967 dbXtYs -= dbXt * dbYs;
968 dbYtXs -= dbYt * dbXs;
970 double del = dbXsXs + dbYsYs;
972 dbLMS = dbXtXt + dbYtYt - ((double)pow(dbXtXs + dbYtYs,2) + (double)pow(dbXtYs - dbYtXs,2)) / del;