GDCM 库无法读取 C# 中的 dicom 文件
GDCM library cannot read dicom file in C#
我尝试使用以下代码使用 GDCM 库读取 DICOM 文件:
gdcm.ImageReader imagereader = new gdcm.ImageReader();
imagereader.SetFileName(@"E:\sample_success.dcm");
if (!imagereader.Read()) throw new Exception("Cannot read dicom file!");
对于 "sample_success.dcm" 文件,我可以很好地读取文件 (sample_success.png)。
但是使用 "sample_failed.dcm" 文件,GDCM 会抛出异常,因为它无法读取它。我尝试使用其他 DICOM 查看器(例如 Radiant)打开该文件,但它成功了。我的 GDCM 构建有问题吗?为什么它不能读取它?
我使用 GDCM 2.6.5。请找到两个示例 here.
您的文件在偏移 0x1480aa
后(像素数据属性中的某处)包含垃圾(一堆二进制 0)。如果没有正确报告错误,您对工具包有什么期望?
根据设计,GDCM 仍会加载它可以加载的任何内容,直到出现错误。因此,如果您在代码中删除 new Exception
,您可以决定(例如)将 imagereader.GetFile()
传递给 gdcm::Writer
并将文件重写为干净的 DICOM。
附带说明一下,我无法访问 Radiant
软件,但我觉得很奇怪,在这种情况下它没有指示错误。
我已经检查过 DCMTK 和 dicom3tools,它们都报告了解析问题。
使用 gdcm 命令行工具,您几乎可以重写文件:
$ gdcmconv -I sample_failed.dcm sample_failed_correct.dcm
因为您的输入数据集无效,GDCM(错误地)认为看到了一个属性,您可以使用以下方法删除它:
$ gdcmanon --dumb --remove 0,0 sample_failed_correct.dcm sample_failed_correct_clean.dcm
然后:
$ gdcminfo sample_failed_correct.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.3.1 [Ultrasound Multi-frame Image Storage]
TransferSyntax is 1.2.840.10008.1.2.4.50 [JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression]
NumberOfDimensions: 3
Dimensions: (800,600,21)
SamplesPerPixel :3
BitsAllocated :8
BitsStored :8
HighBit :7
PixelRepresentation:0
ScalarType found :UINT8
PhotometricInterpretation: YBR_FULL_422
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2.4.50
Origin: (0,0,0)
Spacing: (0.0106324,0.0106324,1)
DirectionCosines: (1,0,0,0,1,0)
Rescale Intercept/Slope: (0,1)
Orientation Label: AXIAL
像素数据中的碎片数量有效:
$ gdcmdump sample_failed_correct.dcm | grep Item | grep "ff.d8" | wc
21 126 2856
我尝试使用以下代码使用 GDCM 库读取 DICOM 文件:
gdcm.ImageReader imagereader = new gdcm.ImageReader();
imagereader.SetFileName(@"E:\sample_success.dcm");
if (!imagereader.Read()) throw new Exception("Cannot read dicom file!");
对于 "sample_success.dcm" 文件,我可以很好地读取文件 (sample_success.png)。 但是使用 "sample_failed.dcm" 文件,GDCM 会抛出异常,因为它无法读取它。我尝试使用其他 DICOM 查看器(例如 Radiant)打开该文件,但它成功了。我的 GDCM 构建有问题吗?为什么它不能读取它?
我使用 GDCM 2.6.5。请找到两个示例 here.
您的文件在偏移 0x1480aa
后(像素数据属性中的某处)包含垃圾(一堆二进制 0)。如果没有正确报告错误,您对工具包有什么期望?
根据设计,GDCM 仍会加载它可以加载的任何内容,直到出现错误。因此,如果您在代码中删除 new Exception
,您可以决定(例如)将 imagereader.GetFile()
传递给 gdcm::Writer
并将文件重写为干净的 DICOM。
附带说明一下,我无法访问 Radiant
软件,但我觉得很奇怪,在这种情况下它没有指示错误。
我已经检查过 DCMTK 和 dicom3tools,它们都报告了解析问题。
使用 gdcm 命令行工具,您几乎可以重写文件:
$ gdcmconv -I sample_failed.dcm sample_failed_correct.dcm
因为您的输入数据集无效,GDCM(错误地)认为看到了一个属性,您可以使用以下方法删除它:
$ gdcmanon --dumb --remove 0,0 sample_failed_correct.dcm sample_failed_correct_clean.dcm
然后:
$ gdcminfo sample_failed_correct.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.3.1 [Ultrasound Multi-frame Image Storage]
TransferSyntax is 1.2.840.10008.1.2.4.50 [JPEG Baseline (Process 1): Default Transfer Syntax for Lossy JPEG 8 Bit Image Compression]
NumberOfDimensions: 3
Dimensions: (800,600,21)
SamplesPerPixel :3
BitsAllocated :8
BitsStored :8
HighBit :7
PixelRepresentation:0
ScalarType found :UINT8
PhotometricInterpretation: YBR_FULL_422
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2.4.50
Origin: (0,0,0)
Spacing: (0.0106324,0.0106324,1)
DirectionCosines: (1,0,0,0,1,0)
Rescale Intercept/Slope: (0,1)
Orientation Label: AXIAL
像素数据中的碎片数量有效:
$ gdcmdump sample_failed_correct.dcm | grep Item | grep "ff.d8" | wc
21 126 2856