Чтобы сделать конкретный пример:
In [168]: img = np.random.randint(0,2,(10,10))
In [169]: img
Out[169]:
array([[1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 0, 0, 1, 1, 1],
[1, 0, 0, 1, 0, 1, 1, 1, 0, 1],
[0, 1, 0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 1, 1, 0, 1, 1, 1, 0, 1, 1],
[1, 1, 0, 1, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 0, 1, 1, 0],
[0, 1, 0, 0, 1, 1, 1, 1, 0, 0]])
In [170]: idx = np.nonzero(img)
In [171]: idx
Out[171]:
(array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9]),
array([0, 1, 2, 3, 5, 6, 7, 9, 2, 4, 7, 8, 9, 0, 3, 5, 6, 7, 9, 1, 4, 6,
7, 2, 3, 4, 5, 8, 3, 7, 9, 0, 1, 2, 4, 5, 6, 8, 9, 0, 1, 3, 5, 6,
7, 8, 9, 0, 1, 3, 4, 5, 7, 8, 1, 4, 5, 6, 7]))
In [172]: jdx = np.nonzero((idx[0]>=3)&(idx[0]<=8)&(idx[1]>4)&(idx[1]<9))
In [173]: jdx
Out[173]: (array([21, 22, 26, 27, 29, 35, 36, 37, 42, 43, 44, 45, 51, 52, 53]),)
In [174]: idx1 = (idx[0][jdx],idx[1][jdx])
In [175]: idx1
Out[175]:
(array([3, 3, 4, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8]),
array([6, 7, 5, 8, 7, 5, 6, 8, 5, 6, 7, 8, 5, 7, 8]))
In [176]: img[idx1]
Out[176]: array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
И чтобы лучше визуализировать, какие точки мы выбрали:
In [177]: img[idx1] = 2
In [178]: img
Out[178]:
array([[1, 1, 1, 1, 0, 1, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 0, 0, 1, 1, 1],
[1, 0, 0, 1, 0, 1, 1, 1, 0, 1],
[0, 1, 0, 0, 1, 0, 2, 2, 0, 0],
[0, 0, 1, 1, 1, 2, 0, 0, 2, 0],
[0, 0, 0, 1, 0, 0, 0, 2, 0, 1],
[1, 1, 1, 0, 1, 2, 2, 0, 2, 1],
[1, 1, 0, 1, 0, 2, 2, 2, 2, 1],
[1, 1, 0, 1, 1, 2, 0, 2, 2, 0],
[0, 1, 0, 0, 1, 1, 1, 1, 0, 0]])
In [180]: img[3:9, 5:9]
Out[180]:
array([[0, 2, 2, 0],
[2, 0, 0, 2],
[0, 0, 2, 0],
[2, 2, 0, 2],
[2, 2, 2, 2],
[2, 0, 2, 2]])
In [182]: np.argwhere(img[3:9, 5:9]) # transpose of `nonzero`
Out[182]:
array([[0, 1],
[0, 2],
[1, 0],
...
[5, 2],
[5, 3]])
Применение среза среза, как предлагает @roganjosh, воссоздает idx1
массивы:
In [183]: np.argwhere(img[3:9, 5:9])+[3,5]
Out[183]:
array([[3, 6],
[3, 7],
[4, 5],
[4, 8],
[5, 7],
[6, 5],
[6, 6],
[6, 8],
[7, 5],
[7, 6],
[7, 7],
[7, 8],
[8, 5],
[8, 7],
[8, 8]])
Я не думаю, что имеет значение, какой маршрут вы выберете.