如何将一个numpy数组中的值放入另一个数组中,并由第三个数组指定索引(奖励,索引是浮点数)

wvyml7n5  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(84)

我编写了代码,提取图像的一部分,并将其保存为第二个图像沿着将新图像中的每个像素Map到其原始位置的数组。新图像不是从原始图像裁剪的矩形,并且不是可以经历简单变换回到原始形状的东西。示例:

new_img = np.zeros((5,5,3))
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.]]])
poorly_shaped_img = np.random.random((3,2,3))
array([[[0.56996145, 0.66969092, 0.39299133],
        [0.71405233, 0.81291533, 0.84920548]],
       [[0.75519416, 0.25214197, 0.45477072],
        [0.05180197, 0.64204567, 0.81518423]],
       [[0.91130947, 0.69654691, 0.99531231],
        [0.07430683, 0.18580811, 0.66368306]]])
rounded_pixel_map:
array([[[0,0],[0,2]],
       [[1,4],[2,4]],
       [[0,3],[4,2]]])
desired_output:
array([[[0.56996145, 0.66969092, 0.39299133],
        [0., 0., 0.],
        [0.71405233, 0.81291533, 0.84920548],
        [0.91130947, 0.69654691, 0.99531231],
        [0., 0., 0.]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0.75519416, 0.25214197, 0.45477072]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0.05180197, 0.64204567, 0.81518423]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],
       [[0., 0., 0.],
        [0., 0., 0.],
        [0.07430683, 0.18580811, 0.66368306],
        [0., 0., 0.],
        [0., 0., 0.]]])

字符串
有没有什么方法可以将这些值放回它们所属的位置而不循环遍历它们?如果我在上面循环,它看起来会像这样:

for i in range(len(rounded_pixel_map)):
    for j in range(len(rounded_pixel_map[i])):
        pixel_coords = rounded_pixel_map[i][j]
        pixel_value = poorly_shaped_img[i][j]
        new_img[pixel_coords[0], pixel_coords[1]] = pixel_value


即使这只是真正问题的一部分。在生成这个“形状不佳的图像”时,我使用了双线性插值,因为我经常需要非整数像素的值。像素Map数组充满了浮点数,所以我还需要一种“反向插值”的方法来获得整数索引处的正确值。
该循环选项的问题在于性能。这些形状不佳的图像是4096 x 80像素,因此以这种方式循环根本没有性能。我不知道如何应用“反向插值”从浮点索引到整数索引的正确值。

h9a6wy2h

h9a6wy2h1#

如果你想避免显式循环并提高性能,你可以利用NumPy的高级索引和广播功能来实现你想要的结果。以下是如何修改代码以避免循环:

import numpy as np

new_img = np.zeros((5, 5, 3))
poorly_shaped_img = np.random.random((3, 2, 3))
rounded_pixel_map = np.array([[[0, 0], [0, 2]],
                              [[1, 4], [2, 4]],
                              [[0, 3], [4, 2]]])

# Reshape rounded_pixel_map to match the dimensions of poorly_shaped_img
reshaped_map = rounded_pixel_map[:, :, np.newaxis, :]

# Index new_img using reshaped_map to assign pixel values
new_img[reshaped_map[:, :, :, 0], reshaped_map[:, :, :, 1]] = poorly_shaped_img

print(new_img)

字符串

whitzsjs

whitzsjs2#

一种方法是

idx=rounded_pixel_map.reshape(-1,2)
new_img[idx[:,0], idx[:,1]] = poorly_shaped_img.reshape(-1,3)

字符串
说明:我真的不需要关心索引的形状。关键是,如果我按顺序读取它们(即按C顺序,但这并不重要,只要reshape对indexpoorly_shaped_array做同样的事情),那么我必须将它们存储在这些地方。
因此,这只是一个扁平化索引和源图像的问题。除了我们希望索引的扁平化保持一个坐标数组(所以成对),我们希望图像的扁平化保持像素(所以三倍)。因此,除最后一个(尺寸为2的轴)外的所有部分的展平为reshape(-1,2),除最后一个(尺寸为3的轴)外的所有部分的展平为reshape(-1,3)

相关问题