Last Updated : 03 Aug, 2023
Image inpainting is the process of removing damage, such as noises, strokes or text, on images. It is particularly useful in the restoration of old photographs which might have scratched edges or ink spots on them. These can be digitally removed through this method. Image inpainting works by replacing the damaged pixels with pixels similar to the neighboring ones, therefore, making them inconspicuous and helping them blend well with the background. Consider the image below. The image has some marks to the right. To inpaint this image, we require a mask, which is essentially a black image with white marks on it to indicate the regions which need to be corrected. In this case, the mask is created manually on GIMP.
But don't worry. You don't need to learn how to use GIMP tool. Even if you know how to use OpenCV, you can easily create the following mask.
Creating the mask manually in OpenCV
We will follow the following steps to create the mask using OpenCV python:
import cv2
import numpy as np
# reading the damaged image
damaged_img = cv2.imread(filename=r"cat_damaged.png")
# get the shape of the image
height, width = damaged_img.shape[0], damaged_img.shape[1]
# Converting all pixels greater than zero to black while black becomes white
for i in range(height):
for j in range(width):
if damaged_img[i, j].sum() > 0:
damaged_img[i, j] = 0
else:
damaged_img[i, j] = [255, 255, 255]
# saving the mask
mask = damaged_img
cv2.imwrite('mask.jpg', mask)
# displaying mask
cv2.imshow("damaged image mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
If you run the above code block, you can see the following result (mask created using the above cat_damaged image using OpenCV):
mask created using the above cat_damaged image using OpenCVNow if you use the above mask and apply the following inpainting algorithm then you would get same results.
Inpainting Algorithms - OpenCV implements two inpainting algorithms:
FMM can be invoked by using cv2.INPAINT_TELEA, while Navier-Stokes can be invoked using cv2.INPAINT_NS. The Python code below inpaints the image of the cat using Navier-Stokes.
Python
import numpy as np
import cv2
# Open the image.
img = cv2.imread('cat_damaged.png')
# Load the mask.
mask = cv2.imread('cat_mask.png', 0)
# Inpaint.
dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_NS)
# Write the output.
cv2.imwrite('cat_inpainted.png', dst)
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4