我需要使用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。