Принимая прямоугольную форму, вы можете использовать np.mgrid
, чтобы сгенерировать матрицу координат для точек между верхним левым и нижним правым углами.
X, Y = np.mgrid[xmin:xmax, ymin:ymax]
и преобразовать их вдвумерный массив координат с
np.vstack((X.ravel(), Y.ravel()))
РЕДАКТИРОВАТЬ: произвольные формы
Как отметил Марк Сетчелл, в вашем вопросе нет ничего такого, что говорило бы о прямоугольностиформы.
Если вы хотите перечислить все точки внутри произвольного пути, не обязательно из 4 вершин, вы можете использовать contains_points()
из matplotlib
Path
.
Вот код, полученный из другого моего ответа
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
import skimage.data
# path vertex coordinates
vertices = np.asarray([(100, 100),
(300, 80),
(350, 200),
( 60, 150)])
# create dummy image
img = skimage.data.chelsea()
# from vertices to a matplotlib path
path = Path(vertices)
xmin, ymin, xmax, ymax = np.asarray(path.get_extents(), dtype=int).ravel()
# create a mesh grid for the whole image, you could also limit the
# grid to the extents above, I'm creating a full grid for the plot below
x, y = np.mgrid[:img.shape[1], :img.shape[0]]
# mesh grid to a list of points
points = np.vstack((x.ravel(), y.ravel())).T
# select points included in the path
mask = path.contains_points(points)
path_points = points[np.where(mask)]
# reshape mask for display
img_mask = mask.reshape(x.shape).T
# now lets plot something to convince ourselves everything works
fig, ax = plt.subplots()
# masked image
ax.imshow(img * img_mask[..., None])
# a random sample from path_points
idx = np.random.choice(np.arange(path_points.shape[0]), 200)
ax.scatter(path_points[idx, 0], path_points[idx, 1], alpha=0.3, color='cyan')