如何使用 gdcm 和 Python 解压缩压缩的 DICOM 图像?
How to decompress a compressed DICOM image with gdcm and Python?
我正在处理我想解压的压缩 DICOM 图像,Python 2.7 on Ubuntu 14。我正在使用 gdcm,我在 [=16= 之后安装了它] (sudo apt-get install python-gdcm
)
我正在使用 this example 解压缩图像(至少当我尝试打开图像时 ImageJ 将其称为 "compressed dicom image"),但我遇到无法解决的错误。代码如下(只是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)
在print dims
标记程序确实打印了正确尺寸的图像。但是当它到达 w.SetImage(image)
时,我得到了一个错误,而且我还收到了一堆警告:
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.
这个例子只适用于某些类型的图像吗?还是我遗漏了什么?
既然你只是想用 python 简单地解压缩图像,为什么不简单地使用这个:
import gdcm
import sys
if __name__ == "__main__":
file1 = sys.argv[1] # input filename
file2 = sys.argv[2] # output filename
reader = gdcm.ImageReader()
reader.SetFileName( file1 )
if not reader.Read():
sys.exit(1)
change = gdcm.ImageChangeTransferSyntax()
change.SetTransferSyntax( gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian) )
change.SetInput( reader.GetImage() )
if not change.Change():
sys.exit(1)
writer = gdcm.ImageWriter()
writer.SetFileName( file2 )
writer.SetFile( reader.GetFile() )
writer.SetImage( change.GetOutput() )
if not writer.Write():
sys.exit(1)
使用时:
$ python decompress.py gdcm/Testing/Data/012345.002.050.dcm raw.dcm
这导致:
$ gdcminfo raw.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.4 [MR Image Storage]
TransferSyntax is 1.2.840.10008.1.2 [Implicit VR Little Endian: Default Transfer Syntax for DICOM]
NumberOfDimensions: 2
Dimensions: (256,256,1)
SamplesPerPixel :1
BitsAllocated :16
BitsStored :16
HighBit :15
PixelRepresentation:1
ScalarType found :INT16
PhotometricInterpretation: MONOCHROME2
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2
Origin: (-85,21.6,108.7)
Spacing: (0.664062,0.664062,1.5)
DirectionCosines: (1,0,0,0,0,-1)
Rescale Intercept/Slope: (0,1)
Orientation Label: CORONAL
更新,好像是原来的bug
gdcmObject.h:58: virtual gdcm::Object::~Object(): Assertion
`ReferenceCount == 0' failed.
已在此处上游解决:
我正在处理我想解压的压缩 DICOM 图像,Python 2.7 on Ubuntu 14。我正在使用 gdcm,我在 [=16= 之后安装了它] (sudo apt-get install python-gdcm
)
我正在使用 this example 解压缩图像(至少当我尝试打开图像时 ImageJ 将其称为 "compressed dicom image"),但我遇到无法解决的错误。代码如下(只是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)
在print dims
标记程序确实打印了正确尺寸的图像。但是当它到达 w.SetImage(image)
时,我得到了一个错误,而且我还收到了一堆警告:
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.
这个例子只适用于某些类型的图像吗?还是我遗漏了什么?
既然你只是想用 python 简单地解压缩图像,为什么不简单地使用这个:
import gdcm
import sys
if __name__ == "__main__":
file1 = sys.argv[1] # input filename
file2 = sys.argv[2] # output filename
reader = gdcm.ImageReader()
reader.SetFileName( file1 )
if not reader.Read():
sys.exit(1)
change = gdcm.ImageChangeTransferSyntax()
change.SetTransferSyntax( gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian) )
change.SetInput( reader.GetImage() )
if not change.Change():
sys.exit(1)
writer = gdcm.ImageWriter()
writer.SetFileName( file2 )
writer.SetFile( reader.GetFile() )
writer.SetImage( change.GetOutput() )
if not writer.Write():
sys.exit(1)
使用时:
$ python decompress.py gdcm/Testing/Data/012345.002.050.dcm raw.dcm
这导致:
$ gdcminfo raw.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.4 [MR Image Storage]
TransferSyntax is 1.2.840.10008.1.2 [Implicit VR Little Endian: Default Transfer Syntax for DICOM]
NumberOfDimensions: 2
Dimensions: (256,256,1)
SamplesPerPixel :1
BitsAllocated :16
BitsStored :16
HighBit :15
PixelRepresentation:1
ScalarType found :INT16
PhotometricInterpretation: MONOCHROME2
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2
Origin: (-85,21.6,108.7)
Spacing: (0.664062,0.664062,1.5)
DirectionCosines: (1,0,0,0,0,-1)
Rescale Intercept/Slope: (0,1)
Orientation Label: CORONAL
更新,好像是原来的bug
gdcmObject.h:58: virtual gdcm::Object::~Object(): Assertion `ReferenceCount == 0' failed.
已在此处上游解决: