Search code examples
pythonnumpyopencvmediapipe

How can I blur a mask and smooth its edges?


I want to change the color of the cheeks of this photo. I got a mask with sharp edges How can I blur a mask and smooth its edges? image mask


Solution

  • # import the necessary packages
    import argparse
    import cv2
    # construct the argument parser and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", type=str, default="pca8e.png",
        help="path to input image")
    args = vars(ap.parse_args())
    # load the image, display it to our screen, and initialize a list of
    # kernel sizes (so we can evaluate the relationship between kernel
    # size and amount of blurring)
    image = cv2.imread(args["image"])
    cv2.imshow("Original", image)
    kernelSizes = [(41,41)]
    # loop over the kernel sizes
    for (kX, kY) in kernelSizes:
        # apply a "Gaussian" blur to the image
        blurred = cv2.GaussianBlur(image, (kX, kY), 0)
        cv2.imshow("Gaussian ({}, {})".format(kX, kY), blurred)
        cv2.waitKey(0)
    

    Try this code to blur the mask image. But I don't know how you're going to use it with your image.