- Import the modules:
import cv2import numpy as npimport matplotlib.pyplot as plt
- 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)
- Compute the distance from every point to the circle:
distmap = cv2.distanceTransform(image, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
- Visualize the results:
plt.figure()plt.imshow(distmap, cmap='gray')plt.show()