我有一个零的二维numpy数组,它们代表某个平坦的表面:
field = np.zeros((10,10))
field
Out[165]:
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
然后我有一个坐标数组,其格式为[row,column]
,例如:
In [166]:c = np.array([[1,2],[4,5],[7,3],[2,6]])
In [167]:c
Out[167]:
array([[1, 2],
[4, 5],
[7, 3],
[2, 6]])
我想做的是在c中的坐标周围填充field数组的一个块。
Out[192]:
array([[0., 1., 1., 1., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0., 1., 1., 1., 0., 0.],
[0., 1., 1., 1., 0., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 0., 0., 1., 1., 1., 0., 0., 0.],
[0., 0., 1., 1., 1., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
我最初使用numpy向量化的尝试是:
In [168]:field[c[:,0]-1:c[:,0]+1,c[:,1]-1:c[:,1]+1] = 10
Traceback (most recent call last):
File "<ipython-input-168-5433a2f4a5cf>", line 1, in <module>
field[c[:,0]-1:c[:,0]+1,c[:,1]-1:c[:,1]+1] = 10
TypeError: only integer scalar arrays can be converted to a scalar index
然后我尝试先先创建c[:,0]-1
和c[:,1]+1
数组,但遇到相同的错误,这使我得出这样的结论,即在numpy中无法完成这种类型的范围索引。
我也看过np.ix_(),但是如果没有使用此方法的for循环,就无法设置多个坐标的周围块。
但是我可以使用for循环实现所需的输出:
for row,column in c:
field[row-1:row+2,column-1:column+2] = 1
但不希望使用for循环,因为在我的最终应用程序中c和f都将是大型多维的,我觉得我可以利用numpy向量化带来的速度改进。
此外,我知道在图像处理中,这可以看作是膨胀或腐蚀问题,但我已经具有将腐蚀/膨胀内核一次又一次地放置在多个维度和非常大的阵列上的坐标。
这是一种简单的方法,只有少量的Python循环和大量的矢量化工作:
x, y = c[:,0], c[:,1]
for i in -1,0,1:
for j in -1,0,1:
field[x+i,y+j] = 1
以这种方式尝试:
for x in c:
xcoordinates_to_fill=[x[0]-1,x[0],x[0]+1]
ycoordinates_to_fill=[x[1]-1,x[1],x[1]+1]
for i in xcoordinates_to_fill:
for j in ycoordinates_to_fill:
if i >= 0 and j >= 0:
field[i,j] = 1