Detect Faces
// Capture the current frame
cap >> frame;
// Resize the frame
resize(frame, frame, Size(), scalingFactor, scalingFactor, INTER\_AREA);
// Convert to grayscale
cvtColor(frame, frameGray, CV\_BGR2GRAY);
// Equalize the histogram
equalizeHist(frameGray, frameGray);
// Detect faces
faceCascade.detectMultiScale(frameGray, faces, 1.1, 2, 0CV\_HAAR\_SCALE\_IMAGE, Size(30, 30) );
Add Mask Image
// Draw green rectangle around the face
for(int i = 0; i < faces.size(); i++)
{
//Rect faceRect(faces\[i\].x, faces\[i\].y, faces\[i\].width, faces\[i\].height);
// Custom parameters to make the mask fit your face. You may have to play around with them to make sure it works.
int x = faces\[i\].x - int(0.1\*faces\[i\].width);
int y = faces\[i\].y - int(0.1\*faces\[i\].height); // 0.0\*faces\[i\].height
int w = int(1.1 \* faces\[i\].width);
int h = int(1.3 \* faces\[i\].height);
// Extract region of interest (ROI) covering your face
frameROI = frame(Rect(x,y,w,h));
// Resize the face mask image based on the dimensions of the above ROI
resize(faceMask, faceMaskSmall, Size(w,h));
// Convert the above image to grayscale
cvtColor(faceMaskSmall, grayMaskSmall, CV\_BGR2GRAY);
// Threshold the above image to isolate the pixels associated only with the face mask
threshold(grayMaskSmall, grayMaskSmallThresh, 230, 255, CV\_THRESH\_BINARY\_INV);
// Create mask by inverting the above image (because we don't want the background to affect the overlay)
bitwise\_not(grayMaskSmallThresh, grayMaskSmallThreshInv);
// Use bitwise "AND" operator to extract precise boundary of face mask
bitwise\_and(faceMaskSmall, faceMaskSmall, maskedFace, grayMaskSmallThresh);
// Use bitwise "AND" operator to overlay face mask
bitwise\_and(frameROI, frameROI, maskedFrame, grayMaskSmallThreshInv);
// Add the above masked images and place it in the original frame ROI to create the final image
add(maskedFace, maskedFrame, frame(Rect(x,y,w,h)));
}