Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting a 3D numpy array to coordinates and values

I have a 3D numpy array of shape (7,100,50) that represents a stack of 7 100×50 images.

I want to convert this array to a dataframe containing the position of all pixels x,y,z and the value of the pixel (id)

I have managed to do this for a single image (no z):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import numpy as np
import pandas as pd
img = np.random.randint(0,30,size=(100,50))
cell_id = img.flatten() 
x = [i % img.shape[1] for i in range(len(cell_id))]
y = [y_ for y_ in range(img.shape[1]) for _ in range(img.shape[0])]
df = pd.DataFrame(data={"id":cell_id, "x":x, "y":y, "z":0})
df:
    id  x   y   z
0   29  0   0   0
1   16  1   0   0
2   3   2   0   0
3   15  3   0   0
4   23  4   0   0
...     ...     ...     ...     ...
4995    7   45  49  0
4996    6   46  49  0
4997    1   47  49  0
4998    5   48  49  0
4999    7   49  49  0

5000 rows × 4 columns

How do I adjust this to work for

zimg = np.random.randint(0,30,size=(7,100,50))

?

>Solution :

I see you mentioned np.ndenumerate in another comment, this should do the trick:

import pandas as pd
import numpy as np

def constructor(array, z=0):
    """Transform an array into df

    Here we assume z=0 as in your example
    """
    for (img_id, y, x), value in np.ndenumerate(array):
            yield (img_id, value, x, y, z)

a = np.random.randint(0,30,size=(7,100,50))  

df = pd.DataFrame(
    constructor(a), 
    columns=('image_id', 'id', 'x', 'y', 'z')
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading