简体   繁体   English

如何在Python中将图像数组转换为2D数组

[英]How can I convert an array of images to a 2D array in Python

I have a numpy array of images in that shape: 我有那种形状的图像的numpy数组:

(50000, 32, 32, 3)
  • 50000 is the number of images 50000是图像数量
  • 32, 32 are the height and width 32, 32是高度和宽度
  • 3 are the RGB values with a range of 0-1 30-1范围内的RGB值

I would like to convert it to a 2D shape of: 我想将其转换为2D形状:

(50000, 1024)

Here I would have 50000 images represented in one row, the RGB value would be converted to let's say an hexadecimal value I've went through a lot of conversion processes into stack overflow and I've found some. 在这里,我将在一行中表示50000张图像,将RGB值转换为一个十六进制值,我已经将许多转换过程转换为堆栈溢出并发现了一些值。 I know that if my array was a 3D array with an already converted value I could easily use reshape() function to convert it to 2D. 我知道,如果我的数组是具有已转换值的3D数组,我可以轻松地使用reshape()函数将其转换为2D。 Now what I'm searching is the easiest way to convert RGB values and reshape my array 现在,我正在搜索的是转换RGB值和调整数组形状的最简单方法

Would this be possible in 1 or two lines or should I use an external function? 1行或2行是否可能,还是应该使用外部功能?

First convert the RGB values in the last dimension to the HEX value using whatever function you like. 首先使用您喜欢的任何功能将最后一个维度中的RGB值转换为十六进制值。 This SO answer may help. 这样的答案可能会有所帮助。

Reshape then works on any number of dimensions: 然后重塑可在任意多个尺寸上使用:

import numpy as np

def rgb2hex(r, g, b):
    return '#%02x%02x%02x' % (r, g, b)

vfunc = np.vectorize(rgb2hex)

a = (np.random.uniform(0,1,(10,5,5,3))*255).astype(int)

c = vfunc(a[:,:,:,0], a[:,:,:,1], a[:,:,:,2])

c.reshape((10,25))

The following combines the RGB values into a single value 以下将RGB值合并为一个值

x=np.zeros((100,32,32,3))
x[:,:,:,0] = np.trunc(x[:,:,:,0]) + np.trunc(x[:,:,:,1] *256) + np.trunc(x[:,:,:,2] *65535)
y=x[:,:,:,0]
print(y.shape)

The resulting shape of y: (100, 32, 32) y的最终形状:(100,32,32)

Next you can use the reshape function on y. 接下来,您可以在y上使用重塑功能。

In order to do so, you firstly need to reshape the ndarray ( np.reshape ): 为此,您首先需要重塑ndarraynp.reshape ):

a = np.random.randint(1,10,(500, 32, 32, 3))
a_r = np.reshape(a, (500, 1024, 3))
print(a_r.shape)
# (500, 1024, 3)

Now, in order to convert the RGB values along the last dimension to hexadecimal representation as you suggest, you could define a function that returns a hexadecimal representation of the three values with a simple string formatting: 现在,为了按照建议将最后一个维度上的RGB值转换为十六进制表示,您可以定义一个函数,该函数以简单的字符串格式返回三个值的十六进制表示:

def rgb_to_hex(x):
    return '#{:02X}{:02X}{:02X}'.format(*rgb.reshape(3))

In order to apply the conversion along all rows in the last axis, you can use np.apply_along_axis : 为了将转换应用于最后一个轴上的所有行,可以使用np.apply_along_axis

a_new = np.apply_along_axis(rgb2hex, axis=-1, arr=a_r).shape
print(a_new.shape)
# (500, 1024)

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

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