[ITK] [ITK-users] Accessing DICOM header/meta data with Python ITK

Michka Popoff michkapopoff at gmail.com
Sun Jun 29 05:24:03 EDT 2014


Hi

I made a patch adding the new feature [1].

The new syntax will be:
found, value = dicomIO.GetValueFromTag("0010|0010", "")

found is a bool telling you if the value/tag was found
value is the returned value. It’s a little bit different than in c++ were this method returns only the bool.

[1] http://review.source.kitware.com/#/c/15954/

Michka


On 29 juin 2014, at 03:58, Mathew Guilfoyle <mrguilfoyle at gmail.com> wrote:

> Thanks very much for looking into this; look forward to the fix.
> 
> Cheers
> 
> 
> On 27 June 2014 14:21, Michka Popoff <michkapopoff at gmail.com> wrote:
> Hi
> 
> I looked further into this, seems you found a bug.
> In fact we do not support passing strings by reference to a method (for the moment).
> I opened an issue on the bugtracker [1], you can register yourself and follow it.
> 
> I found a way to fix this, I just need to implement it in ITK (and see if it does not break the build).
> I will work on this during the weekend.
> 
> [1] https://issues.itk.org/jira/browse/ITK-3285
> 
> 
> Le 25 juin 2014 à 15:45, Michka Popoff a écrit :
> 
>> To get the label, I suppose you may want to use the GetLabelFromTag method. (I never used Dicom before, so I suppose it's what you want).
>> 
>> GetLabelFromTag and GetValueFromTag have the same problem ... they fail with the same error message.
>> It's because in C++ you pass the argument as a string by reference, which you can't in Python.
>> I don't know how Swig is wrapping this, so I'll have to investigate a little bit.
>> 
>> I'll look at this soon and I'll keep you updated.
>> 
>> 
>> Le 25 juin 2014 à 14:22, Mathew Guilfoyle a écrit :
>> 
>>> Fantastic!  
>>> 
>>> Is there a way of getting the label as well as the value of the entry? It would be useful for checking that the dictionary entry is the one expected from the tag.
>>> 
>>> Out of interest, do you know the correct way of using the dicomIO.GetValueFromTag() method?
>>> 
>>> Thanks again
>>> Mat
>>> 
>>> 
>>> On 25 June 2014 13:12, Michka Popoff <michkapopoff at gmail.com> wrote:
>>> Hi
>>> 
>>> I think you can directly access it "python style":
>>> 
>>> tilt = metad['0018|1120']
>>> 
>>> In fact, the itlMetaDataDictionnary class has a __getitem__ method, which downcasts directly the value [1].
>>> 
>>> [1] https://github.com/InsightSoftwareConsortium/ITK/blob/master/Wrapping/Generators/Python/PyBase/pyBase.i#L173
>>> 
>>> Le 25 juin 2014 à 13:35, Mathew Guilfoyle a écrit :
>>> 
>>> > Hi,
>>> >
>>> > I need some help with how to correctly access the DICOM meta data dictionary using Python wrapped ITK (v.4.5).  My CT scans are scanned with gantry tilt which I need to correct with a shear transform prior to segmenting.  The gantry tilt angle is stored in the DICOM header under tag (0018, 1120)
>>> >
>>> > example:
>>> >
>>> > import itk
>>> >
>>> > imdir = '/path/to/images'
>>> > image_t = itk.Image[itk.F, 3]
>>> >
>>> > #set up reader
>>> > reader = itk.ImageSeriesReader[image_t].New()
>>> > dicomIO = itk.GDCMImageIO.New()
>>> > dicomFN = itk.GDCMSeriesFileNames.New()
>>> > reader.SetImageIO(dicomIO)
>>> >
>>> > #get file names
>>> > dicomFN.SetUseSeriesDetails(True)
>>> > dicomFN.SetDirectory(imdir)
>>> >
>>> > #get the first image series
>>> > uids = dicomFN.GetSeriesUIDs()
>>> > fnames = dicomFN.GetFileNames(uids[0])
>>> >
>>> > #read in the files
>>> > reader.SetFileNames(fnames)
>>> > reader.Update()
>>> > image = reader.GetOutput()
>>> >
>>> > #getting the image works fine.  Now to access the meta data dictionary
>>> > metad = dicomIO.GetMetaDataDictionary()
>>> >
>>> > #get the entry for gantry tilt using the relevant DICOM tag (0018, 1120)
>>> > tilt = metad.Get('0018|1120')
>>> >
>>> > At this point I am stuck.  tilt is of type itkMetaDataObjectBase, rather than one the derived classes specific to strings, floats etc..  itkMetaDataObjectBase does not contain the GetMetaDataObjectValue() method, whereas the derived classes do.
>>> >
>>> > In the ITK manual, the C++ example uses a dynamic_cast at the type the tag is extracted so that 'tilt' would be of one the derived class types.  I have tried the following:
>>> >
>>> > sst = itk.MetaDataObject.SS
>>> > tilt.__class__ = sst
>>> > tilt.GetMetaDataObjectValue()
>>> >
>>> > casting tilt does work, and the GetMetaDataObjectValue() method is now present as one of tilt's attributes, but just returns an error if executed.
>>> >
>>> > The other approach I have tried is to use the dicomIO.GetValueFromTag() method.  From the source file the method signature is
>>> >
>>> > GetValueFromTag(self, string tag, string value)
>>> >
>>> > 'value' is, I presume, meant to be the name of the variable created containing the tilt angle.  However if I try:
>>> >
>>> > dicomIO.GetValueFromTag('0018|1120', 't_angle')
>>> >
>>> > I get the error
>>> >    in method 'itkGDCMImageIO_GetValueFromTag', argument 3 of type 'std::string &
>>> >
>>> > I would be tremendously grateful for any pointers on how to access the tag data - it must be a routine operation for people using Python/ITK on DICOM images!
>>> >
>>> > Many thanks
>>> > Mat
>>> >
>>> > _____________________________________
>>> > Powered by www.kitware.com
>>> >
>>> > Visit other Kitware open-source projects at
>>> > http://www.kitware.com/opensource/opensource.html
>>> >
>>> > Kitware offers ITK Training Courses, for more information visit:
>>> > http://www.kitware.com/products/protraining.php
>>> >
>>> > Please keep messages on-topic and check the ITK FAQ at:
>>> > http://www.itk.org/Wiki/ITK_FAQ
>>> >
>>> > Follow this link to subscribe/unsubscribe:
>>> > http://public.kitware.com/mailman/listinfo/insight-users
>>> 
>>> 
>> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20140629/1b163033/attachment-0002.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list