Roi检测眼部区域

时间:2015-11-16 15:45:51

标签: c++ opencv

我想将ROI分配给检测到的脸部,以便仅裁剪眼部区域。

我试过了:

face_cascade.detectMultiScale(frame_gray, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));

size_t i = 0; // ic is index of current element

for (i = 0; i < faces.size(); i++) // Iterate through all current elements (detected faces)

{

    Point pt1(faces[i].x, faces[i].y); // Display detected faces on main window - live stream from camera
    Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
    rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0);

    // set ROI for the eyes

    Rect Roi = faces[i];

    Roi.height = Roi.height / 4;

    Roi.y = Roi.y + Roi.height;

    cv::Mat crop = frame(Roi);

    imshow("ROI", crop);

这是输出:ROI

如何使此输出更准确,就像这张图片一样 Only eyes Roi

2 个答案:

答案 0 :(得分:0)

您可以使用以下函数以给定的百分比值缩小Rect

Rect shrinkRect( Rect rect, int percent )
{
        if (percent>99)
        return rect;

    Rect newrect;
    newrect.width=( rect.width * percent ) / 100;
    newrect.height=( rect.height * percent ) / 100;
    newrect.x = rect.x + ( rect.width - newrect.width ) / 2;
    newrect.y = rect.y + ( rect.height - newrect.height ) / 2;

    return newrect;
}

测试代码:

#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

Rect shrinkRect( Rect rect, int percent )
{
    if (percent>99)
        return rect;

    Rect newrect;
    newrect.width=( rect.width * percent ) / 100;
    newrect.height=( rect.height * percent ) / 100;
    newrect.x = rect.x + ( rect.width - newrect.width ) / 2;
    newrect.y = rect.y + ( rect.height - newrect.height ) / 2;

    return newrect;
}

int main( void )
{

    Rect r;
    r.x = 100;
    r.y = 100;
    r.width = 200;
    r.height = 200;

    cout << r  << endl;
    cout << shrinkRect( r, 75 ) << endl;
    cout << shrinkRect( r, 50 ) << endl;
    cout << shrinkRect( r, 30 ) << endl;

    return 0;
}

测试代码输出:

[200 x 200 from (100, 100)]
[150 x 150 from (125, 125)]
[100 x 100 from (150, 150)]
[60 x 60 from (170, 170)]

答案 1 :(得分:0)

您可能希望使用眼睛分类器来获得唯一的眼睛区域。

有单独的(左/右)眼睛分类器和眼睛对分类器(例如haarcascade_mcs_eyepair_big.xml)

这些应该可以为您提供眼睛的投资回报率。