0

我需要使用GDCM将 DICOM 图像转换为 PNG 格式。虽然此示例 有效,但它似乎没有考虑LUT,因此我得到了反转/非反转图像的混合。虽然我熟悉 C++ 和 Python,但我不能完全掌握包装器中的黑魔法。该文档纯粹是用 C++ 编写的,我需要一些帮助来连接这些点。

主要任务

转换示例中的以下部分:

def gdcm_to_numpy(image):
    ....
    gdcm_array = image.GetBuffer()
    result = numpy.frombuffer(gdcm_array, dtype=dtype)
    ....

像这样:

def gdcm_to_numpy(image):
    ....
    gdcm_array = image.GetBuffer()
    lut = image.GetLUT()
    gdcm_decoded = lut.Decode(gdcm_array)
    result = numpy.frombuffer(gdcm_decoded, dtype=dtype)
    ....

现在这给出了错误:

NotImplementedError: Wrong number or type of arguments for overloaded function 'LookupTable_Decode'.
  Possible C/C++ prototypes are:
    gdcm::LookupTable::Decode(std::istream &,std::ostream &) const
    gdcm::LookupTable::Decode(char *,size_t,char const *,size_t) const

通过查看 GetBuffer 定义,我猜第一个参数是分配的变量bool GetBuffer(char *buffer) const;。我想后一个 4 参数版本是我应该瞄准的版本。不幸的是,我不知道这些size_t论点应该是什么。我试过了

gdcm_in_size = sys.getsizeof(gdcm_array)
gdcm_out_size = sys.getsizeof(gdcm_array)*3
gdcm_decoded = lut.Decode(gdcm_out_size, gdcm_array, gdcm_in_size) 

gdcm_in_size = ctypes.sizeof(gdcm_array)
gdcm_out_size = ctypes.sizeof(gdcm_array)*3
gdcm_decoded = lut.Decode(gdcm_out_size, gdcm_array, gdcm_in_size) 

但没有成功。

更新 -ImageApplyLookupTable根据@malat 的建议进行测试

...
lutfilt = gdcm.ImageApplyLookupTable();
lutfilt.SetInput( image );
if (not lutfilt.Apply()):
    print("Failed to apply LUT")

gdcm_decoded = lutfilt.GetOutputAsPixmap()\
    .GetBuffer()

dtype = get_numpy_array_type(pf)
result = numpy.frombuffer(gdcm_decoded, dtype=dtype)
...

不幸的是,我打印了“无法应用 LUT”,并且图像仍然是倒置的。见下图,ImageJ 表明它有一个反相 LUT。

两张图片的对比

4

1 回答 1

1

作为一个简单的解决方案,我会先应用 LUT。在这种情况下,您需要使用ImageApplyLookupTable。它在内部调用 gdcm::LookupTable API。参见例如

当然,正确的解决方案是传递 DICOM LUT 并将其转换为 PNG LUT。


更新:现在您已经发布了屏幕截图。我明白你这边发生了什么。您不是在尝试应用 DICOM 查找表,而是在尝试更改两个不同的 Photometric Interpration DICOM DataSet 的渲染,即 MONOCHROME1 与 MONOCHROME2。

在这种情况下,您可以通过使用软件实现来更改它:gdcm.ImageChangePhotometricInterpretation。从技术上讲,这种类型的渲染最好使用您的图形卡完成(但这是另一回事)。

于 2015-08-14T15:24:33.940 回答