如何将元组列表转换为掩码?

时间:2016-04-14 02:08:50

标签: python numpy tuples

  • 想象一下,我有一个numpy数组(720,1280):

    grid = np.zeros((720, 1280))

  • 然后我有一个看起来像这样的元组列表:

    x_y_pairs_to_activate = [(241, 623), (390, 143), (313, 406)]

如何将该元组列表转换为掩码,以便激活相应的坐标?

grid[x_y_pairs_to_activate] = 1 # something like that

1 个答案:

答案 0 :(得分:2)

您需要转置坐标列表,以便在一个列表中包含所有第一个坐标,在另一个列表中包含所有第二个坐标。

x_coords = [c[0] for c in x_y_pairs_to_activate]
y_coords = [c[1] for c in x_y_pairs_to_activate]
grid[x_coords, y_coords] = 1

或更多“numpythonic”语言

mask = np.array(x_y_pairs_to_activate).transpose()
grid[mask] = 1