Ask Your Question

How to show overlay image over detected face in openCv c++?

asked 2018-03-25 03:54:31 -0500

_HangOver_ gravatar image

updated 2018-03-25 04:32:18 -0500

berak gravatar image

I am working on face detection using openCv in iOS platform. I am using openCv in c++. It detects face nicely and I am able to draw a rectangle around this face. But I need to show a overlay image instead of rectangle. Here is my code.

 - (void)processImage:(Mat&)image;

     cv::cvtColor(image, img, CV_BGR2GRAY);
     equalizeHist(img, img);
     NSString* cascadePath = [[NSBundle mainBundle]

     CascadeClassifier face_cascade;

     face_cascade.load([cascadePath UTF8String]);

     std::vector<cv::Rect> faces;
     face_cascade.detectMultiScale(img, faces, 1.1,
                                   2, haarOptions, cv::Size(30, 30));

     for(unsigned int i = 0; i < faces.size(); i++) {

         cv::Point p1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
         cv::Point p2(faces[i].x , faces[i].y);
         cv::Scalar magenta = cv::Scalar(255, 10, 255);
         cv::Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
         //const std::string text = Eye;
          cv::putText(image, Face,p2 ,FONT_HERSHEY_SIMPLEX, 1, Scalar(0,200,200), 2);
          cv::rectangle(image, p1, p2, magenta,1,8,0);


Here is my output..

image description

Now I want replace this rectangle with an overlay image. Is it possible? If so then please help me. Thanks in advanced.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-26 05:56:58 -0500

updated 2018-03-26 05:59:50 -0500

Yes it is possible! You simply have to paste your overlay into the image, with the bounding rectangle as a region of interest.

A quick code example

Mat original_image;
Mat overlay;
Rect ROI; // your bounding box of the detection
resize(overlay, overlay, Size(ROI.height, ROI.width));
overlay.copyTo( original_image(ROI) );

An example on how to do so can be seen here, where they use this to build snapchat filters.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-03-25 03:54:31 -0500

Seen: 1,817 times

Last updated: Mar 26 '18