How to repair damaged images using inpainting methods in Python using OpenCV – 2024

In today’s blog, we will see that how we can repair damaged images in Python using inpainting methods of OpenCV. This is gonna be a very fun project, So without any further due, let’s dive into it.

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.

This is going to be a very interesting blog, so without any further due, Let’s do it…

Step 1 – Let’s import the libraries.

import cv2
import matplotlib.pyplot as plt

Step 2 – Read the damaged image.

damaged_image_path = "Damaged Image.tiff"
damaged_image = cv2.imread(damaged_image_path)
repair damaged images, org image
Damaged Image

Step 3 – Read the mask.

mask_path = "Mask.tiff"
mask = cv2.imread(mask_path, 0)
  • Here we are reading our mask in grayscale mode.
  • Mask is basically a binary image in which the white portion depicts the pixels or places where our original image is damaged.
mask 1
mask of damaged image

Step 4 – Convert the damaged image from BGR to RGB.

damaged_image = cv2.cvtColor(damaged_image, cv2.COLOR_BGR2RGB)

Here we are just converting our image from BGR to RGB because cv2 automatically reads the image in BGR format.

Step 5 – Let’s repair damaged images.

Syntax: cv2.inpaint(src, inpaintMask, inpaintRadius, flags)

Parameters:

srcInput damaged image
inpaintMaskInpainting mask, 8-bit grayscale image. Non-zero pixels indicate the area that needs to be inpainted.
inpaintRadiusRadius of a circular neighborhood of each point inpainted that is considered by the algorithm.
flagsInpainting method that could be cv::INPAINT_NS or cv::INPAINT_TELEA
output1 = cv2.inpaint(damaged_image, mask, 1, cv2.INPAINT_TELEA)
output2 = cv2.inpaint(damaged_image, mask, 1, cv2.INPAINT_NS)

Step 6 – Let’s plot the results.

img = [damaged_image, mask, output1, output2]
titles = ['damaged image', 'mask', 'TELEA', 'NS']

for i in range(4):
    plt.subplot(2, 2, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.title(titles[i])
    plt.imshow(img[i])
plt.show()
repair damaged images
Results

Let’s see the whole code…

# repair damaged images
import cv2
import matplotlib.pyplot as plt

damaged_image_path = "Damaged Image.tiff"
damaged_image = cv2.imread(damaged_image_path)

mask_path = "Mask.tiff"
mask = cv2.imread(mask_path, 0)

damaged_image = cv2.cvtColor(damaged_image, cv2.COLOR_BGR2RGB)

output1 = cv2.inpaint(damaged_image, mask, 1, cv2.INPAINT_TELEA)
output2 = cv2.inpaint(damaged_image, mask, 1, cv2.INPAINT_NS)

img = [damaged_image, mask, output1, output2]
titles = ['damaged image', 'mask', 'TELEA', 'NS']

for i in range(4):
    plt.subplot(2, 2, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.title(titles[i])
    plt.imshow(img[i])
plt.show()

NOTE – Read more about cv2.inpaint().

Do let me know if there’s any query regarding repair damaged images by contacting me by email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: HOW TO GENERATE A NEGATIVE IMAGE IN PYTHON USING OPENCV

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *