Image inpainting¶
| Fact | Value |
|---|---|
| Also called | Hole filling, Object removal, Telea inpainting, Navier-Stokes inpainting, Biharmonic inpainting |
| Status | Implemented and tested |
| Family | Restoration and reconstruction |
| Engine | Science engine |
| Type | Nonlinear, spatial, hole filling from the boundary |
| Output | Image |
| Operator shown as | algorithm (definition) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/inpainting.py, inpaint |
| Operator code | engine/src/pixelnexus_engine/filters/inpainting.py, inpaint_representation |
| Packages | OpenCV, scikit-image |
Overview¶
Inpainting fills the selected area from the pixels around it. Use it to remove scratches, dust, spots, wires or small objects. Select the defect, a little larger than it is, and apply the filter. Pixels outside the selection never change. Three classic methods are offered. Telea's method and the Navier-Stokes method come from OpenCV, and biharmonic inpainting comes from scikit-image. The filter runs in the PixelNexus engine on your computer.
How it works¶
- The studio sends the image and the selection to the engine.
- Every pixel the selection touches is part of the hole.
- The chosen method fills the hole from the known pixels around it.
- Pixels outside the hole are kept exactly.
- The studio blends the result in through the selection, so a soft edge fades.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(m\): The selection, from 0 to 1
- \(\Omega\): The hole
- \(I_0\): The image
- \(\Delta\): The Laplacian, the sum of the second derivatives
The hole Omega is every pixel the selection m touches. Pixels outside it never change. All three methods make the fill meet the image smoothly at the edge of the hole. Biharmonic inpainting asks for the smoothest surface that meets the image with its slope, so it solves the biharmonic equation in the hole. Telea's method fills the hole from its edge inward, in order of distance, as found by fast marching. Each new pixel is a weighted mean of the known pixels within the radius, each moved on by its gradient. The Navier-Stokes method of Bertalmio, Bertozzi and Sapiro carries the smoothness Delta I into the hole along the lines of equal brightness, as a 2D fluid carries its vorticity. So edges that meet the hole continue into it. The Inspector shows the equations of the chosen method.
Each method makes the fill meet the image smoothly at the edge of the hole, in its own way.
Telea's method is fast. It first finds the distance T of each hole pixel from the edge of the hole, with the fast marching method. Then it fills pixels in order of that distance, so the hole closes from its edge inward. A new pixel p is a weighted mean of the known pixels q within the radius. Each of them predicts I(p) from its value and its gradient, as I(q) + grad I(q) ยท (p - q). Pixels close to p, along the direction of grad T, and at the same distance from the edge count most.
The Navier-Stokes method of Bertalmio, Bertozzi and Sapiro treats brightness as the stream function of a 2D fluid. The smoothness Delta I then plays the part of the vorticity. In the steady state, it stays the same along the lines of equal brightness. So edges that meet the hole continue into it.
Biharmonic inpainting asks for the smoothest surface that meets the image at the edge of the hole, with its slope. It solves the biharmonic equation in the hole. A plane is its own biharmonic fill, so ramps are filled exactly.
Kernel or operator¶
The Inspector asks the engine for the stages with the current settings. They are the hole and the equations of the chosen method, with the radius for Telea and Navier-Stokes.
Algorithm¶
- Take every pixel the selection touches as the hole.
- Fill the hole with the chosen method, from the known pixels around it.
- Keep every pixel outside the hole.
- Blend the result in through the selection, so a soft edge fades.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Filled image | Filled image, Hole | The filled image, or the hole the filter fills, in white. |
Method method |
Telea (fast marching) | Telea (fast marching), Navier-Stokes, Biharmonic | How the hole is filled. Telea and Navier-Stokes come from OpenCV, biharmonic from scikit-image. |
Neighborhood radius radius |
3 | 1 to 20 px | How far around each hole pixel known pixels are used, for Telea and Navier-Stokes. Biharmonic does not use it. |
Implementation¶
inpaint in engine/src/pixelnexus_engine/filters/inpainting.py takes the selection from the job. The hole_of function turns it into the hole, and fill calls cv2.inpaint with INPAINT_TELEA or INPAINT_NS, or inpaint_biharmonic of scikit-image. A selection that covers the whole image is refused, since nothing is left to fill from.
This filter reads the selection, so its catalog entry sets needsSelection. The studio then uploads the selection as a gray image, and the job names it as its mask. Without a selection, Apply asks you to make one. With the inverse scope, the hole is everything outside the selection. There is no live preview. The result arrives when you click Apply.
The Stage viewer in the Learn panel cannot use your selection, since it works on a small patch. It fills a disk of radius 8 pixels at the center of the patch instead.
Worked example¶
| Method | Disks | Siemens star |
|---|---|---|
| Telea | 26.38 | 12.09 |
| Navier-Stokes | 23.91 | 12.56 |
| Biharmonic | 27.88 | 14.85 |
The scratch and spot of the figure, 918 pixels in all, on two 160 x 160 test images. The numbers are the PSNR in dB inside the hole, against the image before the damage. On a ramp that rises by 1 level per pixel to the right, the largest error in the hole is 7 levels for Telea, 4 for Navier-Stokes and 0 for biharmonic, since a plane is its own biharmonic fill.
Complexity¶
- Time: \(O(H r^2) for Telea and Navier-Stokes, a sparse solve for biharmonic\)
- Memory: \(O(N)\)
N is the number of pixels, H the number of hole pixels and r the radius. Biharmonic solves a sparse linear system with one unknown per hole pixel, which is slower for large holes.
Artifacts and limitations¶
Limitations
- Fills only from the edge of the hole, so it cannot invent texture or objects. Large holes in textured areas look smeared.
- OpenCV's Telea drifts by a few levels in large holes, even on a flat area. With the default radius, a hole of radius 25 on a flat gray ends up to 5 levels off.
Artifacts
- Smooth smears in large holes, most visible on texture such as grass or fabric.
- Edges that cross the hole at a slant may bend or blur.
Tests¶
Pixels outside the hole must never change. Telea and Navier-Stokes must equal cv2.inpaint, and biharmonic must equal inpaint_biharmonic rounded to 8 bits. Biharmonic must fill a linear ramp exactly. Navier-Stokes and biharmonic must keep a flat area flat. Telea's drift on a large flat hole must be the 5 levels the limits name. An empty hole must leave the image, and a hole over the whole image must be refused. The engine API tests send the selection as a mask. They also check that the engine refuses a job without its mask, a mask of another size, and a mask for a filter that does not read one.
References¶
- Telea, A. An image inpainting technique based on the fast marching method. Journal of Graphics Tools 9(1), 23 to 34, 2004. DOI 10.1080/10867651.2004.10487596.
- Bertalmio, M., Bertozzi, A. L., and Sapiro, G. Navier-Stokes, fluid dynamics, and image and video inpainting. IEEE Conference on Computer Vision and Pattern Recognition, I-355 to I-362, 2001. DOI 10.1109/CVPR.2001.990497.
- Damelin, S. B., and Hoang, N. S. On surface completion and image inpainting by biharmonic functions. Numerical aspects. International Journal of Mathematics and Mathematical Sciences, 3950312, 2018. DOI 10.1155/2018/3950312.