This command produces tensor fields from noisy input images. These encode smoothing geometries for the corrupted images, each field having the same dimensions as the input image. The tensors comprising each field are in a onetoone correspondence with the pixels of the input image.
The command implements the "anisotropic smoothing" operation usually attributed to smooth, one that strives to recover corrupted images (see the example on the left). Typically, smooth calls the internal implementation of diffusiontensors when it has not otherwise been given a particular smoothing geometry.
A script author usually calls diffusiontensors when she wishes to modify smoothing geometries in particular ways. See “Tensors for the Tonsorially Challenged” in the Cookbook for an example.
Belying a close relationship, the parameter list for diffusiontensors echoes many of smooth's. Indeed, smooth essentially passes such parameters unchanged to its internal copy of diffusiontensors. The command has the following format:
$gmic diffusiontensors sharpness>=0,0<=anisotropy<=1, alpha[%]>=0,sigma[%]>=0,is_sqrt={01}
sharpness: >= 0.0 Optional and defaults to 0.7. Increasing values diminish smoothing and preserves small scale detail. Settings in excess of 1.0 run the risk of suppressing smoothing too much, preserving noise as well as detail.
anisotropy: range: 1.0 ≥ a ≥ 0.0. Optional and defaults to 0.3. Induces greater smoothing along edge contours at the expense of crossgradient smoothing. Smaller anisotropy values induce a shift toward unidirectional diffusion; the effect tends to be more like conventional blurring. Anisotropy values near unity often causes "false edges" to be detected creating sinuous, hairlike artifacts.
alpha: Prestructure blur variance. Gaussian variance σ_{0} ≥ 0. Optional and defaults to 0.6. The command initially calls structuretensors to locate edges in images. Before doing so, diffusiontensors applies a conventional isotropic Gaussian blur to the operand image, which, to a degree, assists structuretensors with edge detection and finding edge orientation. Alpha sets the Gaussian variance, σ, for that operation. As it is based on isotropic blurring, too high a value of alpha can obliterate edges as well as noise.
sigma: Poststructure blur filter. Gaussian variance σ_{1} ≥ 0. Optional and defaults to 1.1. Following its call to structuretensors, diffusiontensors blurs the tensor field it obtains from that command; this parameter sets the variance, σ. For particularly noisy images, the tensor field produced by structuretensors can have spurious entries; blurring tends to reduce the effects of such outliers. Larger values of sigma tend to favor largescale laminar smoothing over localized 'eddy' smoothing.
is_sqrt: Boolean flag {01} Modifies the computation of the smoothing intensities in the gradient and contour directions. See "Smoothing Geometry," below, for further details.
For each pixel in a noisy image, diffusiontensors calculates a tensor embodying a "smoothing geometry" for that pixel. This tensor encodes two vectors, one directed along the gradient, the largest intensity change manifest in the locale of a pixel, and which is likely to be an edge, and the other along the contour of the (suspected) edge. These vectors are orthogonal to each other.
In the presence of noise, and with increased anisotropy settings, the smoothing vector along the contour is stronger than across the gradient. The operation tends to strengthen edges because diffusion proceeds more rapidly in directions parallel to edges, smearing noise pixels without smearing edge gradients.
The process is adaptive. Where intensity differences are great in a particular locale, indicating edges much stronger than the ambient noise, smoothing vectors are correspondingly small, an inverse proportional relationship. Strong edges do not require as much enhancement as degraded ones, and unnecessary smoothing would require more time and possibly introduce artifacts.
diffusiontensors calculates smoothing geometries from two sources: guidance parameters from the command line, in particular, sharpness, anisotropy, and is_sqrt, and a survey performed by structuretensors. The survey identifies "edgelike" structures from differences in image intensity in the locality of each pixel.
structuretensors itself produces a tensor field. The tensors comprising that field characterize the rapidity of change in image intensity along the cardinal directions (width and height) in the neighborhood of associated pixels. Pixels which straddle marked changes in intensity give rise to structure tensors with eigenvalues that differ greatly in value, their associated eigenvectors oriented along and transverse to the gradient.
diffusiontensors sets out to estimate corresponding smoothing tensors for each pixel, applying sharpness, anisotropy, is_root and the corresponding eigenvalues and eigenvectors computed from the structure tensors. At each pixel, smoothing tensors have the same orientation as the corresponding structure tensor but have different magnitudes, respectively s_{+} for smoothing strength in the gradient direction and s_{‒} for smoothing strength in the contour direction.
These new magnitudes have inverse power relationships with the corresponding eigenvalues of the structure tensors. Smoothing is very small where structure tensor eigenvalues exhibit large differences. This phenomenon corresponds to welldefined edges in the source image and these are not regions where one needs a great deal of edge enhancement. As the absolute sizes and differences of structure tensor eigenvalues decline, gradient and contour smoothing magnitudes increase. This is natural, as the smaller eigenvalues correspond to less distinct features which smoothing might profitably enhance.
We could regard s_{+} and s_{‒} as the axes of an elliptic diffusion kernel; differences in their magnitudes induces ellipses of greater eccentricity and a correspondingly greater directional effect in the diffusion pattern. The command line parameters which set the overall smoothing magnitude and effect a differentiation between s_{+} and s_{‒} are, respectively, sharpness and anisotropy.
Given are:
With these given parameters, the following relationships calculate smoothness values s_{+} and s_{‒}:
Exponents 

