← back to roto on ba3a.dev

Morphology

object Morphology

Binary morphology with a square structuring element of side 2 × radius + 1. A square is separable, so each operation runs along rows and then columns, with a sliding count that makes the cost independent of the radius.

Outside the mask is background. Opening is then the union of every square that fits inside the foreground, so a thick figure cut off by the frame edge keeps its edge; closing works on the mask padded by the radius and cropped back, so a figure near the edge is not glued to it.

Functions

Link copied to clipboard
fun close(mask: Mask, radius: Int): Mask

Closing, dilation then erosion: fills gaps and notches narrower than the square.

Link copied to clipboard
fun dilate(mask: Mask, radius: Int): Mask

A pixel becomes foreground when anything in the square around it is foreground.

Link copied to clipboard
fun erode(mask: Mask, radius: Int): Mask

A pixel stays foreground only when the whole square around it is foreground.

Link copied to clipboard
fun open(mask: Mask, radius: Int): Mask

Opening, erosion then dilation: removes specks and spurs narrower than the square.