face.cpp 11 KB
Newer Older
wester committed
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
///////////////////////////////////////////////
//// Created by Khudyakov V.A. bober@gorodok.net
//////////////////////////////////////////////

#include "precomp.hpp"
#include "_facedetection.h"

Face::Face(FaceTemplate * lpFaceTemplate)
{
    //init number of face elements;
    m_lFaceFeaturesNumber = lpFaceTemplate->GetCount();

    //init array of numbers of foundet face elements of each type
    m_lplFaceFeaturesCount = new long[m_lFaceFeaturesNumber];
    memset(m_lplFaceFeaturesCount,0,m_lFaceFeaturesNumber*sizeof(long));

    //init array of ideal face features
    m_lpIdealFace = new FaceFeature[m_lFaceFeaturesNumber];

    //init array of founded features
    m_lppFoundedFaceFeatures = new FaceFeature*[m_lFaceFeaturesNumber];

    for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
    {
        m_lppFoundedFaceFeatures[i] = (new FaceFeature[3*MAX_LAYERS]);
    }

    //set start weight 0
    m_dWeight = 0;

}//Face::Face(FaceTemplate * lpFaceTemplate)

Face::~Face()
{
    for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
    {
        delete [] (m_lppFoundedFaceFeatures[i]);
    }
    delete [] m_lppFoundedFaceFeatures;


    delete [] m_lplFaceFeaturesCount;
    delete [] m_lpIdealFace;

}//Face::~Face()


#define UP_SCALE    1
#define DOWN_SCALE  2


////////////
//class RFace(rect based face)
////////////
RFace::RFace(FaceTemplate * lpFaceTemplate):Face(lpFaceTemplate)
{
    //init ideal face
    FaceFeature * lpTmp = lpFaceTemplate->GetFeatures();

    for (int j = 0;j < m_lFaceFeaturesNumber;j ++)
    {
        CvRect * lpTmpRect = NULL;
        lpTmpRect = new CvRect;
        *lpTmpRect = *(CvRect*)lpTmp[j].GetContour();

        m_lpIdealFace[j].SetContour( lpTmpRect );
        m_lpIdealFace[j].SetWeight( lpTmp[j].GetWeight() );
        m_lpIdealFace[j].SetFeature( lpTmp[j].isFaceFeature() );

    }

    m_bIsGenerated = false;
}//RFace::RFace(FaceTemplate * lpFaceTemplate)

RFace::~RFace()
{

}//RFace::~RFace()

inline bool RFace::isPointInRect(CvPoint p,CvRect rect)
{
    if ( (p.x >= rect.x) && (p.y >= rect.y) && (p.x <= rect.x + rect.width) && (p.y <= rect.y + rect.height) )
        return true;

    return false;
}//inline bool RFace::isPointInRect(CvPoint,CvRect rect)

double RFace::GetWeight()
{
    return m_dWeight;
}//double RFace::GetWeight()


bool RFace::CheckElem(void * lpCandidat,void * lpIdeal)
{

    CvRect IdealRect = *(CvRect*)lpIdeal;
    CvRect Rect = *(CvRect*)lpCandidat;

    if (Rect.height > Rect.width)
        return false;

    long SizeIdeal = IdealRect.width*IdealRect.height;
    long Size = Rect.width*Rect.height;

    if ( (Size > SizeIdeal) || ( Size < (SizeIdeal/5) ) )
        return false;

//  CvRect UpRect;
//  CvRect DownRect;
//  ResizeRect(IdealRect,&UpRect,UP_SCALE,7);
//  ResizeRect(IdealRect,&DownRect,DOWN_SCALE,7);

    long x = Rect.x + cvRound(Rect.width/2);
    long y = Rect.y + cvRound(Rect.height/2);

    if ( isPointInRect(cvPoint((int)x,(int)y),IdealRect) )
        return true;

//  if ( isPointInRect(cvPoint(Rect.x,Rect.y),UpRect) &&
//       isPointInRect(cvPoint(Rect.x + Rect.width,Rect.y + Rect.height),UpRect ) &&
//       isPointInRect(cvPoint(DownRect.x,DownRect.y),Rect) &&
//       isPointInRect(cvPoint(DownRect.x + DownRect.width,DownRect.y + DownRect.height),Rect) )
//      return true;


//  if ( isPointInRect(cvPoint(Rect.x,Rect.y),IdealRect) &&
//       isPointInRect(cvPoint(Rect.x + Rect.width,Rect.y + Rect.height),IdealRect ) )
//      return true;

    return false;
}//inline bool RFace::CheckElem(CvRect rect)



void RFace::CalculateError(FaceData * lpFaceData)
{
    CvRect LeftEyeRect = lpFaceData->LeftEyeRect;
    CvRect RightEyeRect = lpFaceData->RightEyeRect;
    CvRect MouthRect = lpFaceData->MouthRect;

    long LeftSquare = LeftEyeRect.width*LeftEyeRect.height;
    long RightSquare = RightEyeRect.width*RightEyeRect.height;

    long dy = LeftEyeRect.y - RightEyeRect.y;

    long dx1 = LeftEyeRect.x + LeftEyeRect.width/2 - MouthRect.x;
    long dx2 = RightEyeRect.x + RightEyeRect.width/2 - MouthRect.x - MouthRect.width;


    lpFaceData->Error = (double)(LeftSquare - RightSquare)*(double)(LeftSquare - RightSquare)/((double)(LeftSquare + RightSquare)*(LeftSquare + RightSquare)) +
                        (double)(dy*dy)/((double)(LeftEyeRect.height + RightEyeRect.height)*(LeftEyeRect.height + RightEyeRect.height)) +
                        (double)(dx1*dx1)/((double)MouthRect.width*MouthRect.width) +
                        (double)(dx2*dx2)/((double)MouthRect.width*MouthRect.width);

}//void RFace::CalculateError(FaceData * lpFaceData)

#define MAX_ERROR 0xFFFFFFFF

void  RFace::CreateFace(void * lpData)
{
    FaceData Data;
    memset(&Data, 0, sizeof(FaceData));

    double Error = MAX_ERROR;
    double CurError = MAX_ERROR;

    FaceData * lpFaceData = (FaceData*)lpData;

    int im = 0;//mouth was find
    int jl = 0;//left eye was find
    int kr = 0;//right eye was find

    long MouthNumber = 0;
    long LeftEyeNumber = 0;
    long RightEyeNumber = 0;

    for (int i = 0;i < m_lplFaceFeaturesCount[0] + 1;i ++)
    {

        if ( !m_lplFaceFeaturesCount[0] )
            Data.MouthRect = *(CvRect*)m_lpIdealFace[0].GetContour();
        else
        {
            if ( i != m_lplFaceFeaturesCount[0] )
                Data.MouthRect = *(CvRect*)m_lppFoundedFaceFeatures[0][i].GetContour();
            im = 1;
        }


        for (int j = 0;j < m_lplFaceFeaturesCount[1] + 1;j ++)
        {

            if ( !m_lplFaceFeaturesCount[1] )
                Data.LeftEyeRect = *(CvRect*)m_lpIdealFace[1].GetContour();
            else
            {
                if (j != m_lplFaceFeaturesCount[1] )
                    Data.LeftEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[1][j].GetContour();
                jl = 1;
            }


            for (int k = 0;k < m_lplFaceFeaturesCount[2] + 1;k ++)
            {

                if ( !m_lplFaceFeaturesCount[2] )
                    Data.RightEyeRect = *(CvRect*)m_lpIdealFace[2].GetContour();
                else
                {
                    if (k != m_lplFaceFeaturesCount[2] )
                        Data.RightEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[2][k].GetContour();
                    kr = 1;
                }

                CalculateError(&Data);

                if ( (im + jl + kr) )
                {
                    Error = Data.Error/(im + jl + kr);
                }else
                    Error = MAX_ERROR;

                if (CurError > Error)
                {
                    CurError = Error;
                    MouthNumber = i;
                    LeftEyeNumber = j;
                    RightEyeNumber = k;
                }

            }


        }

    }

    if ( m_lplFaceFeaturesCount[0] )
        lpFaceData->MouthRect = *(CvRect*)m_lppFoundedFaceFeatures[0][MouthNumber].GetContour();
    else
        lpFaceData->MouthRect = *(CvRect*)m_lpIdealFace[0].GetContour();

    if ( m_lplFaceFeaturesCount[1] )
        lpFaceData->LeftEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[1][LeftEyeNumber].GetContour();
    else
        lpFaceData->LeftEyeRect = *(CvRect*)m_lpIdealFace[1].GetContour();

    if ( m_lplFaceFeaturesCount[2] )
        lpFaceData->RightEyeRect = *(CvRect*)m_lppFoundedFaceFeatures[2][RightEyeNumber].GetContour();
    else
        lpFaceData->RightEyeRect = *(CvRect*)m_lpIdealFace[2].GetContour();

    lpFaceData->Error = CurError;

}//void * RFace::CreateFace()

void RFace::Show(IplImage * Image)
{
    for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
    {
        if (m_lplFaceFeaturesCount[i])
        {
            for (int j = 0;j < m_lplFaceFeaturesCount[i];j ++)
            {
                CvRect rect = *(CvRect*)m_lppFoundedFaceFeatures[i][j].GetContour();
                CvPoint p1 = cvPoint(rect.x,rect.y);
                CvPoint p2 = cvPoint(rect.x + rect.width,rect.y + rect.height);
                cvRectangle(Image,p1,p2,CV_RGB(255,0,0),1);
            }
        }
    }

}//void RFace::Show(IplImage * Image)

void RFace::ShowIdeal(IplImage* Image)
{
    for (int i = 0;i < m_lFaceFeaturesNumber;i ++)
    {
        CvRect Rect = *(CvRect*)m_lpIdealFace[i].GetContour();
        CvPoint p1 = cvPoint(Rect.x,Rect.y);
        CvPoint p2 = cvPoint(Rect.x + Rect.width,Rect.y + Rect.height);
        cvRectangle(Image,p1,p2,CV_RGB(0,0,255),1);
    }
}//void RFace::ShowIdeal(IplImage* Image)


inline void RFace::ResizeRect(CvRect Rect,CvRect * lpRect,long lDir,long lD)
{
    if (lDir == UP_SCALE)
    {
        lpRect->x = Rect.x - (int)lD;
        lpRect->y = Rect.y - (int)lD;
        lpRect->width = Rect.width + (int)(2*lD);
        lpRect->height = Rect.height + (int)(2*lD);
    }
    if (lDir == DOWN_SCALE)
    {
        lpRect->x = Rect.x + (int)lD;
        lpRect->y = Rect.y + (int)lD;
        if (Rect.width - 2*lD >= 0)
        {
            lpRect->width = Rect.width - (int)(2*lD);
        }else
            lpRect->width = 0;

        if (Rect.height - 2*lD >= 0)
        {
            lpRect->height = Rect.height - (int)(2*lD);
        }else
            lpRect->height = 0;
    }

}// inline void RFace::ResizeRect(CvRect * lpRect,long lDir,long lD)