-convolve

A convolution of a kernel (or mask) and an input image arises from the traversal of the kernel over the image. At each step, the kernel's origin coincides with a select image pixel, with the kernel establishing a neigborhood over which the convolution takes place. The command multiplies kernel pixels with their image counterparts, giving rise to an array of intermediary products. The sum of these products sets the intensity of a pixel in the convolution image corresponding to the select image pixel. The shape of the kernel establishes the neighborhood and the pixel values comprising the kernel determine a particular kind of convolution.

Kernels are commonly small in relation to the input image, though they need not be. They have a local origin, which, in the G'MIC command, is at or near the center of the kernel and this aligns with the input pixel during each aggregation. Masks may consist of positive and negative values, so, in principle, any number of convolution effects are possible, though in practice there are a small number of standard masks that give rise to particular applications. Blurring, edge detection, image sharpening, simulated photographic bokeh and detail amplification are some of the more common ones.

The format of the command is:

-convolve [mask],_boundary,_is_normalized={ 0 | 1 }

mask: A (typically small) image, serving as the convolution kernel. In the absence of a limiting selection decorating the right hand side, the command will apply the mask to all images on the stack.

boundary policy: Near edges, the neighborhood may encompass 'off-image' pixels; this flag defines a default value for such. '0' (zero: 'dirichlet' ) regards these as zero-valued (black). '1' (unity: 'neumann') extends the values of edge pixels out to infinity so that the value of an off-image pixel is taken to be that of the nearest edge pixel.

normalize: Kernels typically have attendant image gain, so that the dynamic range of the convolution differs from the input image. This flag normalizes the convolution to have a dynamic range approximately from negative to positive one.

Application: Uniform Spline Basis Functions

The animation on the left illustrates the successive effects of a box filter on a one dimensional image 512 pixels wide. The top image is black but for a single intensity one pixel — an impulse — in the middle of the interval (red). The kernel, a so-called box filter of 64 constant, intensity one pixels, moves from left to right, evaluating each image pixel in turn (the box filter is blue).

The bottom image depicts the convolution of the box filter and the image with its single impulse: its pixels are successively set with the evaluation of the neighborhoods formed by the box filter as it moves from left to right. At the outset, with the filter situated wholly on black pixels, the sixty four intermediary products, each zero, aggregate to a sum of zero, so the corresponding pixels of the convolution are black.

When some part of the box filter coincides with the impulse, a non-zero result occurs among the intermediary products. At unity, the box filter pixel coincident with the impulse forms the only non-zero product in the intermediary, as, at unity, the impulse is the only non-zero pixel in the image, The aggregation of the entire neighborhood is therefore unity, a circumstance that necessarily prevails so long as the impulse is in the neighborhood of the box filter. When the kernel and impulse are again disjoint, the intermediary product array reverts to being entirely zero and, concomittantly, convolution pixels, as sums of the intermediary array, are again black. Thus, the convolution reflects all pixels that are within the box filter's width of the impulse, consequently forming an image of the box filter in the convolution. It is a quality of impulses to capture images of convolution kernels.

The animation goes on to illustrate successive convolution rounds using the same box filter, with the convolution arising from one stage serving as input to the next. In the second round, the box filter convolves with itself, giving rise to a hat function, a spline comprised of two linear segments. The convolution arising from the third stage is a three segment quadratic spline, each piece a degree two polynomial. The convolution arising from the fourth stage is a four segment cubic spline, each piece being a degree three polynomial. The degree elevation of these polynomial segments stems from the convolution of the box filter and the image, which, in its summing of areas under curves, finds the integral of the intensity function, the variation of luminance of the image. Suitably normalized, these polynomial splines serve as basis functions for splines that are defined over uniformly spaced knot sequences, establishing a connection between spline curves and convolutions.

Examples

The image on the far left contains three single pixel impulses on the middle row, one red, one green (in the middle of the image) and one blue. The chart just to the left graphs the middle row values, showing the three single pixels. 
We invoke the -convolve command on the previous image, employing a Goudy ampersand for a kernel, to which we are inordinately partial. Impulses echo kernels in the resulting convolution, producing here red, green and blue Goudy ampersands. Impulses by nature are not easily discerned in images; this convolution application is one that makes such features easily visible.

We pass the Goudy Ampersand kernel over the previous convolution, producing a second convolution. The graph on the left illustrates a complex and nuanced effect, a kind of a vibration of Goudy Ampersands.

An 8x8 Gaussian kernel blurs images uniformly. It is not uncommon to form an elliptic Gaussian kernel and rotate it, giving rise to a kind of "motion blur" similar to that made by a moving camera with a slow shutter.   
Taking a gradient of an eliptical Gaussian kernel produces a positive/negative pulse pair, an approximation of a Gabor filter. This filter reacts to edges oriented at the same angle as the ellipse. When tracing from upper right to lower left, the filter produces strong positive signals in dark-to-light transitions, and strong negative signals in light-to-dark transitions — an embossing effect.
An approximate Airy Disc with a positive central spike surrounded by a ring of negative values serves as an edge detector.  This particular kernel is somewhat removed from an ideal Airy disk, giving rise to some distortions which mimic a pencil. The convolution was inverted to give a semblance of dark marks on a white background.

Garry Osgood