Add smoothed TV inpainting demo#321
Conversation
|
Hi @jorgensd, It seemed like chapter 3 would be the best place for this? Thanks again. |
|
Hi again @jorgensd , Was there anything else that you though was need or need to be changed? Thanks |
Hi @jb2178-star, I've been quite busy lately and will hopefully have time to review this during this week. |
|
@jorgensd |
jorgensd
left a comment
There was a problem hiding this comment.
Some comments regarding both the variational formulation and the plotting.
I'll also push some general changes.
| grad_u = ufl.grad(u) | ||
| tv_denom = ufl.sqrt(ufl.inner(grad_u, grad_u) + eps**2) | ||
|
|
||
| F = beta * m * (u - f) * v * ufl.dx + alpha * ufl.inner(grad_u, ufl.grad(v)) / tv_denom * ufl.dx |
There was a problem hiding this comment.
Didn't we want to just define J and then use ufl.derivative(J, u, dv) to get F?
|
|
||
| msh.topology.create_connectivity(msh.topology.dim, 0) | ||
| cells = msh.topology.connectivity(msh.topology.dim, 0) | ||
| triangles = np.array(cells.array, dtype=np.int32).reshape(-1, 3) | ||
| triang = mtri.Triangulation(x, y, triangles) |
There was a problem hiding this comment.
This is not safe (or correct), as x and y stems from
coords = V.tabulate_dof_coordinates()
x, y = coords[:, 0], coords[:, 1]If your coordinates x and y is based on this you should use triangles = V.dofmap.list.
There was a problem hiding this comment.
Hi @jorgensd ,
Thank you so much for review and suggesting changes. Im working on the revisions, will update this.
There was a problem hiding this comment.
Hi @jorgensd,
Thanks for the review. I pushed a commit addressing the requested changes.
I defined the smoothed TV energy functional first and used ufl.derivative to obtain the residual form; updated the plotting triangulation to use V.dofmap.list with coordinates from V.tabulate_dof_coordinates; updated the notebook and script together; removed some outputs that I had used to tune alpha.
I also ran the demo successfully after the changes.
I again really appreciate the time you've taken with me during this process.
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:
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$ :
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:
Which leads to the weak formulation: find$u\in V$ such that:
The demo includes:
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.