MLearning.ai
Published in

MLearning.ai

How to Repair Damaged Images using Inpainting methods in Python using OpenCV

In today’s blog, we will see 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.

Read the full article with source code here — https://machinelearningprojects.net/repair-damaged-images-using-inpainting/

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)

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.

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)

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()

NOTE — Read more about cv2.inpaint().

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

For further code explanation and source code visit here https://machinelearningprojects.net/repair-damaged-images-using-inpainting/

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 projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Sharma

Abhishek Sharma

Data Scientist || Blogger || machinelearningprojects.net || Contact me for freelance projects on asharma70420@gmail.com