Removing defects from a photo with image inpainting
Sometimes, photo images have defects. This is especially the case for old photos that have been scanned: they may have scratches, spots, and stains. All these imperfections hinder enjoyment of the photo. The procedure of reconstructing parts of an image based on their surroundings is called inpainting, and OpenCV has an implementation of this algorithm. Here, we'll review ways of exploiting this OpenCV functionality.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV version 3.3 (or greater) Python API package.
How to do it
You need to complete the following steps:
- Import the necessary modules:
import cv2 import numpy as np
- Define a class that encapsulates mask creation:
class MaskCreator: def __init__(self, image, mask): self.prev_pt = None self.image = image self.mask = mask self.dirty = False self.show() cv2.setMouseCallback('mask', self.mouse_callback) def...