How to do it...

  1. Import the modules:
import cv2import numpy as npimport matplotlib.pyplot as plt
  1. Draw a test image—a black circle (without filling) on a white background:
image = np.full((480, 640), 255, np.uint8)cv2.circle(image, (320, 240), 100, 0)
  1. Compute the distance from every point to the circle:
distmap = cv2.distanceTransform(image, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
  1. Visualize the results:
plt.figure()plt.imshow(distmap, cmap='gray')plt.show()

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.