Human face acne principle

In fact, in front of the face skin and whitening, we have completed the face acne, as long as the height of skin (bilateral filtering) can achieve the effect of acne.

But generally speaking, now the Meituapp will provide users with the function of manual acne removal, after all, no matter how good the effect of automatic whitening acne removal, can not be perfect to restore all faces, so additional personal repair is particularly important.

In fact, face acne, is the image repair. The patch function provided in OpenCV is cv2.inpaint(). Here, let’s first look at the definition of the change function:

def inpaint(src, inpaintMask, inpaintRadius, flags, dst=None) :
Copy the code

SRC: Input an image

InpaintMask: Repair mask

InpaintRadius: The circular field of each point to be repaired is the reference radius of the repair algorithm

Flags: Indicates the repair method. There are two values. The value is INPAINT_NS based on Navier-Stokes method. INPAINT_TELEA is based on Alexandru Telea.

DST: Returns the restored image

The principle of this method is to use the edge information of the area to be repaired, at the same time, a coarse to fine method is adopted to estimate the isoillumination line, and the propagation mechanism is adopted to spread the information to the area to be repaired, so as to achieve a better repair effect.

Use the edge pixel value to patch the pixels in the specified area.

To achieve manual face acne

Now that we know the method function for removing acne manually. Next, let’s achieve the manual acne effect, the specific code is as follows:

global img, point
global inpaintMask
# Remove acne manually
def manual_acne(event, x, y, flags, param) :
    global img, point
    img2 = img.copy()
    height, width, n = img.shape
    inpaintMask = np.zeros((height, width), dtype=np.uint8)
    if event == cv2.EVENT_LBUTTONDOWN:
        point = (x, y)
        cv2.circle(img2, point, 15, (0.255.0), -1)
        cv2.circle(inpaintMask, point, 15.255, -1)
        cv2.imshow("image", img2)
    elif event == cv2.EVENT_LBUTTONUP:
        cv2.circle(img2, point, 15, (0.255.0), -1)
        cv2.circle(inpaintMask, point, 15.255, -1)
        cv2.imshow("inpaintMask", inpaintMask)
        cv2.imshow("image", img2)
        cv2.imshow("image0", img)
        result = cv2.inpaint(img, inpaintMask, 100, cv2.INPAINT_TELEA)
        cv2.imshow("result", result)


if __name__ == "__main__":
    global img
    img = cv2.imread("60.jpg")
    cv2.namedWindow("image")
    cv2.setMouseCallback("image", manual_acne)
    cv2.imshow("image", img)
    cv2.waitKey()
    cv2.destroyAllWindows()
Copy the code

After running, the effect is as follows: