Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)

Introduction

Programmatically use OpenCV and Python to remove debris and scratches from scanned photos and remove unwanted objects from still images and videos.

This time, I will erase the electric wire.

** ◎ Input image ** img.jpg

** ◎ Mask ** mask.jpg

** ◎ Result ** result.jpg

cv2.inpaint inpaint(src, inpaintMask, inpaintRadius, flags[, dst]) -> dst

A mask is applied to the input image, and the mask part is interpolated inward based on the image around the mask boundary.

program

inpaint.py


# -*- coding: utf-8 -*-

import cv2

#Original image
img = cv2.imread('img.jpg')
#mask
mask = cv2.imread('mask.png',0)

#Image correction
dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
#dst = cv2.inpaint(img, mask, 3, cv2.INPAINT_NS)

#display
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

#Save
cv2.imwrite('result.jpg', dst)

OpenCV : 3.1.0 Python : 3.5.2

Please refer to here for building OpenCV and Python environment. (Link)

in conclusion

Since the pixels in the mask are interpolated from the pixels around the mask, it works well if the image has a relatively flat feeling around the mask. On the contrary, if the surroundings are complicated patterns, it will not work. If the mask does not completely mask the part you want to erase, noise will appear. Also, if there was a problem with the shape of the mask, the mask part might appear like a watermark.

Similar image operations are possible in PhotoShop, but you will have to do it manually. With OpenCV, you can do it programmatically, so you can apply it to videos. For privacy protection, it seems that you can also use it to automatically extract people in the video and automatically complement that part with this method.

Recommended Posts

Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Image editing with python OpenCV
Shining life with Python and OpenCV
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
Find image similarity with Python + OpenCV
Automatic update method with python Pyinstaller exe
Capturing images with Pupil, python and OpenCV
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
Light image processing with Python x OpenCV
Hello World and face detection with OpenCV 4.3 + Python
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
How to crop an image with Python + OpenCV
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
I tried "binarizing" the image with Python + OpenCV
[Small story] Test image generation with Python / OpenCV
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
Binarization with OpenCV / Python
Notes on HDR and RAW image processing with Python
Kernel Method with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
Image capture / OpenCV speed comparison with and without GPU
Visualize the range of interpolation and extrapolation with python
Automatic follow on Twitter with python and selenium! (RPA)
Create a High Dynamic Range Image (HDR) with OpenCV and Python (Mertens, Robertson, Debevec)
Image processing with Python
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
JPEG image generation by specifying quality with Python + OpenCV
Rotate and scale the image before cropping [python] [OpenCV]
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
[Python] Webcam frame size and FPS settings with OpenCV
Reading, displaying and speeding up gifs with python [OpenCV]
Python application: Data cleansing # 3: Use of OpenCV and preprocessing of image data
Web crawling, web scraping, character acquisition and image saving with python
Paste png with alpha channel as transparent image with Python / OpenCV
[Let's play with Python] Image processing to monochrome and dots
Convert video to black and white with ffmpeg + python + opencv
[Python] Accessing and cropping image pixels using OpenCV (for beginners)
[Python] Easy reading of serial number image files with OpenCV
Programming with Python and Tkinter
"Apple processing" with OpenCV3 + Python3
Python and hardware-Using RS232C with Python-
Camera capture with Python + OpenCV
[Python] Calculation method with numpy
[Python] Using OpenCV with Python (Basic)
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Get image features with OpenCV
python with pyenv and venv
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Works with Python and R
[Python] Image processing with scikit-image