3

I have a 3D numpy array I want to iterate through. If it's important, this is a .nii filetype (file used to store MRI brain data) and I used the nipy module to load these images, which can then be handled as numpy arrays to do image processing. I want to take the and go through the voxels and only include the voxels that have value < 2. Here is my attempt

import nipy

import numpy   

img = nipy.load_image('image.nii.gz')

img_manip = img.get_data()

result = numpy.zeros(shape = img_manip.shape, dtype = img_manip.dtype) 

for matrix in img_manip:

    for row in matrix:

        for item in row:

            if item < 2:

                result += img_manip

This SEEMS to work,but it's extremely slow, like it's still running now. I'm just wondering, is this the right way to do it? Should I have used np.empty instead? I'm not sure I'm still pretty noob at python.

EDIT: Just an FYI, the shape of img_manip is something like (368, 170, 32) and data type is float64

(Sorry I don't know how to make the code look "pythonic"!)

1
  • your code does something like result = (img_manip < 2).sum() * img_manip that is different from img_manip[img_manip > 2] = 0 Commented Jun 21, 2013 at 7:08

2 Answers 2

3

I found a solution to my problem again! Haha, OK so it might not be PERFECT but it does the job. If anyone has a more elegant way of doing it please share! BTW this is not my solution, I actually asked the nipy mail list and they happily obliged to help me. Anywho, they suggested I take advantage of numpy's indexing system. So you would say:

img_manip[img_manip > 2] = 0
result = 15000 * img_manip #This is optional, just makes it into a nicer range for my purposes

Now for those interested, if you want to go back to .nii format, you could use the nifti package, see here, then you would simply do

new_img = nifti.NiftiImage(result)

And save your output!

EDIT: You could also use nibabel (and you probably should since it's being supported/developped further) by:

new_img = nib.NiftiImage(result)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Norman, I like your question. I have just started working with MR data. I am now iterating through a DICOM file which has 15000 slices which are actually different dimensions of 43 actual slices. Can I ask you what software you use and why you use nifti? So far I have only used ptyhon with its pydicom module. I might take a look at nipy as well.
Hi Leo. I use nifti because FSL's output is in nifti. To visualize I use Amira, itksnap and FSLview. I personally haven't tried my hand with Dicom images yet, though nibabel and pydicom can go hand in hand. If you'd like me to try out a task, I would be more than happy to! Oh I've been looking into another cool software called Slicer and it integrate well with python too
Ah I'll import nibabel as well then! Slicer sounds handy too. Im still getting acquainted with all the aspects of handeling such big datasets. I wont be making 3D reconstructions, so I probably wont use FSL. I just installed NeuroDebian on my system because our admin was still busy getting me a MatLab key, but Im actually quite excited about it (and about python). If you want to exchange ideas or experiences please feel free to email me! I filled out my email adress in my profile so it should be visible.
1

Here's a potential solution to the original question, using nibabel and numpy:

import nibabel as nib
import numpy as np
img = nib.load('image.nii.gz')
data = nib.get_data()
data[data>2] = np.nan  # If you really don't want to look at these...
nib.Nifti1Image(data, img.get_affine()).to_filename('new_image.nii.gz')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.