Python将.dcm转换为.png,图像太亮 [英] Python convert .dcm to .png, images are too bright

查看:126
本文介绍了Python将.dcm转换为.png,图像太亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一些默认为 .dcm 的文件转换为 .png ,我发现一些代码示例可以在此处实现,但最终结果太亮了有人可以看看吗?

I have to convert some files which come by default as .dcm to .png, I've found some code samples to achieve that around here but the end results are too bright. Could anybody have a look at this, please?

 def convert_to_png(file):
    ds = pydicom.dcmread(file)

    shape = ds.pixel_array.shape

    # Convert to float to avoid overflow or underflow losses.
    image_2d = ds.pixel_array.astype(float)

    # Rescaling grey scale between 0-255
    image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0

    # Convert to uint
    image_2d_scaled = np.uint8(image_2d_scaled)

    # Write the PNG file
    with open(f'{file.strip(".dcm")}.png', 'wb') as png_file:
        w = png.Writer(shape[1], shape[0], greyscale=True)
        w.write(png_file, image_2d_scaled)

我已经对代码进行了调整,但是似乎没有任何效果.

I've tweaked around the code but nothing seems to work.

这是实际情况,就像dicom一样,右侧是运行此代码的结果

This is how the actual thing looks like as dicom and on the right side is the result of running this code

推荐答案

某些DICOM数据集需要Window Width 元素="nofollow noreferrer"> VOI LUT模块),以便重现查看"它们的方式.

Some DICOM datasets require window center/width rescaling of the original pixel intensities (via the (0028,1050) Window Center and (0028,1051) Window Width elements in the VOI LUT Module) in order to reproduce the way they were "viewed".

最新的 pydicom 版本(v1.4)具有功能

The most recent pydicom release (v1.4) has a function apply_voi_lut() for applying this windowing:

from pydicom import dcmread
from pydicom.pixel_data_handlers.util import apply_voi_lut

ds = dcmread(file)
if 'WindowWidth' in ds:
    print('Dataset has windowing')

windowed = apply_voi_lut(ds.pixel_array, ds)

# Add code for rescaling to 8-bit...

根据数据集类型,您可能需要使用

Depending on the dataset type you may need to use apply_modality_lut() beforehand.

这篇关于Python将.dcm转换为.png,图像太亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