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.