How to get bottom left and bottom right points of boolean numpy mask

Viewed 568

I have a boolean mask numpy array of shape (1024,1024), which is a boolean mask of a person on a picture, like below. Now I would like to get the X and Y coordinates (indices in the array) of the bottom left-most point in the mask (marked in red), bottom-right most point in the mask (marked in blue). More challenging, I would like to get the indices of the green and pink points in the image below (top of the shoulders of the person on the image).

Is there any simple way to get the coordinates of these points?

boolean_mask

The sample code to get the mask in exactly the same format can be found below:

from skimage import io

image = io.imread('https://i.imgur.com/X9BKDtl.png')
image = image.astype(bool)
image = image[:, :, 0]
1 Answers

My naive approach would be to find the maximum of the sum x + y where x and y are coordinates on the image that equate to True. To get the bottom left index, we use x - y

Implementing this could be done by using numpy.where to get the indices:

import numpy as np
from skimage import io
import matplotlib.pyplot as plt


image = io.imread('https://i.imgur.com/X9BKDtl.png')
image = image.astype(bool)
image = image[:, :, 0]

x, y = np.where(image)  # get the indices (x, y) where the image is True

# Create a dictionary where the keys equate to the sum x + y
# or x - y respectively and the values are the corresponding
# x and y coordinates.

map_left = {x_i - y_i: (x_i, y_i) for x_i, y_i in zip(x, y)}
max_left = max(map_left.keys())
x_left, y_left = map_left[max_left]

map_right = {(x_i) + (y_i): (x_i, y_i) for x_i, y_i in zip(x, y)}
max_right = max(map_right.keys())
x_right, y_right = map_right[max_right]

plt.imshow(image)
# Note: x and y are swapped here due to the way the
# image is plotted
plt.plot(y_left, x_left, "x")
plt.plot(y_right, x_right, "x")

With this code I get the following result

Plot

Related