Skip to content Skip to sidebar Skip to footer

Add A Index Selected Numpy Array To Another Numpy Array With Overlapping Indices

I have two numpy arrays image and warped_image and indices arrays ix,iy. I need to add image to warped_image such that image[i,j] is added to warped_image[iy[i,j],ix[i,j]]. The bel

Solution 1:

Use numpy.add.at:

import numpy
image = numpy.array([[246,  50, 101], [116,   1, 113], [187, 110,  64]])
iy = numpy.array([[1, 0, 2], [1, 0, 2], [2, 2, 2]])
ix = numpy.array([[0, 2, 1], [1, 2, 0], [0, 1, 2]])
warped_image = numpy.zeros(shape=image.shape)

np.add.at(warped_image, (iy, ix), image)

print(warped_image)

Output

[[  0.   0.  51.]
 [246. 116.   0.]
 [300. 211.  64.]]

Post a Comment for "Add A Index Selected Numpy Array To Another Numpy Array With Overlapping Indices"