gpt4 book ai didi

python - 如何获得 bool numpy掩码的左下角和右下角点

转载 作者:行者123 更新时间:2023-12-04 04:02:00 24 4
gpt4 key购买 nike

我有一个形状为 (1024,1024) 的 bool 掩码 numpy 数组,它是图片上人物的 bool 掩码,如下所示。现在我想获取掩码中最左下角的点(标记为红色)和掩码中最右下角的点(标记为蓝色)的 X 和 Y 坐标(数组中的索引)。更具挑战性的是,我想获得下图中绿色点和粉色点的索引(图像上人的肩膀顶部)。

有什么简单的方法可以得到这些点的坐标吗?

boolean_mask

可以在下面找到以完全相同的格式获取掩码的示例代码:

from skimage import io

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

最佳答案

我天真的方法是找到和 x + y 的最大值,其中 x 和 y 是图像上等于 True 的坐标。要获得左下角的索引,我们使用 x - y

可以通过使用 numpy.where 获取索引来实现这一点:

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")

使用这段代码我得到了以下结果

Plot

关于python - 如何获得 bool numpy掩码的左下角和右下角点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62937848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com