New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pixel_array is read as broken values #966
Comments
Transfer Syntax UID is 1.2.840.10008.1.2.4.90 - JPEG 2000 Lossless.
Edit: See this comment |
@scaramallion Thank you so much for your help. From your comment, two questions arise me.
import pydicom
x = pydicom.dcmread('failed_dcm.dcm')
with open('failed.jp2', 'wb') as f:
f.write(x.PixelData)
and below result shows me image data stored in dicom is not valid:
However, when I check the same validity of dicom file whose pixel data open successfully using pydicom, jpylyzer also told me this image data is also invalid. How is it possible? Do you have any ideas how pydicom can handle pixel data which is known to be broken by jpylyzer? |
Handling compressed pixel data is not done directly by pydicom, but rather by GDCM or Pillow, so we rely on these libraries to correctly decode the data. Other libraries used by the viewers you mentioned may be able to handle some invalid DICOM files that these cannot handle. |
One last thing to check is if it is supported by Pillow (not very likely, but worth a try). To test this, you have to use an enviroment without GDCM, or adapt |
@mrbean-bremen Thank you very much your help. Unfortunately, NotImplementedError Traceback (most recent call last)
<ipython-input-4-7c0b6bd55455> in <module>
----> 1 x.pixel_array
/opt/conda/lib/python3.7/site-packages/pydicom/dataset.py in pixel_array(self)
1513 The (7fe0,0010) *Pixel Data* converted to a :class:`numpy.ndarray`.
1514 """
-> 1515 self.convert_pixel_data()
1516 return self._pixel_array
1517
/opt/conda/lib/python3.7/site-packages/pydicom/dataset.py in convert_pixel_data(self)
1374 )
1375
-> 1376 raise last_exception
1377
1378 def decompress(self):
/opt/conda/lib/python3.7/site-packages/pydicom/dataset.py in convert_pixel_data(self)
1342 try:
1343 # Use the handler to get a 1D numpy array of the pixel data
-> 1344 arr = handler.get_pixeldata(self)
1345 self._pixel_array = reshape_pixel_array(self, arr)
1346
/opt/conda/lib/python3.7/site-packages/pydicom/pixel_data_handlers/pillow_handler.py in get_pixeldata(ds)
129 "Pillow lacks the jpeg 2000 decoder plugin"
130 .format(transfer_syntax.name))
--> 131 raise NotImplementedError(msg)
132
133 if transfer_syntax not in PillowSupportedTransferSyntaxes:
NotImplementedError: this transfer syntax JPEG 2000 Image Compression (Lossless Only), can not be read because Pillow lacks the jpeg 2000 decoder plugin as you said, it is better trying workaround with other programs... But it was really helpful to know that it is not the problem of pydicom. Thank you again. |
I think you need OpenJPEG installed for Pillow JPEG 2000 support, though I have no experience there, and it is not guaranteed that this will work anyway... |
Closing as answered. |
For future readers, something I've realised after working on better support for JPEG2000 is that jpylyzer won't return a valid result for the data stored in
After re-checking using the openjpeg library, the JPEG2000 codestream data itself in this issue was showing as broken. |
And looking at it again after seeing that openjpeg (as used by Pillow and GDCM) can have issues with trailing padding, it looks like the problem with the data is the COM marker at offset 4099003 (which I think is non-conformant to the JPEG2K standard). If you do the following your image is readable: from pydicom import dcmread
from pydicom.encaps import defragment_data, encapsulate
ds = dcmread('966.dcm')
bs = defragment_data(ds.PixelData)
bs = bs[:4099003] + b'\xff\xd9'
ds.PixelData = encapsulate([bs])
arr = ds.pixel_array So final conclusion: image broken but savable. The other viewers likely use a different JPEG2K library. |
I have a similar case when a try to open my .dcm with pydicom and the pixel value will be read as all zero values. https://drive.google.com/file/d/1AC44KMxfKdR6_ODO97mCBeLepNV7-x4d/view?usp=sharing |
Welp, looking at the file, I'd say your image is all zeros because your image is all zeros.
Data is uncompressed (i.e. not JPEG), entire Pixel Data (offset is 74566) is Are you expecting to see text? There does appear to be Overlay Data, but I didn't look at it. Edit: from pydicom import dcmread
import matplotlib.pyplot as plt
ds = dcmread("966b.dcm")
plt.imshow(ds.overlay_array(0x6002))
plt.show() |
Describe the issue
I want to access pixel data from the dicom file below. It opens successfully in dicom viewers such as horos or dwv, but if I use pydicom to access pixel data from the dicom file, the pixel value will be read as all zero values.
Expected behavior
Read pixel array without broken pixel value.
Steps To Reproduce
How to reproduce the issue. Please include:
https://drive.google.com/file/d/1QEDbjcK09cxqxflJkOvqBUIHgTJbtBaa/view?usp=sharing
Your environment
Please run the following and paste the output.
The text was updated successfully, but these errors were encountered: