The new design is now the recommended default.
The test-code for the old design has the neat feature of propagating noise from the boundary into the interior, but I have not reproduced that in the test-code for the new design.
Current code requires at least
- Eigen-3.4
- Catch2-3.0 (for unit-tests)
Design under dirichlet
internally uses Eigen
throughly (almost no for-loops), minimizes
copying, and does not use std::map
, as old
design did for building coefficients for
sparse matrix.
The basic idea is to employ an interface
enabling speed and flexibility. The constructor
for
dirichlet::Fill
and the constructed function-object will do very
little copying. The function that solves the
Dirichlet-problem will copy the solution back
into the original image only if that image be
passed to constructor by way of non-const
pointer. Anyway, the solution, only for the
specified pixels, is returned by the
function-object.
Speed with -O3
is much faster than that of
old design with -O3
.
Conjugate-gradient solution is now available as
final, optional argument to constructor. It is
faster than Cholesky to construct instance of
Fill
on my test-image, but it is slower than
Cholesky to solve. Overall, CG is slower than
Cholesky on my test-image. It is turned off by
default for the moment. Nevertheless,
unit-tests all use conjugate-gradient and look
OK.
Here are the original image, the mask, the filled image produced by implementation of new design, and a histogram-equalized image zoomed in on the central, filled circle:
Suppose that "deep" and "shallow" are taken to refer to proximity to the boundary of the region to be filled. The deepest pixels are the ones farthest from the boundary of the region to be filled, and the shallowest are the ones nearest the boundary.
In a large region filled according to Laplace's equation, the pixel-values are most smoothly distributed across deepest portions of the region.
Because of the underlying geometry of the pixel-grid, an approach based on the bilinear interpolation over a square group of pixels seems natural. Although the bilinear interpolant is linear along each coordinate-axis, it is quadratic along any other direction. Anyway, the bilinear interpolant satisfies Laplace's equation. So, if the border of the square participate in a global solution to Laplace's equation, then the interpolant provides a local solution that is at least continuous with the global one.
What I propose is a hierarchical approach, in which the deepest portions of the region to be filled have the largest interpolated squares, shallower portions have smaller interpolated squares, and, in the shallowest portions, every individual pixel participates in a finite-element solution to Laplace's equation.
Consider an image
Extend
and, for the smallest integer
Similarly, extend
For each
Construct every successive, binned image of
-
$I_1$ and$M_1$ , each of which has$W_1=\frac{W_0}{2},$ has$H_1=\frac{H_0}{2},$ and is binned into superpixels, each of${2}\times{2}$ unbinned pixels; then -
$I_2$ and$M_2,$ each with superpixels of${4}\times{4}$ unbinned pixels; -
$I_3$ and$M_3,$ each with superpixels of${8}\times{8}$ unbinned pixels; etc.
In the case of
Construct
After construction of the binned images, next
consider them, beginning with
For each
The weights are generalized from those used in
the standard solution. A superpixel
After the solution has been found at the current
level of binning, suppose that a superpixel's
value applies at the center of the superpixel,
and, for each
Redo the binning for those interpolated pixels
from the bottom up in
Then proceed to
Finally, solve the Dirichlet problem for the remaining unbinned pixels.
When the size of the region to be filled is
larger than, say,
Application of the Dirichlet problem to the filling of a bounded region of an image.
Although I worked out the solution while waiting for my daughter to finish softball-practice, when I later searched for this technique, I found that (not surprising!) this had already been done. See Region Filling and Laplace's Equation.
Here are some images from that blog post:
My contribution is, first of all, to provide a solution that depends not at all on proprietary software like Matlab. Rather, I use C++ and Eigen. Second, I improve upon the approach of Eddins by using Laplace's equation in order to propagate high-spatial-frequency noise from the boundary into the region.
Here are some images from implementation of old design:
Copyright 2018-2022 Thomas E. Vaughan. See terms of redistribution in LICENSE.