Search code examples
opencvcomputer-visionface-detection

How do you cut a face out of an image?


I have been given an image with rgb channels. I only want to see the persons face. How would I do that? Are neural nets used for this? If so, are there existing data files from neural nets that have already done the processing?


Solution

  • Since your questions is tagged with OpenCV, I will assume that you are looking for a solution within this library.

    The first step is to find the faces. For this, use one of the cascade object detectors that are available: either the Viola-Jones one or the LBP one. OpenCV comes with cascades trained for face detection for each of these detectors.

    Then, it depends if getting a bounding box is enough or not.

    If you need something more accurate, then you can:

    • [coarse face] use a skin color detector inside the face bounding box to get a finer face estimate, binarize the image and finally close the face shape using morphological filtering;
    • [fine face contour] use something like a grabcut procedure to get a pixel-accurate contour. You can initialize the grabcut with borders of the bounding box as background and center part of the bounding box as foreground.