hrastnik / FaceSwap

Real-time FaceSwap application built with OpenCV and dlib
MIT License
679 stars 189 forks source link

[Feature] Creating better face masks using opencv::convexHull #18

Open alexandarZ opened 6 years ago

alexandarZ commented 6 years ago

Hi Mateo,

Your work is great! I have one improvement for you. Face mask could be much better extracted using convexHull. This is my code where I use the convex hull method from OpenCV library to extract face mask and results are excellent.

cv::Mat FaceSwap::getFaceMask(cv::Mat &face, std::vector<cv::Point> &facePoints)
{
    cv::Mat mask;
    mask.create(face.size(), CV_8UC1);
    mask.setTo(cv::Scalar::all(0));

    // Find convex hull
    std::vector<int> hullPtsIndex;
    cv::convexHull(facePoints, hullPtsIndex, false, false);

    // Create convex polygon based on hull points
    cv::Point2i maskPoints[68];

    for(int i=0;i<hullPtsIndex.size();i++)
    {
        maskPoints[i] = facePoints[hullPtsIndex[i]];
    }

    // Fill mask polygon
    cv::fillConvexPoly(mask, maskPoints,hullPtsIndex.size(),cv::Scalar(255));

    return mask;
}

Results:

image

hrastnik commented 6 years ago

This is great. I will look into it.