简体   繁体   English

如何在Python中的gdcm中使用DICOM LUT解码器-传递缓冲区

[英]How to use the DICOM LUT decoder with gdcm in Python - passing buffers

I need to use the GDCM for converting DICOM images to PNG-format. 我需要使用GDCM将DICOM图像转换为PNG格式。 While this example works, it does not seem to take the LUT into account and thus I get a mixture of inverted/non-inverted images. 尽管此示例有效,但似乎并没有考虑到LUT ,因此我得到了反转/非反转图像的混合体。 While I'm familiar with both C++ and Python I can't quite grasp the black magic inside the wrapper. 虽然我既熟悉C ++和Python,但也不太了解包装器内部的黑魔法。 The documentation is purely written in C++ and I need some help in connecting the dots. 该文档纯粹是用C ++编写的,在连接点方面我需要一些帮助。

The main task 主要任务

Convert the following section in the example: 在示例中转换以下部分:

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

to something like this: 像这样:

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)
    ....

Now this gives the error: 现在这给出了错误:

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

From looking at the GetBuffer definition I guess the first parameter is the assigned variable bool GetBuffer(char *buffer) const; 通过查看GetBuffer定义,我猜第一个参数是分配的变量bool GetBuffer(char *buffer) const; . I guess that the latter 4-argument version is the one I should aim for. 我想后者是4参数版本,我应该针对该版本。 Unfortunately I have no clue to what the size_t arguments should be. 不幸的是,我不知道size_t参数应该是什么。 I've tried with 我尝试过

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) 

also

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) 

but with no success. 但没有成功。

Update - test with the ImageApplyLookupTable according to @malat's suggestion 更新-测试与ImageApplyLookupTable根据@马拉特的建议

...
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)
...

Unfortunately I get "Failed to apply LUT" printed and the images are still inverted. 不幸的是,我打印了“无法应用LUT”,并且图像仍被反转。 See the below image, ImageJ suggests that it has an inverting LUT. 参见下图,ImageJ建议它具有反相LUT。

两个图像的比较

As a simple solution, I would apply the LUT first. 作为一个简单的解决方案,我将首先应用LUT。 In which case you'll need to use ImageApplyLookupTable . 在这种情况下,您需要使用ImageApplyLookupTable It internally calls the gdcm::LookupTable API . 它在内部调用gdcm :: LookupTable API See for example . 参见例如

Of course the correct solution would be to pass the DICOM LUT and convert it to a PNG LUT. 当然,正确的解决方案是传递DICOM LUT并将其转换为PNG LUT。


Update: now that you have posted the screenshot. 更新:现在您已经发布了屏幕截图。 I understand what is going on on your side. 我了解您身边的情况。 You are not trying to apply the DICOM Lookup Table, you are trying to change the rendering of two different Photometric Interpration DICOM DataSet, namely MONOCHROME1 vs MONOCHROME2. 您不是要应用DICOM查找表,而是要更改两个不同的光度插值DICOM数据集的呈现,即MONOCHROME1与MONOCHROME2。

In this case you can change that using software implementation via the use of: gdcm.ImageChangePhotometricInterpretation . 在这种情况下,您可以使用软件实现通过gdcm.ImageChangePhotometricInterpretation进行更改 Technically this type of rendering is best done using your graphic card (but that is a different story). 从技术上讲,最好使用图形卡完成这种类型的渲染(但这是另一回事)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM