eyedet.cpp 26.5 KB
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
// eyedet.cpp: interface to OpenCV eye and mouth detectors
//
// Copyright (C) 2005-2013, Stephen Milborrow

#include "stasm.h"

namespace stasm
{
//static cv::CascadeClassifier leye_det_g;  // left eye detector
//static cv::CascadeClassifier reye_det_g;  // right eye detector
//static cv::CascadeClassifier mouth_det_g; // mouth detector

//-----------------------------------------------------------------------------

// Return the region of the face we search for the left or right eye.
// Return rect of width=0 if eye must not be searched for (outer eyes in side views).
// We reduce false positives and save time by searching in only part of the face.
// The entire eye box must fall in this region, not just the center of the eye.
// The magic numbers below were found empirically to give good
// results in informal tests.  They reduce the number of false positives
// in the forehead, eyebrows, nostrils, and mouth.

static Rect EyeSearchRect(
    EYAW        eyaw,         // in
    const Rect& facerect,     // in
    const bool  is_right_eye) // in: true for right eye, false for left eye
{
    Rect rect = facerect;
    int width = facerect.width;
    switch (eyaw)
    {
        case EYAW00:                        // frontal model
            if (is_right_eye)
                rect.x += width / 3; // don't search left third of face
            rect.width -= width / 3; // or right third
            rect.height = cvRound(.6 * facerect.height); // don't search lower part of face
            break;
        case EYAW_22:                       // left facing three-quarter model
            if (is_right_eye)               // inner eye
            {
                rect.x += cvRound(.4 * width);
                rect.width = cvRound(.5 * width);
            }
            else                            // outer eye
            {
                rect.x += cvRound(.1 * width);
                rect.width = cvRound(.5 * width);
            }
            rect.height = cvRound(.5 * facerect.height);
            break;
        case EYAW22:                        // right facing three-quarter model
            if (is_right_eye)               // outer eye
            {
                rect.x += cvRound(.4 * width);
                rect.width = cvRound(.5 * width);
            }
            else                            // inner eye
            {
                rect.x += cvRound(.1 * width);
                rect.width = cvRound(.5 * width);
            }
            rect.height = cvRound(.5 * facerect.height);
            break;
        case EYAW_45:                       // left facing three-quarter model
            if (is_right_eye)               // inner eye
            {
                rect.x += cvRound(.4 * width);
                rect.width = cvRound(.5 * width);
                rect.height = cvRound(.5 * facerect.height);
            }
            else                            // outer eye
                rect.width = rect.height = 0;
            break;
        case EYAW45:                        // right facing three-quarter model
            if (is_right_eye)               // outer eye
                rect.width = rect.height = 0;
            else                            // inner eye
            {
                rect.x += cvRound(.1 * width);
                rect.width = cvRound(.5 * width);
                rect.height = cvRound(.5 * facerect.height);
            }
            break;
        default:
            Err("EyeSearchRect: Invalid eyaw %d", eyaw);
            break;
    }
    rect.width  = MAX(0, rect.width);
    rect.height = MAX(0, rect.height);
    return rect;
}

// Get adjustment for position of mouth, based on model type and eye angle.

static void MouthRectShift(
    int&            ixshift,         // out
    int&            iyshift,         // out
    EYAW            eyaw,            // in
    int             facerect_width,  // in
    int             facerect_height, // in
    int             ileft_best,      // in
    int             iright_best,     // in
    const vec_Rect& leyes,           // in
    const vec_Rect& reyes)           // in
{
    double xshift = 0, yshift = 0;
    switch (eyaw)
    {
        case EYAW00: // frontal model
            break;
        case EYAW_45: // left facing three-quarter model
            xshift -= .04 * facerect_width;
            break;
        case EYAW_22: // left facing three-quarter model
            xshift -= .03 * facerect_width;
            break;
        case EYAW22: // right facing three-quarter model
            xshift += .03 * facerect_width;
            break;
        case EYAW45: // right facing three-quarter model
            xshift += .04 * facerect_width;
            break;
        default:
            Err("GeMouthRect: Invalid eyaw %d", eyaw);
            break;
    }

    if (ileft_best != -1 && iright_best != -1)   // got both eyes?
    {
        // get center of eye boxes to get eye angle
        const int xleft  = leyes[ileft_best].x  + leyes[ileft_best].width/2;
        const int yleft  = leyes[ileft_best].y  + leyes[ileft_best].height/2;
        const int xright = reyes[iright_best].x + reyes[iright_best].width/2;
        const int yright = reyes[iright_best].y + reyes[iright_best].height/2;
        double theta = -atan2(double(yright - yleft), double(xright - xleft));
        // move the mouth in the direction of rotation
        xshift += .3 * facerect_height * tan(theta);
        // as the face rotates, the mouth moves up the page
        yshift -= .1 * facerect_height * ABS(tan(theta));
    }
    ixshift = cvRound(xshift);
    iyshift = cvRound(yshift);
}

static Rect MouthRect(           // will search for mouth in this rectangle
    const Rect&     facerect,    // in
    EYAW            eyaw,        // in
    int             ileft_best,  // in: index of best left eye, -1 if none
    int             iright_best, // in: index of best right eye, -1 if none
    const vec_Rect& leyes,       // in: left eyes found by eye detector
    const vec_Rect& reyes)       // in: right eyes found by eye detector
{
    Rect rect = facerect;

    int ixshift, iyshift;
    MouthRectShift(ixshift, iyshift,
                   eyaw, facerect.width, facerect.height,
                   ileft_best, iright_best, leyes, reyes);

    rect.x += cvRound(.2  * facerect.width) + ixshift;

    rect.width = MAX(1, cvRound(.6  * facerect.width));

    switch (eyaw)
    {
        case EYAW00: // frontal model
            rect.y += cvRound(.64 * facerect.height);
            break;
        case EYAW_45: // left facing three-quarter model
            rect.y += cvRound(.55 * facerect.height);
            break;
        case EYAW_22: // left facing three-quarter model
            rect.y += cvRound(.55 * facerect.height);
            break;
        case EYAW22: // right facing three-quarter model
            rect.y += cvRound(.55 * facerect.height);
            break;
        case EYAW45: // right facing three-quarter model
            rect.y += cvRound(.55 * facerect.height);
            break;
        default:
            Err("MouthRect: Invalid eyaw %d", eyaw);
            break;
    }
    rect.y += iyshift;
    rect.height = cvRound(.42 * facerect.height);
    rect.width  = MAX(0, rect.width);
    rect.height = MAX(0, rect.height);
    return rect;
}

bool NeedMouth(
    const vec_Mod& mods) // in: the ASM model(s)
{
    for (int imod = 0; imod < NSIZE(mods); imod++)
        if (mods[imod]->Estart_() == ESTART_EYE_AND_MOUTH)
            return true;
    return false;
}

// Possibly open OpenCV eye detectors and mouth detector.  We say "possibly" because
// the eye and mouth detectors will actually only be opened if any model in mods
// actually needs them.  That is determined by the model's estart field.

void OpenEyeMouthDetectors(
    const vec_Mod& mods,    // in: the ASM models (used to see if we need eyes or mouth)
    const char*    datadir) // in
{
    (void) mods;
    (void) datadir;
    /*
    static bool needeyes = true; // static for efficiency
    // we need the eyes if the estart field of any model
    // is ESTART_EYES or ESTART_EYE_AND_MOUTH
    needeyes = false;
    for (int imod = 0; imod < NSIZE(mods); imod++)
        if (mods[imod]->Estart_() == ESTART_EYES ||
                mods[imod]->Estart_() == ESTART_EYE_AND_MOUTH)
            needeyes = true;
    if (needeyes)
    {
        // I tried all the eye XML files that come with OpenCV 2.1 and found that
        // the files used below give the best results.  The other eye XML files
        // often failed to detect eyes, even with EYE_MIN_NEIGHBORS=1.
        //
        // In the XML filenames, "left" was verified empirically by me to respond
        // to the image left (not the subject's left).  I tested this on the on
        // the MUCT and BioID sets: haarcascade_mcs_lefteye.xml finds more eyes
        // on the viewer's left than it finds on the right (milbo Lusaka Dec 2011).

        //OpenDetector(leye_det_g,  "haarcascade_mcs_lefteye.xml",  datadir);
        //OpenDetector(reye_det_g,  "haarcascade_mcs_righteye.xml", datadir);
    }
    static bool needmouth = true; // static for efficiency
    // we need the eyes if the estart field of any model is ESTART_EYE_AND_MOUTH
    needmouth = false;
    for (int imod = 0; imod < NSIZE(mods); imod++)
        if (mods[imod]->Estart_() == ESTART_EYE_AND_MOUTH)
            needmouth = true;
    if (needmouth)
        //OpenDetector(mouth_det_g, "haarcascade_mcs_mouth.xml", datadir);*/
}

static void DetectAllEyes(
    vec_Rect&    leyes,    // out
    vec_Rect&    reyes,    // out
    const Image& img,      // in
    EYAW         eyaw,     // in
    const Rect&  facerect, // in
    StasmCascadeClassifier cascade)
{
    // 1.2 is 40ms faster than 1.1 but finds slightly fewer eyes
    static const double EYE_SCALE_FACTOR   = 1.2;
    static const int    EYE_MIN_NEIGHBORS  = 3;
    static const int    EYE_DETECTOR_FLAGS = 0;

    Rect leftrect(EyeSearchRect(eyaw, facerect, false));

    if (leftrect.width)
        leyes = Detect(img, &cascade.leftEyeCascade, &leftrect,
                       EYE_SCALE_FACTOR, EYE_MIN_NEIGHBORS, EYE_DETECTOR_FLAGS,
                       facerect.width / 10);

    Rect rightrect(EyeSearchRect(eyaw, facerect, true));

    if (rightrect.width)
        reyes = Detect(img, &cascade.rightEyeCascade, &rightrect,
                       EYE_SCALE_FACTOR, EYE_MIN_NEIGHBORS, EYE_DETECTOR_FLAGS,
                       facerect.width / 10);
}

static void DetectAllMouths(
    vec_Rect&       mouths,      // out
    const Image&    img,         // in
    EYAW            eyaw,        // in
    const Rect&     facerect,    // in
    int             ileft_best,  // in
    int             iright_best, // in
    const vec_Rect& leyes,       // in
    const vec_Rect& reyes,       // in
    cv::CascadeClassifier cascade)
{
    static const double MOUTH_SCALE_FACTOR   = 1.2; // less false pos with 1.2 than 1.1
    static const int    MOUTH_MIN_NEIGHBORS  = 5;   // less false pos with 5 than 3
    static const int    MOUTH_DETECTOR_FLAGS = 0;

    Rect mouth_rect(MouthRect(facerect,
                              eyaw, ileft_best, iright_best, leyes, reyes));

    mouths =
        Detect(img, &cascade, &mouth_rect,
               MOUTH_SCALE_FACTOR, MOUTH_MIN_NEIGHBORS, MOUTH_DETECTOR_FLAGS,
               facerect.width / 10);
}

// Return the region of the face which the _center_ of an eye must be for
// the eye to be considered valid.  This is a subset of the region we
// search for eyes (as returned by EyeSearchRect, which must be big
// enough to enclose the _entire_ eye box).

static Rect EyeInnerRect(
    EYAW        eyaw,        // in
    const Rect& facerect)    // in
{
    Rect rect = facerect;
    switch (eyaw)
    {
        case EYAW00: // frontal model
            rect.x     += cvRound(.1 * facerect.width);
            rect.width  = cvRound(.8 * facerect.width);
            rect.y     += cvRound(.2 * facerect.height);
            rect.height = cvRound(.28 * facerect.height);
            break;
        case EYAW_45: // left facing three-quarter model
            rect.x     += cvRound(.4 * facerect.width);
            rect.width  = cvRound(.5 * facerect.width);
            rect.y     += cvRound(.20 * facerect.height);
            rect.height = cvRound(.25 * facerect.height);
            break;
        case EYAW_22: // left facing three-quarter model
            rect.x     += cvRound(.1 * facerect.width);
            rect.width  = cvRound(.8 * facerect.width);
            rect.y     += cvRound(.20 * facerect.height);
            rect.height = cvRound(.25 * facerect.height);
            break;
        case EYAW22: // right facing three-quarter model
            rect.x     += cvRound(.1 * facerect.width);
            rect.width  = cvRound(.8 * facerect.width);
            rect.y     += cvRound(.20 * facerect.height);
            rect.height = cvRound(.25 * facerect.height);
            break;
        case EYAW45: // right facing three-quarter model
            rect.x     += cvRound(.1 * facerect.width);
            rect.width  = cvRound(.5 * facerect.width);
            rect.y     += cvRound(.20 * facerect.height);
            rect.height = cvRound(.25 * facerect.height);
            break;
        default:
            Err("EyeInnerRect: Invalid eyaw %d", eyaw);
            break;
    }
    rect.width  = MAX(0, rect.width);
    rect.height = MAX(0, rect.height);
    return rect;
}

// Is the horizontal overlap between the LeftEye and RightEye rectangles no
// more than 10% and is the horizontal distance between the edges of the
// eyes no more than the eye width.

static bool IsEyeHorizOk(
    const Rect& left,         // in
    const Rect& right)        // in
{
    return left.x + left.width - right.x   <= .1 * left.width &&
           right.x - (left.x + left.width) <= left.width;
}

static bool VerticalOverlap( // do the two eyes overlap vertically?
    const Rect& left,        // in
    const Rect& right)       // in
{
    const int topleft = left.y + left.height;
    const int topright = right.y + right.height;

    return (left.y   >= right.y && left.y   <= right.y + right.height) ||
           (topleft  >= right.y && topleft  <= right.y + right.height) ||
           (right.y  >= left.y  && right.y  <= left.y  + left.height)  ||
           (topright >= left.y  && topright <= left.y  + left.height);
}


// Is the center of rect within the enclosing rect?

static bool InRect(
    const Rect& rect,      // in
    const Rect& enclosing) // in
{
    int x = rect.x + rect.width / 2;  // center of rectangle
    int y = rect.y + rect.height / 2;

    return x >= enclosing.x &&
           x <= enclosing.x + enclosing.width &&
           y >= enclosing.y &&
           y <= enclosing.y + enclosing.height;
}

// Return the indices of the best left and right eye in the list of eyes.
// returned by the feature detectors.
// The heuristic in in detail (based on looking at images produced):
// Find the left and right eye that
//  (i)   are both in eye_inner_rect
//  (ii)  don't overlap horizontally by more than 10%
//  (ii)  overlap vertically.
//  (iii) have the largest total width.
//  (iv)  if frontal have an intereye dist at least .25 * eye_inner_rect width

static void SelectEyes(
    int&            ileft_best,     // out: index into leyes, -1 if none
    int&            iright_best,    // out: index into reyes, -1 if none
    EYAW            eyaw,           // in
    const vec_Rect& leyes,          // in: left eyes found by detectMultiScale
    const vec_Rect& reyes,          // in: right eyes found by detectMultiScale
    const Rect&     eye_inner_rect) // in: center of the eye must be in this region
{
    ileft_best = iright_best = -1; // assume will return no eyes
    int min_intereye = eyaw == EYAW00? cvRound(.25 * eye_inner_rect.width): 0;
    int maxwidth = 0; // combined width of both eye boxes
    int ileft, iright;
    Rect left, right;

    // this part of the code will either select both eyes or no eyes

    for (ileft = 0; ileft < NSIZE(leyes); ileft++)
    {
        left = leyes[ileft];
        if (InRect(left, eye_inner_rect))
        {
            for (iright = 0; iright < NSIZE(reyes); iright++)
            {
                right = reyes[iright];
                if (InRect(right, eye_inner_rect) &&
                    IsEyeHorizOk(left, right) &&
                    right.x - left.x >= min_intereye &&
                    VerticalOverlap(left, right))
                {
                    int total_width = left.width + right.width;
                    if (total_width > maxwidth)
                    {
                        maxwidth = total_width;
                        ileft_best = ileft;
                        iright_best = iright;
                    }
                }
            }
        }
    }
    if (ileft_best == -1 && iright_best == -1)
    {
        // The above loops failed to find a left and right eye in correct
        // relationship to each other.  So simply select largest left eye and
        // largest right eye (but make sure that they are in the eye_inner_rect).

        int max_left_width = 0;
        for (ileft = 0; ileft < NSIZE(leyes); ileft++)
        {
            left = leyes[ileft];
            if (InRect(left, eye_inner_rect))
            {
                if (left.width > max_left_width)
                {
                    max_left_width = left.width;
                    ileft_best = ileft;
                }
            }
        }
        int max_right_width = 0;
        for (iright = 0; iright < NSIZE(reyes); iright++)
        {
            right = reyes[iright];
            if (InRect(right, eye_inner_rect))
            {
                if (right.width > max_right_width)
                {
                    max_right_width = right.width;
                    iright_best = iright;
                }
            }
        }
        // One final check (for vr08m03.bmp) -- if the two largest eyes overlap
        // too much horizontally then discard the smaller eye.

        if (ileft_best != -1 && iright_best != -1)
        {
            left = leyes[ileft_best];
            right = reyes[iright_best];
            if (!IsEyeHorizOk(left, right) || right.x - left.x < min_intereye)
            {
                if (max_right_width > max_left_width)
                    ileft_best = -1;
                else
                    iright_best = -1;
            }
        }
    }
}

// The values below are fairly conservative: for the ASM start shape,
// it's better to not find a mouth than to find an incorrect mouth.

static Rect MouthInnerRect(
    const Rect&     facerect,    // in
    EYAW            eyaw,        // in
    int             ileft_best,  // in: index of best left eye, -1 if none
    int             iright_best, // in: index of best right eye, -1 if none
    const vec_Rect& leyes,       // in: left eyes found by eye detector
    const vec_Rect& reyes)       // in: right eyes found by eye detector
{
    Rect rect = facerect;
    double width = (eyaw == EYAW00? .12: .20) * facerect.width;
    double height = .30 * facerect.height;

    int ixshift, iyshift;
    MouthRectShift(ixshift, iyshift,
                   eyaw, facerect.width, facerect.height,
                   ileft_best, iright_best, leyes, reyes);

    rect.x += cvRound(.50 * (facerect.width - width)) + ixshift;

    rect.width  =  cvRound(width);

    switch (eyaw)
    {
        case EYAW00: // frontal model
            rect.y += cvRound(.7 * facerect.height);
            break;
        case EYAW_45: // left facing three-quarter model
            rect.y += cvRound(.65 * facerect.height);
            break;
        case EYAW_22: // left facing three-quarter model
            rect.y += cvRound(.65 * facerect.height);
            break;
        case EYAW22: // right facing three-quarter model
            rect.y += cvRound(.65 * facerect.height);
            break;
        case EYAW45: // right facing three-quarter model
            rect.y += cvRound(.65 * facerect.height);
            break;
        default:
            Err("MouthInnerRect: Invalid eyaw %d", eyaw);
            break;
    }
    rect.y += iyshift;
    rect.height = cvRound(height);
    rect.width  = MAX(0, rect.width);
    rect.height = MAX(0, rect.height);
    return rect;
}

// The OpenCV mouth detector biases the position of the mouth downward (wrt the
// center of the mouth determined by manual landmarking).  Correct that here.

static int MouthVerticalShift(
    const int       ileft_best,   // in
    const int       iright_best,  // in
    const int       imouth_best,  // in
    const vec_Rect& leyes,        // in
    const vec_Rect& reyes,        // in
    const vec_Rect& mouths)       // in
{
    double shift = 0;
    if (ileft_best != -1 && iright_best != -1) // got both eyes?
    {
        CV_Assert(imouth_best != -1);
        // get eye mouth distance: first get center of both eyes
        const double xleft  = leyes[ileft_best].x  + leyes[ileft_best].width   / 2;
        const double yleft  = leyes[ileft_best].y  + leyes[ileft_best].height  / 2;
        const double xright = reyes[iright_best].x + reyes[iright_best].width  / 2;
        const double yright = reyes[iright_best].y + reyes[iright_best].height / 2;
        const double eyemouth =
            PointDist((xleft + xright) / 2,(yleft + yright) / 2,
                      mouths[imouth_best].x, mouths[imouth_best].y);
        static const double MOUTH_VERT_ADJUST = -0.050; // neg to shift up
        shift = MOUTH_VERT_ADJUST * eyemouth;
    }
    return cvRound(shift);
}

// Return the indices of the best mouth in the list of mouths

static void SelectMouth(
    int&            imouth_best,      // out: index into mouths, -1 if none
    int             ileft_best,       // in: index of best left eye, -1 if none
    int             iright_best,      // in: index of best right eye, -1 if none
    const vec_Rect& leyes,            // in: left eyes found by eye detector
    const vec_Rect& reyes,            // in: right eyes found by eye detector
    const vec_Rect& mouths,           // in: left eyes found by eye detector
    const Rect&     mouth_inner_rect) // in: center of mouth must be in this region
{
    CV_Assert(!mouths.empty());
    imouth_best = -1;

    // if only one mouth, use it
    if (NSIZE(mouths) == 1 && InRect(mouths[0], mouth_inner_rect))
        imouth_best = 0;
    else
    {
        // More than one mouth: selected the lowest mouth to avoid
        // "nostril mouths".  But to avoid "chin mouths", the mouth
        // must also meet the following criteria:
        //   i)  it must be wider than the .7 * smallest eye width
        //   ii) it must be not much narrower than widest mouth.

        int minwidth = 0;
        if (ileft_best != -1)
            minwidth = leyes[ileft_best].width;
        if (iright_best != -1)
            minwidth = MIN(minwidth, reyes[iright_best].width);
        minwidth = cvRound(.7 * minwidth);

        // find widest mouth
        int maxwidth = minwidth;
        for (int imouth = 0; imouth < NSIZE(mouths); imouth++)
        {
            const Rect mouth = mouths[imouth];
            if (InRect(mouth, mouth_inner_rect) && mouth.width > maxwidth)
            {
                maxwidth = mouth.width;
                imouth_best = imouth;
            }
        }
        // choose lowest mouth that is at least .84 the width of widest
        minwidth = MAX(minwidth, cvRound(.84 * maxwidth));
        int ymin = int(-1e5);
        for (int imouth = 0; imouth < NSIZE(mouths); imouth++)
        {
            const Rect mouth = mouths[imouth];
            if (InRect(mouth, mouth_inner_rect) &&
                mouth.y + mouth.height / 2 > ymin &&
                mouth.width > minwidth)
            {
                ymin = mouth.y + mouth.height / 2;
                imouth_best = imouth;
            }
        }
    }
}

static void TweakMouthPosition(
    vec_Rect&       mouths,      // io
    const vec_Rect& leyes,       // in
    const vec_Rect& reyes,       // in
    const int       ileft_best,  // in
    const int       iright_best, // in
    const int       imouth_best, // in
    const DetPar&   detpar)      // in

{
    mouths[imouth_best].y += // move mouth up to counteract OpenCV mouth bias
         MouthVerticalShift(ileft_best, iright_best, imouth_best,
                            leyes, reyes, mouths);

    // If face pose is strong three-quarter, move mouth
    // out to counteract OpenCV mouth detector bias.

    if (detpar.eyaw == EYAW_45)
        mouths[imouth_best].x -= cvRound(.06 * detpar.width);
    else if (detpar.eyaw == EYAW45)
        mouths[imouth_best].x += cvRound(.06 * detpar.width);
}

static void RectToImgFrame(
    double&     x,          // out: center of feature
    double&     y,          // out: center of feature
    const Rect& featrect)   // in
{
    x = featrect.x + featrect.width / 2;
    y = featrect.y + featrect.height / 2;
}

void DetectEyesAndMouth(  // use OpenCV detectors to find the eyes and mouth
    DetPar&       detpar, // io: eye and mouth fields updated, other fields untouched
    const Image&  img,    // in: ROI around face (already rotated if necessary)
    StasmCascadeClassifier cascade)
{
    Rect facerect(cvRound(detpar.x - detpar.width/2),
                  cvRound(detpar.y - detpar.height/2),
                  cvRound(detpar.width),
                  cvRound(detpar.height));

    // possibly get the eyes

    detpar.lex = detpar.ley = INVALID; // mark eyes as unavailable
    detpar.rex = detpar.rey = INVALID;
    vec_Rect leyes, reyes;
    int ileft_best = -1, iright_best = -1; // indices into leyes and reyes

    DetectAllEyes(leyes, reyes, img, detpar.eyaw, facerect, cascade);

    SelectEyes(ileft_best, iright_best, detpar.eyaw, leyes, reyes, EyeInnerRect(detpar.eyaw, facerect));

    if (ileft_best >= 0)
        RectToImgFrame(detpar.lex, detpar.ley,
                       leyes[ileft_best]);

    if (iright_best >= 0)
        RectToImgFrame(detpar.rex, detpar.rey,
                       reyes[iright_best]);

    // possibly get the mouth

    detpar.mouthx = detpar.mouthy = INVALID;  // mark mouth as unavailable
    vec_Rect mouths;
    DetectAllMouths(mouths,
                    img, detpar.eyaw, facerect,
                    ileft_best, iright_best, leyes, reyes, cascade.mouthCascade);

    if (!mouths.empty())
    {
        int imouth_best = -1;

        SelectMouth(imouth_best,
                    ileft_best, iright_best, leyes, reyes, mouths,
                    MouthInnerRect(facerect, detpar.eyaw,
                                   ileft_best, iright_best, leyes, reyes));

        if (imouth_best >= 0)
        {
            TweakMouthPosition(mouths,
                               leyes, reyes, ileft_best, iright_best,
                               imouth_best, detpar);

            RectToImgFrame(detpar.mouthx, detpar.mouthy,
                           mouths[imouth_best]);
        }
    }
}

} // namespace stasm