import numpy as np def chebyshev_distance_transform(binary_image): # Create a distance map of the same size as the input image, initialized with large values distance_map = np.ones_like(binary_image) * np.inf # Step 1: Initialize the distance of background pixels to 0 distance_map[binary_image == 0] = 0 # Initialize the queue for pixel traversal queue = [] # Step 1: Initialize the distance of background pixels to 0 and add them to the queue for i in range(binary_image.shape[0]): for j in range(binary_image.shape[1]): if binary_image[i, j] == 0: distance_map[i, j] = 0 queue.append((i, j)) # Step 2: Iteratively update the distance values while queue: # Pop the pixel from the queue i, j = queue.pop(0) # Iterate over all pixels in the image for ni in range(binary_image.shape[0]): for nj in range(binary_image.shape[1]): # Compute Chebyshev distance distance = max(abs(ni - i), abs(nj - j)) # Update the distance if it's smaller than the current value if distance < distance_map[ni, nj]: distance_map[ni, nj] = distance return distance_map # Example binary image binary_image = np.array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]) # Compute Chebyshev Distance Transform distance_map = chebyshev_distance_transform(binary_image) print("Chebyshev Distance Transform:") print(distance_map)