简体   繁体   English

如何将项目添加到numpy数组的每个元组元素?

[英]How to add item to each tuple element of numpy array?

I have a numpy array A of shape (512, 512, 4) Each element is a tuple: (r, g, b, a). 我有一个形状为(512、512、4)的numpy数组A,每个元素都是一个元组:(r,g,b,a)。 It represents a 512x512 RGBA image. 它代表512x512 RGBA图像。

I have a numpy array B of shape (512, 512, 3) Each element is a tuple: (r, g, b). 我有一个形状为(512、512、3)的numpy数组B,每个元素都是一个元组:(r,g,b)。 It represents a similar, RGB image. 它代表一个相似的RGB图像。

I want to fast copy all the 'a' (alpha) values from each element of A into corresponding elements in B. (basically transferring the alpha channel). 我想将A的每个元素中的所有“ a”(alpha)值快速复制到B中的相应元素中(基本上是传递alpha通道)。

resulting B shape would be (512, 512, 4). 结果B形状为(512、512、4)。

How can I achieve this? 我该如何实现? The algorithm is based on fast pixel manipulation technique laid out here . 该算法基于此处提出的快速像素处理技术。

Code: 码:

## . 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

Output: 输出:

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)

Using numpy slices: 使用numpy slices:

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)

Output: 输出:

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]]]

Let's be careful about terminology 让我们注意术语

I have a numpy array A of shape (512, 512, 4) Each element is a tuple: (r, g, b, a). 我有一个形状为(512、512、4)的numpy数组A,每个元素都是一个元组:(r,g,b,a)。 It represents a 512x512 RGBA image. 它代表512x512 RGBA图像。

If A has that shape, and has a numeric dtype (eg np.int32 ), then it has 512*512*4 elements. 如果A具有形状,并且具有一个数值dtype (例如np.int32 ),那么它具有512×512×4个元素。 The only way it can have a tuple element is if the dtype was object. 它具有tuple元素的唯一方法是dtype为object。 I suspect rather that you have a 512x512 image where each pixel is represented by 4 values. 我怀疑您有一个512x512图像,其中每个像素由4个值表示。

A[0,0,:]

will be a (4,) shape array representing those 4 values (sometimes called channels) of one pixel. 将是一个(4,)形状数组,代表一个像素的这4个值(有时称为通道)。

A[:,:,0]

is the r value for the whole image. 是整个图像的r值。

If they really are 3d arrays, then @mocav's solution of copying columns (indexing on the last dimension) to a new array is the right one. 如果它们确实是3d数组,那么@mocav将列(在最后一个维度上索引)复制到新数组的解决方案是正确的。

Another possibility is that they are structured 2d arrays with 4 and 3 fields respectively. 另一种可能性是它们是分别具有4和3个场的2d阵列结构。 That would print ( str ) as tuples, though the repr print will make the compound dtype explicit. 尽管repr打印将使复合dtype显式显示,但这将把( str )打印为元组。 But the solution will be similar - make a new array of the right shape and dtype (like A ), and copy values by field name from B and A . 但是解决方案将是相似的-制作一个具有正确形状和dtype(如A )的新数组,并从BA按字段名称复制值。 (I'll wait with details until you clarify the situation). (我将等待细节,直到您弄清情况为止)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM