Sto facendo qualche lavoro per cui devo caricare un manipolare immagini CT in un formato chiamato Analyze 7.5 file format.Caricamento immagini di formato 7.5 in python
Parte di questa manipolazione, che richiede anni senza problemi con immagini di grandi dimensioni, consiste nel caricare i dati binari non elaborati in una matrice numpy e ridisegnarla alle dimensioni corrette. Ecco un esempio:
headshape = (512,512,245) # The shape the image should be
headdata = np.fromfile("Analyze_CT_Head.img", dtype=np.int16) # loads the image as a flat array, 64225280 long. For testing, a large array of random numbers would do
head_shaped = np.zeros(shape=headshape) # Array to hold the reshaped data
# This set of loops is the problem
for ux in range(0, headshape[0]):
for uy in range(0, headshape[1]):
for uz in range(0, headshape[2]):
head_shaped[ux][uy][uz] = headdata[ux + headshape[0]*uy + (headshape[0]*headshape[1])*uz] # Note the weird indexing of the flat array - this is the pixel ordering I have to work with
So NumPy può fare rimodulazione degli array in fretta, ma io non riesco a capire la giusta combinazione di trasformazioni necessarie per replicare l'effetto dei cicli annidati.
C'è un modo per replicare quella indicizzazione strana con una combinazione di numpy.reshape/numpy.ravel ecc.?
Esattamente il codice che stavo cercando, grazie! – Theolodus