Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Analyze 7.5 format images in python

I'm doing some work whereby I have to load an manipulate CT images in a format called the Analyze 7.5 file format.

Part of this manipulation - which takes absolutely ages with large images - is loading the raw binary data to a numpy array and reshaping it to the correct dimensions. Here is an example:

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

I know numpy can do reshaping of arrays quickly, but I can't figure out the correct combination of transformations needed to replicate the effect of the nested loops.

Is there a way to replicate that strange indexing with some combination of numpy.reshape/numpy.ravel etc?

like image 905
Theolodus Avatar asked Dec 16 '14 15:12

Theolodus


1 Answers

Take a look at the nibabel, a python library that implements readers/writers for the 'Analyze' format. It may have already solved this for you.

like image 92
arokem Avatar answered Nov 10 '22 22:11

arokem