How it works...

Distance maps can be calculated using the OpenCV cv2.distanceTransform function. It calculates the specified type of distance (cv2.DIST_L1, cv2.DIST_L2, or cv2.DIST_C) to the closest zero pixel. You can also vary the mask size that's used for computing the approximate distance (the available options are cv2.DIST_MASK_3 and cv2.DIST_MASK_5). You can also use the cv2.DIST_MASK_PRECISE flag, which leads to computing not approximate, but precise distances.

The following output is expected:

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.