Python Medical Image Conversion and File management

There are three main medical image formats used for projects and image datasets:

  1. DICOM, industry standard format. A 3D image is usually represented as a set of 2-D images in a directory. The format is a tagged file format. The tags give information on how to order the images into a 3D array.


  2. NifTi, this format is often used in image research. A 3D image is represented by a 3D file, it also always contains a 3D affine transform that may need to be managed.

3. vx is the format used in the VisionX v4 system. A 3D or 2D image is contained in a single vx file. It is the best format to use with the vsimba image viewer.

Image file format conversion utilities


Program

Function

dcmtoni

convert DICOM to NifTi

vnitovx

convert NifTi to vx

vdcptovx

convert DICOM to v4

vxtoni

convert vx to NifTi

dcminfo

view all the tags in a DICOM image or directory


Python programming

For reading and writing images use the packages pydicom for DICOM, nibabel for NifTi and v4 for vx. For most image analysis applications, the critical image meta data is the pixel

size and the pixel gray level offset. In python For DICOM(pydicom) and NifTi(nibabel) there are meta data functions in the packages.

Currently for meta data for vx files use the following python code:


def lparse ( row, ps ):

''' Parse a vx header row '''

ss = row.split(' ') for xx in ss:

yy=xx.split('=')

ps[yy[0]] = yy[1]


def dcm_meta(vximage):

''' Decode pixel size and offset for a DICOM derived vx image

'''

retval = {}

plist = vximage.h.split('\n') valid1=False

for st in plist:

if st[0:5] == "DICOM":

valid1=True

elif "xs=" == st[0:3]: lparse(st, retval)

strl = plist[-1] if len(strl) < 4:

strl = plist[-2]

#print ("strl is ",strl) if len(strl) < 4:

strl = plist[-3]

#print ("strl is ",strl) strl = strl.strip()

valid = (strl.find("vx.py write") < 4) or (strl.find("vdcmtovx ")<4) valid = valid or (strl.find("vdcme ")<4) or (strl.find("vbrand ")<4) retval['valid'] = valid and valid1

return retval


# Example use


a = vx.Vx('imdcp.vx')
print(dcm_meta(a))


{'valid': True,

'xs': '0.710938',

'ys': '0.710938',

'zs': '1.25',

'ri': '0.0',

'rs': '1.00'}

Create a v4 header with CT information


def dcm_ctmeta (vxim, meta):

''' create a CT vx header with DICOM information meta is a dict with some of xs ys zs ri rs st

'''

ctheader='''DICOM StudyUID=<1.2.3.4 >
SeriesUID=<1.2.3.5>i
Date=<20000101> Time=<000000>

so=<Unknown>

sss=2 ssa=1 ssi=1 dfn=1 nim=%s ist=%s
iss=0.000000 ip=0.000000
xs=%s ys=%s zs=%s ri=%s rs=%s
xn=%s yn=%s zn=%s modality=%s'''

shape = vxim.i.shape nd = len (shape)

xn = str(shape[2]) yn = str(shape[1]) zn = str(shape[0])

#need to handle color single images if (shape[0] <= 3 ):

xn = ys yn = zs zn = '1'

psize = {}

for i in ('xs','ys','zs', 'rs'):
if i in meta:

psize[i] = meta[i]
else
:

psize[i] = '1.0'

psize['ri'] = '0.0' if 'ri' not in meta else meta['ri']
psize['rs'] = '1.0' if 'rs' not in meta else meta['rs']
psize['st'] = psize['zs'] if 'st' not in meta else meta['st']

hmeta = (zn, psize['st'],

psize['xs'], psize['ys'], psize['zs'], psize['ri'], psize['rs'], xn,yn,zn, 'CT')

vxim.h=ctheader % hmeta vxim.cmnd = 'vdcme -brand'