# 将浮点坐标添加到numpy数组

``````import numpy as np

arr = np.zeros((5, 5), dtype=float)

coord = [2, 2]
arr[coord[0], coord[1]] = 1

arr
>>> array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
``````

``````grid = np.meshgrid(*[np.arange(i) for i in arr.shape], indexing='ij')

out = np.exp(-np.dstack([(grid[i]-c)**2 for i, c in enumerate(coord)]).sum(axis=-1) / 0.5**2)
``````

``````from scipy.ndimage import center_of_mass

coord = [2.2, 1.7]

# get region coords
grid = np.meshgrid(*[range(2) for i in coord], indexing='ij')
# difference Euclidean distance between coords and coord
delta = np.linalg.norm(np.dstack([g-(c%1) for g, c, in zip(grid, coord)]), axis=-1)

value = 3 # pixel value of original coord
# create final array by 1/delta, ie. closer is weighted more
# normalise by sum of 1/delta
out = value * (1/delta) / (1/delta).sum()

out.sum()
>>> 3.0 # as expected

# but
center_of_mass(out)
>>> (0.34, 0.63) # should be (0.2, 0.7) in this case, ie. from coord
``````

``````arr = np.zeros((5, 5), dtype=float)

coord = [2.2, 0.7]

indexes = np.array([[x, y] for x in [int(coord[0]), int(coord[0])+1] for y in [int(coord[1]), int(coord[1])+1]])
values = [1. / (abs(coord[0]-index[0]) * abs(coord[1]-index[1])) for index in indexes]
sum_values = sum(values)
for (value, index) in zip(values, indexes):
arr[index[0], index[1]] = value / sum_values
print(arr)
print(center_of_mass(arr))
``````

``````[[0.   0.   0.   0.   0.  ]
[0.   0.   0.   0.   0.  ]
[0.   0.24 0.56 0.   0.  ]
[0.   0.06 0.14 0.   0.  ]
[0.   0.   0.   0.   0.  ]]
(2.2, 1.7)
``````

0 条评论