Opencv C++ face detection Tutorial with Transparent image

Face detection and Transparent image in opencv 

Face detection Opencv C++ tutorial about how to replace the face with mask. Easy steps to achieve results like many popular applications that just enhance your face. Code is just part that we already have available video reader written in opencv and just apply the face detection part with mask over the detected face. 

Pixel by pixel color value filtering

Let me explain how to add a transparent mask over the ROI in a video sequence. 
This image of the anonymous mask has a white background. Add this image to the selected area  pixel by pixel with following condition. Add mask to the background video if the color of the mask is black. It s really simple idea. Image could be added over the roi in different way, but in the case you want to filter by pixel color value. This is example for you.

Transparent Mask Opnecv

Code Opencv Tutorial

Just remember you need to at least figure out how to load the image or video into the IMG source mat. 

//Normal face detection by haar cascade in opencv
  CascadeClassifier faceDetector;
  bool loaded1 = faceDetector.load("facehaarcascade.xml");
  Mat original;  img.copyTo(original);
  std::vector faces;  
  cvtColor(img, img, CV_BGR2GRAY); 
  equalizeHist(img, img);
  detectorBody.detectMultiScale(img, faces, 1.1, 2, 0 | 1, Size(40, 40), Size(400,400 ));

Mat Original; 
//Read the anonymous mask 
Mat imageMask = imread("MASK.png", 1);
// Draw the mask over all rectangles
for( size_t i = 0; i < faces.size(); i++ )
      int xx =0, yy =0;
  // Just iterate in face region pixel by pixel
     for(int x = faces[i].x; x < faces[i].x+faces[i].width; x++)
              for (int y = faces[i].y; y < faces[i].y+faces[i].height; y++)
                     //This is core of the trick. 
                     // Copy Mask to Original image  If the 0 chan
                     // Proper condition is over all color channels
                     //,yy)[0] < 10
                     //,yy)[1] < 10
                     //,yy)[2] < 10
                                               // Black Color(0,0,0)
                        if (,yy)[0] < 10)
                          // Copy to original image on (y,x) places  the pixel of xx,yy mask
                ,x)[0] =  imageMask .at(xx,yy)[0];
                ,x)[1] =  imageMask .at(xx,yy)[1];
                ,x)[2] =  imageMask .at(xx,yy)[2]; 
                          // Iterate in mask x
                  xx =xx+1; 
      // iterate next row of imageMask
      xx = 0;
      yy =yy+1;


  1. so interesting! can i have full source code bro? :D

  2. This is almost full source code! You only need capture video from video file or web camera. Put this image or image sequence into Mat Original; and apply the mask (Code is here). I am sorry this is more than enough. You only need to video input loop.

  3. I am sorry this is more than enough. You only need to video input loop.



  4. Great work! I want to do something like placing image on the eyes. How can I get it done? Any ideas? I'd really love to know. Thank you! :D

  5. creativity of writer is purely impressive. It has touched to the level of expertise with his writing. Everything is up to the mark. Written perfectly and I can use such information for my coming assignment.ทำจมูก

  6. Videos and articles are good and useful.


  7. This comment has been removed by the author.

  8. It is a subject that is novel and very interesting to follow.

    viva9988 holiday

  9. โปรโมชั่นใหม่ “แจกโบนัส สูงสุดทันที 1,000 บาท”
    gclub online โปรโมชั่น แบบครบวงจรที่คอยอัพเดทข่าวสารเกี่ยวกับโปรโมชั่น จีคลับ บาคาร่าออนไลน์ สล็อตออนไลน์ ตลอดทั้ง 24 ชั่วโมง เพื่อให้ท่านได้สามารถเข้าร่วมสนุกไปกับเกมออนไลน์ของเราได้ตลอดเวลาโปรโมชั่นสำหรับสมาชิก Premium, VIP ที่แจกโบนัสให้สูงที่สุด 10% และโปรโมชั่นแบบรายวัน สำหรับคำขอบคุณที่มอบให้กับ โปรโมชั่น gclub


Post a Comment