Question

I have a face tracking program that reads video from a camera and draws a rectangle around the persons face. What I want to do is have the program recognise when the face enters a particular region of the frame, and initialise some other action. What commands would I need to do this? (I am using C++ and openCV 2.4.3)

E.g

    detect face;
if (face is in ROI)
{
    close video feed;
}
Était-ce utile?

La solution

So you have a rectangle enclosing your face and a rectangle defining the ROI of the image. To check if the face enters the ROI you just have to check if the two rects intersect. The easiest way to do this is to use the overloaded operator & of cv::Rect_ as described here ( http://docs.opencv.org/modules/core/doc/basic_structures.html#rect ) and then check if the area of the resulting rect is > 0.

An example code would look as follows:

cv::Rect r1(0, 0, 10, 10);
cv::Rect r2(5, 5, 10, 10);
if ( (r1&r2).area() )
{
    // rects intersect
}

If you want the face to have entered the ROI with a certain percentage, you can compare the intersection area with the minimun of both input areas:

cv::Rect r1(0, 0, 10, 10);
cv::Rect r2(5, 5, 10, 10);
double minFraction( 0.1 );
if ( (r1&r2).area() > minFraction * std::min(r1.area(), r2.area() ) )
{
    // rects intersect by at least minFraction
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top