Skip to content

Draft PR Add Python demo for smoothed TV inpainting#4152

Open
jb2178-star wants to merge 7 commits intoFEniCS:mainfrom
jb2178-star:demo-smoothed-tv-inpainting
Open

Draft PR Add Python demo for smoothed TV inpainting#4152
jb2178-star wants to merge 7 commits intoFEniCS:mainfrom
jb2178-star:demo-smoothed-tv-inpainting

Conversation

@jb2178-star
Copy link
Copy Markdown

@jb2178-star jb2178-star commented Apr 17, 2026

Summary

This PR adds a minimal Python demo for for variational image inpainting with smoothed total variation regularization on a synthetic image with an irregular interior mask, as this demonstrates the use of FEM over Finite Difference methods.

Starting with a variational inpainting model with the spirit of Chan, Shen we have J(u)= data fit + regularization term, we use the TV term from Rudin, Osher, Fatemi :

$$ \int_{\Omega}|\nabla u| \mathrm{d}x$$

And make it smooth:

$$ \int_{\Omega}\sqrt{|\nabla u|^2 +\varepsilon^2}$$

and the data fitted term, from standard masked least squares (m=1 on known, m=0 on missing data):

$${1\over 2}\int_{\Omega}m(x)(u-f)^2\mathrm{d}x $$

Due to the fact $|\nabla u|$ is not differentiable when $\nabla u=0$, and its hard to use newton's method, so we introduce a small $\varepsilon$ term.

When applying Euler-Lagrange to minimize $J(u)$ we get:

$$ m(u-f )-\alpha \nabla \cdot({\nabla u\over \sqrt{|\nabla u|^2 +\varepsilon^2}})=0$$

And with our test function $v$ we can arrive at the weak form:

$$\int_{\Omega}m(u-f)v\mathrm{d}x+\alpha \int_{\Omega}{\nabla u \cdot \nabla v \over \sqrt{|\nabla u|^2+\varepsilon^2}}\mathrm{d}x=0 $$

this current demo adds a unit-square mesh generated with DOLFINx, a synthetic scalar image, an irregular mask, a nonlinear variational formulation in UFL, a Jacobian generated with ufl.derivative and a nonlinear solve using dolfinx.fem.petsc.NonlinearProblem

Notes

This is intended as a rough draft of the demo. I do intend to include plotting/diagnostics and more advanced geometry in a follow-up revision if that makes sense. Additionally more defined comments and documentation.

@jb2178-star
Copy link
Copy Markdown
Author

jb2178-star commented Apr 21, 2026

Added plotting with matplotlib for comparison of fields (u_true, mask, f, u, and difference). Matplotlib was chosen here for clarity in comparing multiple scalar fields. PyVista could be used for alternative visualization if preferred. Additionally added diagnostics for data fidelity, TV seminorm and reconstruction error.

@jb2178-star
Copy link
Copy Markdown
Author

Hi all,

I believe this PR is now ready for review. I have added clearer and more detailed documentation to better align with the style used in dolfinx. I can also include a more complete derivation of the weak form if that would be helpful. I have also added the nonlinear solver metrics such as:

  • initial vs final objective (optimization progress)
  • number of iterations of nonlinear iterations
  • final residual norm

As well as reconstruction metrics:

  • L2 error
  • hole error (error only in the missing regions)
  • PSNR (peak signal to noise ratio)
  • H1 seminorm error

I have also improved the visualization by including both global error and the hole only error plots.

This example demonstrates how to use dolfinx for nonlinear variational problems that may arise in image processing. In particular it demonstrates:

  • formulation of an energy function using UFL
  • differentiation of the jacobian via ufl.derviative
  • solution of the resulting nonlinear system using the integrated PETSc SNES
  • FEM assembly and discretization on a triangular mesh

This highlights dolfinx's ability to handle nonlinear, non-quadratic variational problems in a clear and concise way.

Thanks to @mscroggs for linking this PR to the relevant issue, and to @jorgensd for reviewing the original issue and providing feedback.

If there are any further suggestions or improvements, please let me know. Otherwise I believe this is now a complete documented inpainting demo ready for review.

@mscroggs
Copy link
Copy Markdown
Member

The CI lint checks are picking up some ruff warnings. You can fix many of these by running ruff check --fix - this command should also display the other warnings that need fixing to get the green tick

@jorgensd
Copy link
Copy Markdown
Member

@jb2178-star I've tried to do a major refactoring of the text (to make sure the docs render) and simplify the text a bit.
A follow-up question is: why not use ufl.derivative for getting the F?

@jb2178-star
Copy link
Copy Markdown
Author

Hi @jorgensd ,

I really appreciate you for refactoring , simplifying the text and taking the time to review this. It has been a really valuable learning experience for me, as this is my first time contributing to an open source project.

I initially wrote F with the intention to show the weak form explicitly. However I understand that using ufl.derivative to obtain the weak form directly is cleaner and more concise, and I'm happy to refactor it if you think that would be preferable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Python demo for variational image inpainting with smoothed TV regularization on irregular masks

3 participants