将索引选择的 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
中的同一元素?
请注意,我不想使用任何 for
循环。我想保持这个矢量化。我计划在未来将代码转换为 TensorFlow 或 PyTorch,以便为此使用 GPU 功能。那是因为,我有数百张这样的图片,而且每张图片都是全高清分辨率。
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.]])
对于上述情况,索引是唯一的,因此输出符合预期。
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.
上下文:
我正在尝试将图像从 view1 扭曲到 view2。我已经计算出哪个像素必须去哪里。如果像素重叠,我需要对它们进行加权平均。所以,我需要实现上述目标。更多详情 here
使用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 数组 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
中的同一元素?
请注意,我不想使用任何 for
循环。我想保持这个矢量化。我计划在未来将代码转换为 TensorFlow 或 PyTorch,以便为此使用 GPU 功能。那是因为,我有数百张这样的图片,而且每张图片都是全高清分辨率。
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.]])
对于上述情况,索引是唯一的,因此输出符合预期。
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.
上下文:
我正在尝试将图像从 view1 扭曲到 view2。我已经计算出哪个像素必须去哪里。如果像素重叠,我需要对它们进行加权平均。所以,我需要实现上述目标。更多详情 here
使用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.]]