IMG · Images tools

Dilate and Erode Image

Shareable link

Settings are written to the URL as you change them. Nothing differs from the defaults yet.

Grow or shrink the shapes in an image by a set radius. Dilation and erosion are the two basic operations of mathematical morphology, and between them they cover a set of jobs that otherwise need a brush: thickening the lines of a scanned drawing, trimming the fringe off a cutout, closing the gaps in a mask, or clearing dust from a threshold.

Dilate, erode, open, close, gradient

Every pixel looks at its neighbours within the radius. Dilate keeps the brightest, so bright and opaque regions expand into their surroundings. Erode keeps the darkest, so they contract. Run one after the other and small things vanish while large ones keep their size: Open (erode, then dilate) drops bright specks smaller than the radius, Close (dilate, then erode) fills dark holes and joins broken lines. Gradient subtracts the eroded picture from the dilated one, which leaves a band the width of the radius along every edge.

Lines on paper

A pen drawing is dark ink on light paper, so the operation names run the other way from what the task sounds like: Erode makes the lines thicker, because the dark areas grow, and Dilate makes them thinner. To clean a scan, switch Work on to black and white, set the threshold so the paper is white and the ink black, and then Close with a small radius to mend the strokes the scanner broke.

Masks and cutouts

With Work on set to Transparency the operation runs on the alpha channel. Erode by half a percent takes the semi-transparent halo off a cutout from a background remover. Dilate grows a logo outward for a sticker border; the grown pixels take the colour of the nearest edge pixel, so the border is the logo’s own colour and not black. Open removes the stray specks a chroma key leaves in the screen, and Close fills pinholes in the subject.

Radius

Radius is a share of the shorter side, so 1 percent is 15 pixels on a 1500 pixel photo and the download matches the preview. The neighbourhood is a disc, so corners round off as they grow. On the transparency and black-and-white modes it is an exact disc with an anti-aliased edge, computed through a distance transform. On the colour channels it is a regular octagon, which runs in the same time at any radius where an exact disc would slow down with the square of it; at picture sizes the two are indistinguishable.

Colour channels

In the default mode each of the red, green and blue channels is filtered on its own, which is what the Maximum and Minimum filters in an editor do. On a photo this spreads highlights (Dilate) or shadows (Erode) into their surroundings, a blocky, painterly look at large radii.

Frequently Asked Questions

Dilate replaces every pixel with the brightest pixel within the radius, so bright and opaque areas grow. Erode takes the darkest, so dark and transparent areas grow. Open is an erode followed by a dilate, which removes bright specks; close is the reverse and fills dark gaps. Gradient is the dilated picture minus the eroded one, which leaves only the edges.

Lines are dark on a light page, so use Erode: the dark areas grow. Dilate makes them thinner. Set Work on to black and white if the scan has grey smudges, and raise the threshold until the paper reads as white.

Set Work on to Transparency. Dilate grows the opaque area, with the new pixels taking the colour of the nearest edge pixel, and Erode trims it. A 0.5 percent erode removes the halo a background remover leaves around hair and edges.

A share of the shorter side of the image, so the preview and the full-size download get the same result. 1 percent of a 2000 by 1500 pixel photo is a 15 pixel radius.

A disc, so corners grow into arcs rather than sharp squares. On the alpha and black-and-white modes it is an exact disc with an anti-aliased edge; on colour channels it is a regular octagon, which runs at the same speed whatever the radius and is indistinguishable at picture sizes.

Explore Our Tools

Browse all tools