[nipy] NIFTI to PNG using matplotlib - very distorted

48 views
Skip to first unread message

Daniel Geisler

unread,
Feb 4, 2013, 4:51:32 AM2/4/13
to nipy...@googlegroups.com
Hi guys!

I want to implement a very simple slice-tool (like FSL slicer) by using nipy and maplotlib. But i noticed a problem: the following lines will produce a png-image from a coregistered T1. Unfortunately the png is very distorted (see attached)


from nipy.io.api import load_image

from matplotlib.pyplot import imsave


img = load_image("T1.nii")

imsave('T1_slice.png', img.get_data()[300,:,:], cmap=cm.hot)


I guess that it has to do with voxel<-->world transformation. But i have no idea how to deal with it. Maybe some kind of resampling is necessary? For specific properties of the Nifti see below. I have no clue about how to solve this problem. Do you have?

Thanks,
Daniel
...........

>>> img.coordmap

AffineTransform(
   function_domain=CoordinateSystem(coord_names=('i', 'j', 'k'), name='voxels', coord_dtype=float64),
   function_range=CoordinateSystem(coord_names=('aligned-x=L->R', 'aligned-y=P->A', 'aligned-z=I->S'), name='aligned', coord_dtype=float64),
   affine=array([[  8.02220590e-03,   3.07039986e-03,   9.99852419e-01,
                   -9.29910736e+01],
                 [ -4.86986578e-01,   1.13107853e-01,   1.42397815e-02,
                    7.48955917e+01],
                 [  1.13047436e-01,   4.87028927e-01,  -9.61047225e-03,
                   -1.51170853e+02],
                 [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
                    1.00000000e+00]])
)


>>> print "\n".join(map(str, img.header.items()))

('sizeof_hdr', array(348, dtype=int32))
('data_type', array('',
      dtype='|S10'))
('db_name', array('',
      dtype='|S18'))
('extents', array(0, dtype=int32))
('session_error', array(0, dtype=int16))
('regular', array('',
      dtype='|S1'))
('dim_info', array(0, dtype=uint8))
('dim', array([  3, 448, 512, 176,   1,   1,   1,   1], dtype=int16))
('intent_p1', array(0.0, dtype=float32))
('intent_p2', array(0.0, dtype=float32))
('intent_p3', array(0.0, dtype=float32))
('intent_code', array(0, dtype=int16))
('datatype', array(4, dtype=int16))
('bitpix', array(16, dtype=int16))
('slice_start', array(0, dtype=int16))
('pixdim', array([-1. ,  0.5,  0.5,  1. ,  1. ,  1. ,  1. ,  1. ], dtype=float32))
('vox_offset', array(352.0, dtype=float32))
('scl_slope', array(1.5259021893143654e-05, dtype=float32))
('scl_inter', array(0.5000076293945312, dtype=float32))
('slice_end', array(0, dtype=int16))
('slice_code', array(0, dtype=uint8))
('xyzt_units', array(0, dtype=uint8))
('cal_max', array(0.0, dtype=float32))
('cal_min', array(0.0, dtype=float32))
('slice_duration', array(0.0, dtype=float32))
('toffset', array(0.0, dtype=float32))
('glmax', array(0, dtype=int32))
('glmin', array(0, dtype=int32))
('descrip', array('',
      dtype='|S80'))
('aux_file', array('',
      dtype='|S24'))
('qform_code', array(0, dtype=int16))
('sform_code', array(2, dtype=int16))
('quatern_b', array(0.44164982438087463, dtype=float32))
('quatern_c', array(-0.5478505492210388, dtype=float32))
('quatern_d', array(-0.4379926919937134, dtype=float32))
('qoffset_x', array(-92.99107360839844, dtype=float32))
('qoffset_y', array(74.89559173583984, dtype=float32))
('qoffset_z', array(-151.1708526611328, dtype=float32))
('srow_x', array([  8.02220590e-03,   3.07039986e-03,   9.99852419e-01,
        -9.29910736e+01], dtype=float32))
('srow_y', array([ -4.86986578e-01,   1.13107853e-01,   1.42397815e-02,
         7.48955917e+01], dtype=float32))
('srow_z', array([  1.13047436e-01,   4.87028927e-01,  -9.61047225e-03,
        -1.51170853e+02], dtype=float32))
('intent_name', array('',
      dtype='|S16'))
('magic', array('n+1',
      dtype='|S4'))

T1_slice.png
Reply all
Reply to author
Forward
0 new messages