将索引选定的 numpy 数组添加到另一个具有重叠索引的 numpy 数组 [英] Add a index selected numpy array to another numpy array with overlapping indices
问题描述
我有两个 numpy 数组 image
和 warped_image
和索引数组 ix,iy
.我需要将 image
添加到 warped_image
以便将 image[i,j]
添加到 warped_image[iy[i,j],ix[i,j]]
.如果 (iy[i,j], ix[i,j])
对对于所有 i,j
都是唯一的,则以下代码有效.但是当它们不是唯一的时,即当需要将 image
中的 2 个元素添加到 warped_image
中的同一元素时,只会添加其中一个.如何将 image
中的两个元素添加到 warped_image
中的同一个元素?
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 below code works if the pairs (iy[i,j], ix[i,j])
are unique for all i,j
. But when they are not unique i.e. when 2 elements from image
need to be added to the same element in warped_image
, only one of them gets added. How can I add both elements from image
to the same element in warped_image
?
请注意,我不想使用任何 for
循环.我想保持这个矢量化.我计划将来将代码转换为 TensorFlow 或 PyTorch,以便为此使用 GPU 功能.那是因为,我有数百张这样的图像,每张图像都是全高清分辨率.
Note that, I don't want to use any for
loops. I want to keep this vectorized. I'm planning to convert the code to TensorFlow or PyTorch in the future to use GPU capabilities for this. That's because, I have hundreds of such images and each image is of full HD resolution.
import numpy
image = numpy.array([[246, 50, 101], [116, 1, 113], [187, 110, 64]])
iy = numpy.array([[1, 0, 2], [1, 1, 0], [2, 0, 2]])
ix = numpy.array([[0, 2, 1], [1, 2, 0], [0, 1, 2]])
warped_image = numpy.zeros(shape=image.shape)
warped_image[iy, ix] += image
>> warped_image
Out[31]:
array([[ 113., 110., 50.],
[246., 116., 1.],
[187., 101., 64.]])
对于上述情况,索引是唯一的,因此输出符合预期.
For the above case, indices are unique and hence the output is as expected.
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)
warped_image[iy, ix] += image
>> warped_image
Out[32]:
array([[ 0., 0., 1.],
[246., 116., 0.],
[187., 110., 64.]])
预期输出:
array([[ 0., 0., 51.],
[246., 116., 0.],
[300., 211., 64.]])
在这种情况下,有 3 对索引重叠,因此失败.例如.image[0,1]
和 image[1,1]
应该将 gt 添加到 warped_image[0,2]
以给出值 51.然而,只有其中一个 (image[1,1]
) 被添加到值 1.
In this case, there are 3 pairs of indices which overlap and hence it fails. E.g. image[0,1]
and image[1,1]
should gt added to warped_image[0,2]
to give a value 51. However only one of them (image[1,1]
) gets added to give a value 1.
上下文:
我正在尝试将图像从 view1 扭曲到 view2.我已经计算出哪个像素必须去哪里.在重叠像素的情况下,我需要对它们进行加权平均.所以,我需要实现上述目标.更多详情此处
推荐答案
使用 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)
输出
[[ 0. 0. 51.]
[246. 116. 0.]
[300. 211. 64.]]
这篇关于将索引选定的 numpy 数组添加到另一个具有重叠索引的 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!