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.
Inpainting Algorithms –
OpenCV implements two inpainting algorithms:
- “An Image Inpainting Technique Based on the Fast Marching Method”, Alexandru Telea, 2004:
This is based on Fast Marching Method (FMM). Looking at the region to be inpainted, the algorithm first starts with the boundary pixels and then goes to the pixels inside the boundary. It replaces each pixel to be inpainted with a weighted sum of the pixels in the background, with more weight given to nearer pixels and boundary pixels.
- “Navier-Stokes, Fluid Dynamics, and Image and Video Inpainting”, Bertalmio, Marcelo, Andrea L. Bertozzi, and Guillermo Sapiro, 2001:
This algorithm is inspired by partial differential equations. Starting from the edges (known regions) towards the unknown regions, it propagates isophote lines (lines that join same-intensity points). Finally, variance in an area is minimized to fill colors.
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Transition from OpenCV 2 to OpenCV 3.x
- Image resizing using Seam carving using OpenCV in Python
- OpenCV Python Program to analyze an image using Histogram
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Image Processing using OpenCV in Java | Set 13 (Brightness Enhancement)
- Image Processing using OpenCV in Java | Set 14 ( Sharpness Enhancement )
- Reading an image in OpenCV using Python
- Python | Detect corner of an image using OpenCV
- Image Pyramid using OpenCV | Python
- Negative transformation of an image using Python and OpenCV
- Python | Image blurring using OpenCV
- Image Resizing using OpenCV | Python
- Find Circles and Ellipses in an Image using OpenCV | Python
- Image Translation using OpenCV | Python
- Image Registration using OpenCV | Python
- Python | Detect Polygons in an Image using OpenCV
- Image Steganography using OpenCV in Python
- Create Local Binary Pattern of an image using OpenCV-Python
- Log transformation of an image using Python and OpenCV
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.