Using the Distance Transform
The distance transform gives the distance-map representation of an image. It replaces each pixel with the Euclidean distance between the pixel and the nearest background pixel. It is used in various applications, such as blurring, pathfinding, motion planning in robotics, and so on. It can also be used to classify certain objects in an image as foreground or background by defining a threshold. The "foreground range" control in this Demonstration is an example of such a threshold where all the pixels above this threshold are treated as foreground.