Share
Visit

Sunday, November 8, 2015

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
                     // imageMask.at(xx,yy)[0] < 10
                     // imageMask.at(xx,yy)[1] < 10
                     // imageMask.at(xx,yy)[2] < 10
                                               // Black Color(0,0,0)
                        if (imageMask.at(xx,yy)[0] < 10)
                        {
                          // Copy to original image on (y,x) places  the pixel of xx,yy mask
                          Original.at(y,x)[0] =  imageMask .at(xx,yy)[0];
                          Original.at(y,x)[1] =  imageMask .at(xx,yy)[1];
                          Original.at(y,x)[2] =  imageMask .at(xx,yy)[2]; 
                          }
                          // Iterate in mask x
                  xx =xx+1; 
            }
      // iterate next row of imageMask
      xx = 0;
      yy =yy+1;
      }
   }

11 comments:

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

    ReplyDelete
  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.

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


    โกลเด้นสล็อต



    goldenslot

    ReplyDelete
  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

    ReplyDelete
  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.ทำจมูก

    ReplyDelete
  6. Videos and articles are good and useful.

    goldenslot

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete

ad