Closed ravitej177 closed 3 years ago
Hmm, there are a number of things wrong with the dataset:
0xFFFFFFFF
)To fix... let's see
import matplotlib.pyplot as plt
from pydicom import dcmread
from pydicom.encaps import encapsulate
with dcmread("1320.dcm") as ds:
data = ds.PixelData
# Strip out the JP2 header, may not be necessary for GDCM but let's be conformant
# This will only work if Number of Frames is 1
offset = data.index(b"\xff\x4f")
codestream = data[offset:]
ds.PixelData = encapsulate([codestream])
ds.save_as("1320_fixed.dcm")
with dcmread("1320_fixed.dcm") as ds:
arr = ds.pixel_array
plt.imshow(arr)
plt.show()
Also I think that's the first time I've actually seen the animal part of DICOM being used.
Hmm, there are a number of things wrong with the dataset:
- Uses an compressed transfer syntax but the pixel data has a defined length (should be undefined
0xFFFFFFFF
)- No encapsulation of compressed pixel data
- The J2K data includes a JP2 header
To fix... let's see
import matplotlib.pyplot as plt from pydicom import dcmread from pydicom.encaps import encapsulate with dcmread("1320.dcm") as ds: data = ds.PixelData # Strip out the JP2 header, may not be necessary for GDCM but let's be conformant # This will only work if Number of Frames is 1 offset = data.index(b"\xff\x4f") codestream = data[offset:] ds.PixelData = encapsulate([codestream]) ds.save_as("1320_fixed.dcm") with dcmread("1320_fixed.dcm") as ds: arr = ds.pixel_array plt.imshow(arr) plt.show()
Also I think that's the first time I've actually seen the animal part of DICOM being used.
Thanks @scaramallion, that worked like a charm. Stripping out JP2 Header from the pixel data made the image recognizable in all J2K handlers. Also, May I know why this script may not work for multi frame images? If it cannot handle multiframes, is there a way I can encapsulate multi frame images.
Also, not sure about the animal part of DICOM whether it can be used because I got random DICOM images to test out the functionality.
At least part of the reason for encapsulation of compressed data in DICOM is to ensure there's an easy way to determine where one compressed image codestream ends and another starts. So the problem is that if the JPEG (or other compressed data) codestream from multiple images is concatenated together there's no guaranteed way to split them into separate images without parsing the codestream itself.
For JPEG/JPEG-LS/JPEG 2000, there is one will-mostly-work way which involves looking for the end-of-image marker (0xFFD9
) and using that to split them up. Then you can just use encapsulate()
as usual: ds.PixelData = encapsulate([frame1, frame2, ...])
. It may fail if that marker is present in the encoded image data within the codestream (JPEG uses an escape character beforehand so decoders treat it correctly when that happens).
So, I want to view pixel data of dcm file(JPEG2000) using pydicom, But it throws me error when using GDCM handler that Pixel Data is not encapsulated correctly.
I have also used pillow handler even in that case it throws Unexpected tag '(0000, 0c00)' when parsing the Basic Table Offset item.
my code
Output:
I'm able to access filemeta:
I have used python2.7 and python3.6.9 it occurs in both the versions. How to reencapsulate the pixel data of the dcm file to avoid these errors
I'm attaching sample file here for the reference:
https://dl.dropboxusercontent.com/s/8xic5xlax58p0u0/test.dcm?dl=0