$${p}_{1}=\left\{\begin{array}{c}\frac{\text{sharpness}}{2};\text{is\_sqrt}>0\\ \text{sharpness};\text{is\_sqrt}=0\end{array}\right\}$$  $${p}_{2}=\frac{{p}_{1}}{1\text{anisotropy}}$$ 
Contour and Gradient Smoothing 

$${s}_{\text{}}=\frac{1}{{(1+{\mathrm{\lambda}}_{\text{+}}+{\mathrm{\lambda}}_{\text{}})}^{{p}_{1}}}$$  $${s}_{\text{+}}=\frac{1}{{(1+{\mathrm{\lambda}}_{\text{+}}+{\mathrm{\lambda}}_{\text{}})}^{{p}_{2}}}$$ 
To help develop a concrete sense of how diffusiontensors operates, we trace histories of four pixels through an image reconstruction pipeline mediated by this command and smooth. Apart from the action of smooth, this narrative is internal to the diffusiontensors command. Some readers may wish to refer to the command's definition in gmic_def.gmic.
Original picture. We mark four reference pixels. From top to bottom:


The same picture, now corrupted with noise at a variety of scales. We apply the plasma command with iterations through two scales, dependent perturbations (alpha) of seventeen and scale independent perturbations (beta) of fourteen. We overlay a Gaussian noise with a variance of thirty. Our trial diffusiontensor parameters are:


diffusiontensors calls structuretensors to locate the strength and orientation of image features.
Here are the tensors which structuretensors calculated for our reference points, reflecting a local averaging from the second blurring operation (sigma):


While a compact representation, tensors do not lend themselves to easy visualization. Here, we solve for their eigenvalues and eigenvectors and overlay the data onto their structure tensor field locales. In each case, the gradient vector is the larger of the two and is directed along the most rapid change in intensity; the companion contour vector is barely visible at this scale. As noted in the text, the magnitude of the difference between λ_{+} and λ_{\u2500 }signal edges, with the contour vector running along it and the gradient vector crossing it.


Given structure tensors and command parameters, we compute the smoothing magnitudes for the four points, harnessing the relationships that are given in the previous section. These magnitudes scale unit vectors with the same orientation as the input structure tensors and form smoothing vectors. The diagrams on the left depict the smoothing vectors at each of the reference points and are drawn at a larger scale than the previous diagram, approximately at the ratio of 2,421:1. These vectors instruct smooth to diffuse the associated image pixel in an anisotropic way. In the first case, at pixel (155,51), diffusion will proceed at the relative magnitude of 0.03123 along an orientation of 47.1734°. The amplitude parameter given to smooth scales these magnitudes to their working values. The overall tensor field embodies similar instructions for every pixel in the corrupted image, and, in aggregate constitute a smoothing geometry. The smoothing vectors for the four reference pixels are:


diffusiontensors calls eigen2tensors. This command recomposes tensor fields from image pairs. The pair consists of a two channel image of eigenvalues and a two channel image of unit vectors; it produces a single tensor field encoding a smoothing geometry. This is passed as a parameter to smooth. In the present case, the two channel eigenvalue image contains the smoothing values; the unit vectors have been carried forward unchanged from the tensor field produced by structuretensors. This operation gives rise to diffusion tensors for each of the reference points:


The smooth command blurs images through diffusion under the control of a given tensor field. In a sense, this command places over each pixel in the image an elliptic diffusion kernel, taken from the tensor field and which has been tailored to the pixel's immediate environment by diffusiontensors. This gives rise to three broad behaviors:
In practice, we place the smooth command in a repeat 5 …done loop, so that we may apply the diffusion map to a number of intermediary stages. For each stage we use:

Neither diffusiontensors nor smooth can conjure image information wholly lost to noise. We harnessed plasma to corrupt the image, which blurs as well as perturbs, so, from a spectral point of view, part of the image was destroyed by a low pass filter. Gone irretrievably are sharp corners and edges.
Within those limitations, diffusiontensors recovered almost all information still manifest in the image. We could filter out most of the saltandpepper racket and recover areas of constant tone. Our reconstructed image looks like a somewhat blurred version of the original, the variance, one speculates, not too different from the blurring component in plasma.
Comparing a magnified bit of the output with the pristine original illustrates some of the inner workings of diffusiontensor and smooth. The edges of the smoothed image seem to have shadowy, thin lines, quite unlike the softer regions in the interiors of shapes. These are footprints of very elliptical smoothing kernels, diffusing their pixels along thin, edge hugging paths. If the image hadn't been so terribly shot up with noise, these artifacts would be not nearly so manifest. Had we pushed the anisotropy parameter near its limits of one, these artifacts would start to look a little bit like curling hair strands; often these look quite lovely.
Garry Osgood