dicom / ruby-dicom

Library for reading, editing and writing DICOM files, as well as handling DICOM network communication. Written in the Ruby language.
http://dicom.rubyforge.org/
GNU General Public License v3.0
178 stars 68 forks source link

Full jpg decompression support #45

Open dicom opened 11 years ago

dicom commented 11 years ago

Currently, the ruby-dicom library is only able to handle a small subset of the compressed transfer syntaxes allowed in the DICOM standard. For Run Length Encoding, we have a straight ruby implementation, and for jpg encoded image data, we try to use RMagick to decode the images. This works fine for some of the 'simpler' jpg variants, however, surprisingly enough, it seems that RMagick (in reality ImageMagick) is not able to handle all of the JPG variants. For example the following 3 variants have been shown to fail:

It would be awesome if ruby-dicom were able to decompress the pixel data in all DICOM files, no matter the compression algorithm used. What would be a good way to achieve this?

Look at how other open source libraries, e.g. GDCM, dcmtk, etc do it?

Use an alternative JPEG library? Some time ago I briefly checked out the PVRG-JPEG library, which seems to handle everything that is jpeg. It exists in the Ubuntu repositories, where it can be installed and run through terminal. For source code, and some additional information, check out this link: http://www.panix.com/~eli/jpeg/

Any suggestions, or even better yet, code contributions, are welcome!

dicom commented 11 years ago

It seems that it is actually possible to compile ImageMagick to support these more obscure jpeg variants that it doesn't support out of the box, by including some custom 'delegate' jpeg libraries. For reference, have a look at a discussion of the topic here:

http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=23249

If anyone is successful in building ImageMagick to enable these files, e.g. this one (https://github.com/dicom/ruby-dicom/raw/master/samples/explicit_ct_jpeg-lossless-nh_mono2.dcm), it would be awesome if you could post your recipe here.

nark commented 10 years ago

Hi,

Just to point this page: https://code.google.com/p/openjpeg/wiki/GSOC_2013 where users are talking about openjpeg bindings for ImageMagick delegate. Openjpeg currently seems to be the best candidate to handle every JPEG2000 variants, unfortunately after several tries with ImageMagick and custom delegate, I'm not able to provide a working solution, yet. I keep trying.

kuronae12 commented 6 years ago

Hi Christoffer,

I'm trying to decompress the transfer syntax "1.2.840.10008.1.2.4.91 - JPEG 2000 Image Compression (Lossless or Lossy)", but I get an error from the DObject saying that "DICOM: Decompressing the Pixel Data failed. Pixel values can not be extracted."

I already enabled support on ImageMagick for JPEG2000 compression algorithms and actually I'm able to convert the DICOM files if I use the "convert" tool from ImageMagick directly, so ImageMagick and Rmagick are not the problem here.

The DObject hash representation gives me this:

"Samples per Pixel"=>1, "Photometric Interpretation"=>"MONOCHROME2", "Rows"=>512, "Columns"=>512, "Pixel Spacing"=>"0.885\\0.885", "Bits Allocated"=>16, "Bits Stored"=>16, "High Bit"=>15, "Pixel Representation"=>1, "Window Center"=>"40", "Window Width"=>"400", "Rescale Intercept"=>"0", "Rescale Slope"=>"1", "Lossy Image Compression"=>"01", "Lossy Image Compression Ratio"=>"1", "Scheduled Procedure Step Start Date"=>"20180210", "Scheduled Procedure Step Start Time"=>"111206.000", "Scheduled Procedure Step End Date"=>"20180210", "Scheduled Procedure Step End Time"=>"114206.000", "Performed Procedure Step Start Date"=>"20180210", "Performed Procedure Step Start Time"=>"111206.000", "Performed Procedure Step ID"=>"3838", "7005,0010"=>nil, "7005,1007"=>nil, "7005,1008"=>nil, "7005,1009"=>nil, "7005,100A"=>nil, "7005,100B"=>nil, "7005,100D"=>nil, "7005,100E"=>nil, "7005,100F"=>nil, "7005,1012"=>nil, "7005,1013"=>nil, "7005,1016"=>nil, "7005,1017"=>nil, "7005,1018"=>nil, "7005,1019"=>nil, "7005,101A"=>nil, "7005,101B"=>nil, "7005,101D"=>nil, "7005,101E"=>nil, "7005,101F"=>nil, "7005,1020"=>nil, "7005,1021"=>nil, "7005,1022"=>nil, "7005,1023"=>nil, "7005,1024"=>nil, "7005,1030"=>nil, "7005,1040"=>nil, "Encapsulated Pixel Data"=>{"Item 0"=>{"Item 0"=>{"Pixel Data Item"=>nil}}}}

So, I see that actually the DObject doesn't get any pixel data information from the DICOM file. Do you know what might be the problem and point me to the correct part of the code in order to fix it?

At this point I'm going to use Rmagick to convert those DICOM files and see if I can find the problem within the ImageItem and DObject classes, but any help pointing me to the correct direction is greatly appreciated.

Regards

dicom commented 6 years ago

Hi kuronae, thanks for your interest!

I would suggest checking out image_item.rb: https://github.com/dicom/ruby-dicom/blob/master/lib/dicom/image_item.rb

Start with the #image and #images methods.

Then you'd probably have to have a look in the image_process.rb file: https://github.com/dicom/ruby-dicom/blob/master/lib/dicom/image_processor.rb

Specifically the #decompress method.

This again calls the #decompress method in the image_process_r_magick.rb file: https://github.com/dicom/ruby-dicom/blob/master/lib/dicom/image_processor_r_magick.rb

That should get you started I think. Happy hunting!

Best regards, Christoffer

kuronae12 commented 6 years ago

Hi Christoffer,

Thank you for the directions! I managed to track down the bug, and it happens to be Rmagick, there's a bug in decompressing images using the new ImageMagick versions 7.0.7-22 with JPEG2000 support. So I switched the image processor to MiniMagick which happens to be much more updated and maintained than Rmagick, made a couple of changes in the service code that helps me convert the DICOM files and now my service is working as expected :D

Thanks for the help and for creating this amazing tool, I'm creating a service based on RubyDicom. I'll try to factorize some code and add Celluloid support to the DServer and DClient classes in order to make it more stable even though it may not be that necessary, my server has been running for a couple of days without problems, no memory leaks and it's been receiving +100,000 images a day, so I must say that it's pretty stable at this moment.

Best regards, Jose

dicom commented 6 years ago

Wow, that's encouraging to hear, thank you for this nice feedback. I am happy to hear that you are finding this tool useful.

Of course any contributions will be most welcome!

ashramsey commented 3 years ago

made a couple of changes in the service code that helps me convert the DICOM files and now my service is working as expected :D

Hey Jose,

I'm using MiniMagick and am experiencing the error: unsupported transfer syntax: '1.2.840.10008.1.2.4.70' - JPEG Lossless.

Could you please provide some details of what changes you made to your service code to get it working?

Thanks, Ash