python - Python 将 .dcm 转换为 .png,图像太亮
问题描述
我必须将一些默认提供的文件转换为.dcm
,.png
我在这里找到了一些代码示例来实现这一点,但最终结果太亮了。请问有人可以看看这个吗?
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)
我已经调整了代码,但似乎没有任何效果。
解决方案
一些 DICOM 数据集需要对原始像素强度进行窗口中心/宽度重新缩放(通过VOI LUT 模块中的 (0028,1050)窗口中心和 (0028,1051)窗口宽度元素),以便重现它们“查看”的方式.
pydicom有一个函数apply_voi_lut()用于应用此窗口:
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...
根据数据集类型,您可能需要事先使用apply_modality_lut()。
推荐阅读
- string - 解析字符串,然后将其存储为数组并在 shell 脚本上再次解析
- php - 如果发生异常,Laravel 停止队列作业
- html - 基于数据库配置的动态模板Angular
- pandas - 熊猫数据框剥离内容
- python - Python 运行版本 3.5 而不是 3.6
- magento2 - 如何在同一行中制作名字和姓氏结帐页面送货地址
- html - 当您将鼠标悬停在 p 元素的不同部分时,如何使用 CSS 使 div 元素出现?
- hdfs - IMPALA:基于未分区表创建新的分区表
- python - 为什么 repr(float) 在 Google App Engine 上返回的数字比其他数字多
- collections - UWP C++ Winrt中是否有具体的向量类