1

I want to parse DICOM files in order to do some pixel processing. I tried DCMTK libraries but it's not working for me. I want something very simple, like a lightweight cross platform library in C++, since I just want read DICOM files.

Any suggestions will be greatly appreciated.

Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
Youssef Korchi
  • 127
  • 1
  • 4
  • 14
  • 1
    You might be able to get some help if you could be more specific than "it's not working". (Based on my limited knowledge of DICOM, "lightweight" is something you just won't find. Even DCMTK only claims to implement "large parts" of the standard.) – molbdnilo Jun 30 '16 at 09:49
  • what I meant by not working is that I didn' know how to use it , I've installed dcmtk.3.6.0 on ubuntu but didn't know where to start , how can I use it to read dcm files – Youssef Korchi Jun 30 '16 at 10:16
  • What about looking into DCMTK's documentation (e.g. here: http://support.dcmtk.org/docs-snapshot/mod_dcmdata.html#Examples) and/or into the numerous sample tools (in the "apps" subdirectories of the source code package). – J. Riesmeier Jun 30 '16 at 13:39
  • Commercial toolkit, very simple API: https://www.soft-gate.de/Portals/0/dokumente/bildverarbeitung%20und%20service%20software/DICOMconnect_eng.pdf – Markus Sabin Jun 30 '16 at 14:08
  • 1
    I would take a look at GDCM. If the ImageMagick code below works for you, it looks more lightweight, but if you need more DICOM info than the bits, use GDCM. – john elemans Jun 30 '16 at 18:15
  • I'm sorry for my late reply , I saw a DCMTK example here : http://support.dcmtk.org/docs/mod_dcmimgle.html , I'd like to use something like the given example in order to read pixels , I've configured and installed dcmTK and want to use it in a CLion c++ project under ubuntu , the only problem is how to link dcmtk libraries to my project in order to use their Classes like in the example – Youssef Korchi Jul 06 '16 at 04:16

1 Answers1

1

You can use ImageMagick to read DICOM files and it is free and cross-platform, generally installed on Linux distros and available for OSX and Windows.

Sample for Version 6.x follows...

////////////////////////////////////////////////////////////////////////////////
// sample.cpp
// Mark Setchell
//
// ImageMagick Magick++ sample code
//
// Compile with:
// g++ sample.cpp -o sample $(Magick++-config --cppflags --cxxflags --ldflags --libs)
////////////////////////////////////////////////////////////////////////////////
#include <Magick++.h> 
#include <iostream> 

using namespace std; 
using namespace Magick; 

int main(int argc,char **argv) 
{ 
   // Initialise ImageMagick library
   InitializeMagick(*argv);

   // Create Image object and read in DICOM image
   Image image("sample.dcm"); 

   // Get dimensions
   int w = image.columns();
   int h = image.rows();
   cout << "Dimensions: " << w << "x" << h << endl;

   PixelPacket *pixels = image.getPixels(0, 0, w, h);

   for(int y=0; y<h; y++){
      for(int x=0; x<w; x++){
         Color color = pixels[w * y + x];
         cout << x << "," << y << ":" << color.redQuantum() << "/" << color.greenQuantum() << "/" << color.blueQuantum() << endl;
      }
   }
}

Sample Output

Dimensions: 512x512
0,0:0/0/0
1,0:0/0/0
2,0:0/0/0
3,0:0/0/0
4,0:0/0/0
5,0:0/0/0
6,0:0/0/0
7,0:0/0/0
8,0:0/0/0
9,0:0/0/0
10,0:0/0/0
11,0:0/0/0
12,0:0/0/0
13,0:0/0/0
14,0:0/0/0
15,0:0/0/0
16,0:0/0/0
17,0:0/0/0
18,0:0/0/0
19,0:0/0/0
20,0:0/0/0
21,0:0/0/0
22,0:0/0/0
23,0:0/0/0
24,0:0/0/0
25,0:0/0/0
...
...
260,18:80/80/80
261,18:144/144/144
262,18:192/192/192
263,18:80/80/80
264,18:32/32/32
265,18:144/144/144
...
...

If you want to use Version 7.x, see Eric's technique here.

Or, at the command line in Terminal, you can convert a file to raw 8-bit RGB binary data like this:

# Convert 512x512 image to 8-bit RGB binary file
convert sample.dcm -depth 8 rgb:image.bin

ls -l image.bin
-rw-r--r--    1 mark  staff    786432 30 Jun 15:29 image.bin

You can hopefully see from the file size that the image is now 786,432 bytes, which is 3 bytes for each of the 512x512 pixels, so you can directly read the data into your C++ program knowing you will get:

RGB RGB RGB RGB ... RGB

Or, at the command line in Terminal, you can dump the image data as hex:

convert sample.dcm -depth 8 txt: | more

Sample Output

# ImageMagick pixel enumeration: 512,512,65535,gray
0,0: (0,0,0)  #000000  gray(0)
1,0: (0,0,0)  #000000  gray(0)
2,0: (0,0,0)  #000000  gray(0)
Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432