简体   繁体   中英

How to convert three 3D numpy arrays to RGB matrix in python

I'm not even sure if it is possible, but I am pretty new to python.

I have three 3D datasets, each is a 64 x 64 x 50 numpy array. I am trying to combine each 3D dataset into a single 3D RGB image, where each cell is represented by an RGB value, and each color channel represents values for a single dataset.

For example, my data is three different isotopes measured in a rock, so I would like R to represent the values for oxygen-16, G = sulfur-32, and B = magnesium-24.

I have figured out how to normalize each isotope array to a discretized value between 0-255 with the following generalized equation:

new_arr = ((arr - arr.min()) * (1/(arr.max() - arr.min()) * 255).astype('uint8')

More specifically for my data, I have the following:

O16R = ((O16.get_data() - np.min(O16.get_data())) * (1/(np.max(O16.get_data()) - np.min(O16.get_data())) * 255).astype('uint8'))

S32G = ((S32.get_data() - np.min(S32.get_data())) * (1/(np.max(S32.get_data()) - np.min(S32.get_data())) * 255).astype('uint8'))

Mg24B = ((Mg24.get_data() - np.min(Mg24.get_data())) * (1/(np.max(Mg24.get_data()) - np.min(Mg24.get_data())) * 255).astype('uint8'))

Now, I would like to create another 64 x 64 x 50 3D array, with each index in the array defined by the RGB values corresponding to the indexed values defined above.

For a simplified example, if I had small 2 x 1 arrays of:

O16R = (151, 3)

S32G = (2 , 57)

Mg24B = (0, 111)

Then I need a resulting RGB nested matrix with values:

RGB = ( [151,2,0] , [3,57,111] )

I figure that I need to create a for loop, but I haven't been able to figure it out. This is what I have so far, but it doesn't parse the data.

RGB = np.zeros(shape=(64,64,50)) 
for i in RGB:
    RGB = ([O16R, S32G, Mg24B])

Any help would be appreciated.

IIUC, for you minimal example you can do either of the following:

# setup:
O16R = (151, 3)
S32G = (2 , 57)
Mg24B = (0, 111)

# using zip:
RGB = np.array(list(zip(O16R, S32G, Mg24B)))
# or just transposing the array:
RGB = np.array([O16R, S32G, Mg24B]).T

Both return:

>>> RGB
array([[151,   2,   0],
       [  3,  57, 111]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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