Saturday 8 May 2021

opencv 44 inpainting


tattoo stickers on baby's hands

In windows paint, select stickers and cut them out

generate mask with opencv

opencv inpaint function removes stickers and restores hands from deep learning
#main.py
import argparse
import cv2
import numpy as np

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str, required=True,
                help="path input image on which we'll perform inpainting")
ap.add_argument("-m", "--mask", type=str, required=True,
                help="path input mask which corresponds to damaged areas")
ap.add_argument("-a", "--method", type=str, default="telea",
                choices=["telea", "ns"],
                help="inpainting algorithm to use")
ap.add_argument("-r", "--radius", type=int, default=3,
                help="inpainting radius")
args = vars(ap.parse_args())

# initialize the inpainting algorithm to be the Telea et al. method
flags = cv2.INPAINT_TELEA

# check to see if we should be using the Navier-Stokes (i.e., Bertalmio
# et al.) method for inpainting
if args["method"] == "ns":
    flags = cv2.INPAINT_NS

image = cv2.imread(args["image"])
mask = cv2.imread(args["mask"])

cv2.imshow("image cut", mask)

mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

#if pixel is not white then set it to black
mask = np.where(mask != 255, 0, 255).astype("uint8")

#remove salt and pepper noise
mask = cv2.medianBlur(mask, 15)

#mask dilation
kernal = np.ones((5, 5), np.uint8)
mask = cv2.dilate(mask, kernal, iterations=3)

# perform inpainting using OpenCV
output = cv2.inpaint(image, mask, args["radius"], flags=flags)

# show the original input image, mask, and output image after
# applying inpainting
cv2.imshow("Image", image)
cv2.imshow("Mask", mask)
cv2.imshow("Output", output)
cv2.waitKey(0)

reference:

No comments:

Post a Comment