Images
Conversion between finite element functions on 2D meshes and n × m pixel images, e.g. for image-based priors, learned regularisers or plotting. img[i, j] is the pixel in row i from the top and column j from the left of the bounding box. On quadrilateral meshes whose cells are the pixels (generate_grid(Quadrilateral, (m, n)) with piecewise constant σ), both directions are exact inverses. Theory: wiki article Pixel Images and Finite Element Functions.
disc = FerriteDiscretization(grid)
img = to_image(disc, σ, 128, 128) # σ coefficients → 128 × 128 image
σ2 = from_image(disc, img) # bilinear sampling at the nodes
σ3 = from_image(disc, img; method = :l2) # L² projection of the pixel image
im = ImageMap(disc, 128, 128; field = :u) # precompute once, reuse
u_img = to_image(im, u)ModularEITFerrite.ImageMap — Type
ImageMap(disc, n, m; field = :σ, bbox = nothing)Precomputed map between the field (:u or :σ) of a 2D discretization and n × m images (n rows along y, m columns along x) on the bounding box bbox = (xmin, xmax, ymin, ymax) (default: bounding box of the mesh). Row 1 is the top of the image. Fields: S (sparse, n m × ndofs, pixel centres in column-major order), inside (pixels whose centre lies in the mesh), nodes (coordinates of the nodes of the FE space, for sampling images).
See to_image and from_image.
ModularEITFerrite.to_image — Function
to_image(map::ImageMap, coeffs; outside = NaN)
to_image(disc, coeffs, n, m; field = :σ, bbox = nothing, outside = NaN)n × m image of the finite element function with coefficients coeffs, evaluated at the pixel centres (row 1 at the top, see ImageMap). Pixels outside the mesh get outside.
ModularEITFerrite.from_image — Function
from_image(map::ImageMap, img; method = :interpolate, quadrature_order = nothing)
from_image(disc, img; field = :σ, bbox = nothing, method = :interpolate, quadrature_order = nothing)Coefficients of a finite element function from an n × m image (same pixel convention as to_image):
method = :interpolate: bilinear interpolation of the pixel values at the nodes of the FE space (cell centroids for piecewise constants), linearly extrapolated within the outer half pixel. Exact for linear functions, and the inverse ofto_imageon pixel-aligned meshes.method = :l2: L² projection of the image, each pixel constant on its area. The pixels are sampled at the points of a quadrature rule; the default order grows with the number of pixels per cell (several points per pixel, capped at order 15 on triangles). The integral of the image is preserved up to this sampling error (exactly on pixel-aligned meshes). Smooths images that are finer than the mesh.
NaN pixels (outside the domain) are filled from their nearest finite neighbours first.
Unit images
Fixed-size arrays with values in $[0, 1]$, for example as network inputs or for data sets: a rectangle that contains the domain (square pixels by default), a linear or logarithmic value scaling (automatic, or a fixed range for a whole data set), a fill value and a mask for the pixels outside the domain, and the way back to coefficients.
ui = unit_image(disc, σ, 64, 64) # auto range, square pixels, 0 outside
ui = unit_image(disc, σ, 64, 64; range = (0.1, 10.0), scale = :log)
ui.image, ui.mask, ui.bbox, ui.range
σ_back = from_unit_image(disc, ui) # e.g. after denoising ui.imageModularEITFerrite.UnitImage — Type
UnitImageResult of unit_image: image (n × m, values in [0, 1] inside the domain, row 1 at the top), mask (pixels whose centre lies in the domain), bbox = (xmin, xmax, ymin, ymax) of the pixel grid, range = (lo, hi) and scale (:linear or :log) of the value mapping. Together they allow the way back, from_unit_image.
ModularEITFerrite.unit_image — Function
unit_image(disc, σ, n, m; bbox = nothing, pixels = :square, range = :auto, scale = :linear,
outside = 0.0, field = :σ)n × m array of the finite element function σ on a rectangle that contains the domain, scaled to [0, 1]:
bbox: the rectangle(xmin, xmax, ymin, ymax); default: the bounding box of the mesh. Withpixels = :squareits shorter side is widened symmetrically so that the pixels are square (the domain keeps its aspect ratio);pixels = :stretchkeeps the box.range::automaps the smallest and largest value ofσto 0 and 1 (a constantσmaps to 0); a fixed(lo, hi)gives the same scaling for a whole data set, values outside are clamped.scale = :logmapslog σlinearly (for conductivities spanning orders of magnitude; needs positive values).outside: value of the pixels whose centre lies outside the domain (see themask).
Pixels are evaluated at their centres (see to_image). Returns a UnitImage.
ModularEITFerrite.from_unit_image — Function
from_unit_image(disc, ui::UnitImage; field = :σ, method = :interpolate)
from_unit_image(disc, img, bbox, range; scale = :linear, field = :σ, method = :interpolate)Finite element coefficients from a unit image: the values are mapped back from [0, 1] to range (linearly or logarithmically) and sampled with from_image on the rectangle bbox (method = :interpolate or :l2). Pixels outside the domain are ignored. The inverse of unit_image, exact on pixel-aligned meshes when no values were clamped.
Wiki articles
Theory behind this page in the theory wiki: