pydicom / pylibjpeg

A Python framework for decoding JPEG images, with a focus on supporting pydicom

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pixel handler with support for JPEG2000

machur opened this issue · comments

Hi,

I tried to read a DICOM file with transfer syntax [1.2.840.10008.1.2.4.90] as described on the page https://github.com/pydicom/pylibjpeg and received the following warning suggesting that the underlying mechanism for decompression still uses the obsolete pixel handler from PIL instead of the one provided by pylibjpeg-openjpeg plugin:

UserWarning: The (0028,0101) 'Bits Stored' value doesn't match the sample bit depth of the JPEG2000 pixel data (12 vs 11 bit). It's recommended that you first change the 'Bits Stored' value to match the JPEG2000 bit depth in order to get the correct pixel data "correct pixel data".format(ds.BitsStored, j2k_precision)

The script I used:

from pydicom import dcmread
import pylibjpeg

ds = dcmread(r"dicom.dcm")
j2k_arr = ds.pixel_array 

I upgraded all necessary packages to the latest versions, so the setup was as follows:
pydicom 2.0.0
pylibjpeg 1.1.1
pylibjpeg-libjpeg 1.0.0
pylibjpeg-openjpeg 1.0.1

Is this feature included in the latest release? Am I doing something wrong?

Also according to the https://github.com/pydicom/pydicom/blob/master/pydicom/config.py the PIL handler is still being added to the handlers collection as a support for JPEG2000.

Regards,
Bartek

The PIL handler is always there, calling import pylibjpeg should add the pylibjpeg handler. If you also have pillow installed then it'll get called to handle the pixel data first.

pydicom calls the pixel data handlers in the order given in pydicom.config.pixel_data_handlers with the first one that both supports a given transfer syntax and successfully decodes it being the one that returns the array. For the current release the only way to control which handler is used is by changing the order of handlers in pixel_data_handlers (or uninstalling the package the handler depends on). For non-pylibjpeg handlers you can use the Dataset.convert_pixel_data(handler_name='pillow') method, but that doesn't support pylibjpeg yet (next version of pydicom hopefully).

I'd also upgrade the -libjpeg plugin, it should be v1.1.0 to be compatible with pylibjpeg v1.1.1. I should add some version awareness to the plugins...

Thanks for your reply. We've been using PIL anyway, so it was little bit confusing, I didn't see that PIL is optional for pydicom.

Also I should note that the PIL handler isn't obsolete and as far as I'm aware there are no plans to remove it from pydicom. pylibjpeg exists to give users an option for the transfer syntaxes the PIL hander doesn't support and where it's not straightforward to use GDCM.

@scaramallion One more observation from my side: when using PIL handler I received the above-mentioned warning and pixel intensities for some slices were doubled - the max pixel values for such slices mismatched the largest pixel values defined in DICOM tags. That is the reason why I decided to use another package than PIL and though it was marked as obsolete. I just checked closed issues and the problem I encountered looks similar to pydicom/pydicom#693.

The decompressed intensities were correct once I succesfully changed default JPEG2000 pixel handler to pylibjpeg (although a similar warning about bit inconsistency was displayed as well).

Just to confirm, you did change the Bits Stored value as recommended and you're still seeing incorrect images? The change should be done before calling Dataset.pixel_array

With pylibjpeg I'm not sure that the bit inconsistency actually matters very much, but for the pillow handler it does because the value is used to undo a change PIL does to convert the raw pixel values to one of their supported image modes.