1

I am basically trying to read out all or most attribute values from a DICOM file, using the gdcm C++ library. I am having hard time to get out any non-string values. The gdcm examples generally assume I know the group/element numbers beforehand so I can use the Attribute template classes, but I have no need or interest in them, I just have to report all attribute names and values. Actually the values should go into an XML so I need a string representation. What I currently have is something like:

for (gdcm::DataSet::ConstIterator it = ds.Begin(); it!=ds.End(); ++it) {
    const gdcm::DataElement& elem = *it;
    if (elem.GetVR() != gdcm::VR::SQ) {
        const gdcm::Tag& tag = elem.GetTag();
        std::cout << dict.GetDictEntry(tag).GetKeyword() << ": ";
        std::cout << elem.GetValue() << "\n";
    }
}

It seems for numeric values like UL the output is something like "Loaded:4", presumably meaning that the library has loaded 4 bytes of data (an unsigned long). This is not helpful at all, how to get the actual value? I must be certainly overlooking something obvious.

From the examples it seems there is a gdcm::StringFilter class which is able to do that, but it seems it wants to search each element by itself in the DICOM file, which would make the algorithm complexity quadratic, this is certainly something I would like to avoid.

TIA Paavo

Paavo
  • 111
  • 1
  • 10

4 Answers4

1

Have you looked at gdcmdump? You can use it to output the DICOM file as text or XML. You can also look at the source to see how it does this.

michael pan
  • 589
  • 3
  • 10
1

I ended up with extracting parts of gdcm::StringFilter::ToStringPair() into a separate function. Seems to work well for simpler DCM files at least...

Paavo
  • 111
  • 1
  • 10
  • There is a very strong reason to use gdcm::StringFilter. You need to understand about CharacterSet handling. Do not extract function for no good reason. – malat Jul 17 '13 at 15:21
1

You could also start by reading the FAQ, in particular How do I convert an attribute value to a string ?

As explained there, you simply need to use gdcm::StringFilter:

sf = gdcm.StringFilter()
sf.SetFile(r.GetFile())
print sf.ToStringPair(gdcm.Tag(0x0028,0x0010))
malat
  • 12,152
  • 13
  • 89
  • 158
0

Try something like this:

gdcm::Reader reader;

reader.SetFileName( absSlicePath.c_str() );

if( !_reader.Read() )
{
    return;
} 

gdcm::File file = reader.GetFile();
gdcm::DataSet ds = file.GetDataSet();
std::stringstream strm;
strm << ds;

you get a stringstream containing all the DICOM tags-values. Actually, most of the DICOM classes (DataElement, DataSet, etc) have the std::ostream & operator<< (std::ostream &_os, const *Some_Class* &_val) overloaded. So you can just expand the for loop and use operator<< to put the values into the stringstream, and then into the string.

For example, if you are using QT :

ui->pTagsTxt->append(QString(strm.str().c_str()));

Uylenburgh
  • 1,277
  • 4
  • 20
  • 46