Skip to content

Add smoothed TV inpainting demo#321

Open
jb2178-star wants to merge 1 commit intojorgensd:mainfrom
jb2178-star:tv-inpainting-demo
Open

Add smoothed TV inpainting demo#321
jb2178-star wants to merge 1 commit intojorgensd:mainfrom
jb2178-star:tv-inpainting-demo

Conversation

@jb2178-star
Copy link
Copy Markdown

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

Summary

This demo was originally proposed for the DOLFINx main demo repository, but following maintainer feedback, it is submitted here as a more application-focused example. This PR adds a demo illustrating the smoothed total variation (TV) image inpainting using DOLFINx. The example consideres a sythnetic image defined on the unit square with an irregular interior mask, and reconstructs missing data using a nonlinear variational formulation.

Starting from a variational inpainting model from Chan and Shen, we define the energy functional:

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

Where the first term enforces data fidelity on know regions, weighted with $\beta$, on the know regions with mask $m$, and the second term is a smoothed total variation regularization inspired by Rudin, Osher, Fatemi, weight with $\alpha$:

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

The smoothing parameter $\varepsilon>0$ in introduced to make the TV term differentiable, enabling the use of Newton methods (second order).

Taking the Euler-Lagrange equation associated with $J(u)$ yields the nonlinear PDE:

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

Which leads to the weak formulation: find $u\in V$ such that:

$$ \beta \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 \quad \forall v$$

The demo includes:

  • A unit square mesh generated with DOLFINx
  • A synthetic ground truth image and irregular mask
  • A nonlinear variational formulation in UFL
  • A residual and Jacobian via ufl.derivative
  • A nonlinear solve using dolfinx.fem.petcs.NonlinearProblem
  • Visualization of the reconstruction
  • Evaluation metrics to measure convergence and reconstruction quality

Notes

The example is intended as a tutorial style demonstration of using DOLFINx for nonlinear variational problems in imaging. Further extensions could be added such as solving for F, the weak form with ufl.derivative, although I'd prefer it remain explicitly in terms of the weak form to emphasize the connection between the mathematical model and its implementation.

@jb2178-star
Copy link
Copy Markdown
Author

Hi @jorgensd,

It seemed like chapter 3 would be the best place for this?

Thanks again.

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.

1 participant