【问题标题】:How to add item to each tuple element of numpy array?如何将项目添加到numpy数组的每个元组元素?
【发布时间】:2018-06-15 00:12:23
【问题描述】:

我有一个形状为 (512, 512, 4) 的 numpy 数组 A 每个元素都是一个元组:(r, g, b, a)。它代表一个 512x512 RGBA 图像。

我有一个形状为 (512, 512, 3) 的 numpy 数组 B 每个元素都是一个元组:(r, g, b)。它代表一个相似的 RGB 图像。

我想将 A 的每个元素中的所有“a”(alpha)值快速复制到 B 中的相应元素中。(基本上是传输 alpha 通道)。

生成的 B 形状将是 (512, 512, 4)。

我怎样才能做到这一点?该算法基于here 布局的快速像素操作技术。

代码:

## . input_image is loaded using PIL/pillow
rgb_image = input_image
print(f"Image: {rgb_image}")
rgb_image_array = np.asarray(rgb_image) # convert to numpy array
print(f"Image Array Shape: {rgb_image_array.shape}")

gray_image = rgb_image.convert("L") # convert to grayscale
print(f"Gray image: {gray_image}")
gray_image_array = np.asarray(gray_image)
print(f"Gray image shape: {gray_image_array.shape}")

out_image_array = np.zeros(rgb_image_array.shape, rgb_image_array.dtype)
print(f"Gray image array shape: {out_image_array.shape}")

rows, cols, items = out_image_array.shape

# create lookup table for each gray value to new rgb value
LUT = []
for i in range(256):
    color = gray_to_rgb(i / 256.0, positions, colors)
    LUT.append(color)

LUT = np.array(LUT, dtype=np.uint8)

print(f"LUT shape: {LUT.shape}")

# get final output that uses lookup table technique.
# notice that at this point, we don't have the alpha channel
out_image_array = LUT[gray_image_array]
print(f"output image shape: {out_image_array.shape}")

# How do I get the alpha channel back from rgb_image_array into out_image_array

输出:

Image: <PIL.Image.Image image mode=RGBA size=512x512 at 0x7FDEF5F2F438>
Image Array Shape: (512, 512, 4)
Gray image: <PIL.Image.Image image mode=L size=512x512 at 0x7FDEF5C25CF8>
Gray image shape: (512, 512)
Gray image array shape: (512, 512, 4)
LUT shape: (256, 3)
output image shape: (512, 512, 3)

【问题讨论】:

  • 请在您的问题中添加一个最小且可验证的示例以及您之前尝试过的代码。
  • @Kasramvd - 谢谢。我不知道如何实现它。我添加了更多解释/示例集。
  • 那么,这是一个元组列表吗?最小的样本看起来像这样。
  • 您将 Q 标记为 numpy,但您不使用 numpy 数组。那么,numpy 数组解决方案是否可以接受?
  • @NilsWerner 我用代码更新了这个问题。感谢您指出。

标签: python arrays numpy


【解决方案1】:

使用 numpy 切片:

import numpy as np

A = [[(1,1,1,4)], [(1,1,1,5)]]
B = [[(2,2,2)], [(3,3,3)]]

# A and B are tensors of order 3
A = np.array(A)  
B = np.array(B)


print("A=")
print(A)
print("B=")
print(B)

C = np.copy(A)

# assign along all 1st and 2nd dimensions, but only the first three elements of the third dimension
C[:,:,0:3] = B

print("C=")
print(C)

输出:

A=
[[[1 1 1 4]]

 [[1 1 1 5]]]
B=
[[[2 2 2]]

 [[3 3 3]]]
C=
[[[2 2 2 4]]

 [[3 3 3 5]]]

【讨论】:

  • 太棒了。这正是我所需要的。
【解决方案2】:

让我们注意术语

我有一个形状为 (512, 512, 4) 的 numpy 数组 A,每个元素都是一个元组:(r, g, b, a)。它代表一个 512x512 RGBA 图像。

如果 A 具有该形状,并且具有数字 dtype(例如 np.int32),则它具有 512*512*4 个元素。它可以拥有tuple 元素的唯一方法是dtype 是对象。我怀疑你有一个 512x512 的图像,其中每个像素由 4 个值表示。

A[0,0,:]

将是一个 (4,) 形状数组,表示一个像素的这 4 个值(有时称为通道)。

A[:,:,0]

是整个图像的r 值。

如果它们真的是 3d 数组,那么 @mocav 将列(在最后一维上的索引)复制到新数组的解决方案是正确的。

另一种可能性是它们是结构化的二维数组,分别具有 4 个和 3 个字段。这会将 (str) 打印为元组,尽管 repr 打印将使复合 dtype 显式。但解决方案将是相似的 - 创建一个具有正确形状和 dtype 的新数组(如A),并按字段名称从BA 复制值。 (在你澄清情况之前,我会等细节)。

【讨论】:

  • 用代码示例更新了 Q 以更好地查看形状。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2022-11-03
  • 1970-01-01
  • 2020-06-06
  • 2011-11-12
  • 1970-01-01
相关资源
最近更新 更多