I'm working with compressed DICOM images I would like to decompress, with Python 2.7 on Ubuntu 14. I'm using gdcm, which I've installed following this link (sudo apt-get install python-gdcm
)
I'm using this example to decompress an image (at least ImageJ calls it a "compressed dicom image" when I try to open it), but I get an error I can't solve. Code follows (it is simply the example in the link)
import gdcm
import sys
if __name__ == "__main__":
file1 = sys.argv[1]
file2 = sys.argv[2]
r = gdcm.ImageReader()
r.SetFileName(cin)
if not r.Read():
sys.exit(1)
image = gdcm.Image()
ir = r.GetImage()
image.SetNumberOfDimensions( ir.GetNumberOfDimensions() );
dims = ir.GetDimensions();
print ir.GetDimension(0);
print ir.GetDimension(1);
print "Dims:",dims
image.SetDimension(0, ir.GetDimension(0) );
image.SetDimension(1, ir.GetDimension(1) );
pixeltype = ir.GetPixelFormat();
image.SetPixelFormat( pixeltype );
pi = ir.GetPhotometricInterpretation();
image.SetPhotometricInterpretation( pi );
pixeldata = gdcm.DataElement( gdcm.Tag(0x7fe0,0x0010) )
str1 = ir.GetBuffer()
#print ir.GetBufferLength()
pixeldata.SetByteValue( str1, gdcm.VL( len(str1) ) )
image.SetDataElement( pixeldata )
w = gdcm.ImageWriter()
w.SetFileName(path_save+"uncompressed.png")
w.SetFile( r.GetFile() )
w.SetImage( image )
if not w.Write():
sys.exit(1)
At the print dims
mark program indeed prints the correct dimensions of the image. But when it reaches w.SetImage(image)
, I get an error, and I also get a bunch of warnings :
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmOverlay.cxx, line 205, function void gdcm::Overlay::Update(const gdcm::DataElement&)
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx, line 544, function bool gdcm::DoOverlays(const gdcm::DataSet&, gdcm::Pixmap&)
Bits Allocated are wrong. Correcting.
Error: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmOverlay.cxx, line 265, function bool gdcm::Overlay::GrabOverlayFromPixelData(const gdcm::DataSet&)
Could not find Pixel Data. Cannot extract Overlay.
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx, line 550, function bool gdcm::DoOverlays(const gdcm::DataSet&, gdcm::Pixmap&)
Could not extract Overlay from Pixel Data
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx, line 575, function bool gdcm::DoOverlays(const gdcm::DataSet&, gdcm::Pixmap&)
Invalid BitPosition: 0 for overlay #0 removing it.
python2.7: /build/gdcm-uIgnvq/gdcm-2.6.3/Source/Common/gdcmObject.h:58: virtual gdcm::Object::~Object(): Assertion `ReferenceCount == 0' failed.
Is this example only valid for certain kinds of images ? Or am I missing something ?