How do you get axis-wise indices from a detection mask?
nonzero returns one index array per axis. Entries at the same offset together identify one hit.
numpy.nonzero in practice
A panel lights up at top right and bottom left. Pair each row index with the corresponding column index. Sorting the two lists independently destroys those pairs.
import numpy as np
[a.tolist() for a in np.nonzero([[0,1],[1,0]])]Result [[0, 1], [1, 0]]
中文
怎样取得掩码命中位置的逐轴索引?
nonzero 为每个轴返回一个索引数组,同位置的索引合成一个命中坐标。
numpy.nonzero 算例
二维面板在右上、左下报警,输出的行数组与列数组需成对读取。分别排序两组索引会破坏这些坐标的配对。
import numpy as np
[a.tolist() for a in np.nonzero([[0,1],[1,0]])]结果 [[0, 1], [1, 0]]