Connected Components
object ConnectedComponents
Connected regions of a Mask. Foreground connects through all eight neighbours and background through the four edge neighbours — the usual pairing, so a diagonal chain of foreground pixels is one region and background never leaks through it.