1

I have a raw pixel data in a byte[] from a DICOM image. Now I would like to convert this byte[] to an Image object.

I tried:

Image img = Image.FromStream(new MemoryStream(byteArray));

but this is not working for me. What else should I be using ?

malat
  • 12,152
  • 13
  • 89
  • 158
prashant
  • 361
  • 9
  • 26
  • Can you please help me in getting the Image from the Pixel data of a DICOM file? I am getting the Pixel data but not know how to do the rest steps as mentioned in y_zyx's answer. Thanks in advance – SharpUrBrain Aug 13 '12 at 12:53

7 Answers7

3

One thing to be aware of is that a dicom "image" is not necessarily just image data. The dicom file format contains much more than raw image data. This may be where you're getting hung up. Consider checking out the dicom file standard which you should be able to find linked on the wikipedia article for dicom. This should help you figure out how to parse out the information you're actually interested in.

dustyburwell
  • 5,755
  • 2
  • 27
  • 34
  • Thanks for your help, Actually the raw pixel data i am getting from a third party tool. And the pixel data is which was apart from the tag information. When i use the above code with a jpeg file byte [] then everything is ok, but with dicom byte array it is saying invalid parameter. Please hep – prashant Apr 06 '09 at 13:53
  • If you are really getting just the pixel data and the data is for a single frame, then what you're doing should work. If you're getting multiple frames I'm not sure how the .Net Image library will react. Maybe try an open source DICOM library like http://imebra.com/ – dustyburwell Apr 06 '09 at 14:21
  • Just help me for 8 bit single frame images. No i was bound to use only our third party tools. And it is giving correctly a raw pixel data. – prashant Apr 07 '09 at 09:07
  • See this link it is some useful. I am doing something like this but not successful. http://stackoverflow.com/questions/337631/how-to-access-each-byte-in-a-bitmap-image thanks – prashant Apr 07 '09 at 09:10
  • ascalonx - Jpeg data in DICOM are encapsulated so i can't see how the bytearray of several frames comes in one concatenated piece – user88637 Apr 15 '09 at 11:50
2

You have to do the following

  1. Identify the PIXEL DATA tag from the file. You may use FileStream to read byte by byte.
  2. Read the pixel data
  3. Convert it to RGB
  4. Create a BitMap object from the RGB
  5. Use Graphics class to draw the BitMap on a panel.
y_zyx
  • 582
  • 4
  • 10
  • 28
  • I am getting the Pixel data but do not know how to do the rest steps as mentioned in yours answer. please help me in getting this, any sample code or idea will be appreciated, thanks in advance. – SharpUrBrain Aug 13 '12 at 12:54
1

You should use GDCM.

Grassroots DiCoM is a C++ library for DICOM medical files. It is automatically wrapped to python/C#/Java (using swig). It supports RAW, JPEG 8/12/16bits (lossy/lossless), JPEG 2000, JPEG-LS, RLE and deflated (zlib).

It is portable and is known to run on most system (Win32, linux, MacOSX).

See for example:

malat
  • 12,152
  • 13
  • 89
  • 158
1

The pixel data usually (if not always) ends up at the end of the DICOM data. If you can figure out width, height, stride and color depth, it should be doable to skip to the (7FE0,0010) data element value and just grab the succeeding bytes. This is the trick that most normal image viewers use when they show DICOM images.

doppelfish
  • 963
  • 7
  • 18
1

There is a C# library called EvilDicom (http://rexcardan.com/evildicom/) that can be used to pull the image out of a DICOM file. It has a tutorial on how to do it on the website.

Rex Cardan
  • 11
  • 2
0

DICOM is a ridiculous specification and I sincerely hope it gets overhauled in the near future. That said Offis has a software suite "DCMTK" which is fairly good at converting dicoms with the various popular encodings. Just trying to skip ahead in the file x-bytes will probably be fine for a single file but if you have a volume or several volumes a more robust strategy is in order. I used DCMTK's conversion code and just grabbed the image bits before they went into a pnm. The file you'll be looking for in DCMTK is dcm2pnm or possibly dcmj2pnm depending on the encoding scheme.

I had a problem with the scale window that I fixed with one of the runtime flags. DCMTK is open source and comes with fairly simple build instructions.

0

Are you working with a pure standard DICOM File? I've been maintainning a DICOM parser for over a two years and I came across some realy strange DICOM files that didn't completely fulfill the standard (companies implementing their "own" twisted standard DICOM files) . flush you byte array into a file and test whether your image viewer(irfanview, picassa or whatever) can show it. If your code is working with a normal JPEG stream then from my experience , 99.9999% chance that this simply because the file voilate the standard in some strange way ( and believe me , medical companies does that a lot)

Also note that DICOM standard support several variants of the JPEG standard . could be that the Bitmap class doesn't support the data you get from the DICOM file. Can you please write down the transfer syntax?

You are welcome to send me the file (if it's not big) yossi1981@gmail.com , I can check it out , There was a time I've been hex-editing DICOM file for a half a year.

user88637
  • 11,790
  • 9
  • 37
  • 36