Programmatically use OpenCV and Python to remove debris and scratches from scanned photos and remove unwanted objects from still images and videos.
This time, I will erase the electric wire.
** ◎ Input image **
** ◎ Mask **
** ◎ Result **
cv2.inpaint
inpaint(src, inpaintMask, inpaintRadius, flags[, dst]) -> dst
A mask is applied to the input image, and the mask part is interpolated inward based on the image around the mask boundary.
inpaint.py
# -*- coding: utf-8 -*-
import cv2
#Original image
img = cv2.imread('img.jpg')
#mask
mask = cv2.imread('mask.png',0)
#Image correction
dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
#dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_NS)
#display
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Save
cv2.imwrite('result.jpg', dst)
OpenCV : 3.1.0 Python : 3.5.2
Please refer to here for building OpenCV and Python environment. (Link)
Since the pixels in the mask are interpolated from the pixels around the mask, it works well if the image has a relatively flat feeling around the mask. On the contrary, if the surroundings are complicated patterns, it will not work. If the mask does not completely mask the part you want to erase, noise will appear. Also, if there was a problem with the shape of the mask, the mask part might appear like a watermark.
Similar image operations are possible in PhotoShop, but you will have to do it manually. With OpenCV, you can do it programmatically, so you can apply it to videos. For privacy protection, it seems that you can also use it to automatically extract people in the video and automatically complement that part with this method.
Recommended Posts