【问题标题】:Transform DICOM Image to a list of XYZ coordinates and their values - Python将 DICOM 图像转换为 XYZ 坐标及其值的列表 - Python
【发布时间】:2013-07-24 03:55:29
【问题描述】:

这是我试图在 Python 中完成的工作(请记住,我对 Python 比较陌生):

  1. 将 DICOM 图像转换为 xyz 坐标及其各自像素值的列表,并将列表导出为 .csv 文件。
  2. 从上一个任务中生成的 xyz 坐标和像素值列表重新生成相同的图像。

到目前为止,我已经能够读取 dicom 图像并通过使用 pydicom 和 numpy 将它们转换为数组。我还能够通过几个 for 循环提取像素和坐标值,并将该列表导出为 .csv。但是必须有更好的方法来保持某种质量控制,因为当我尝试重新生成图像时(通过使用另一组 for 循环),我没有得到原始图像。

我需要两个函数分别在不同的 python 脚本中运行。

这是我目前所拥有的:

    #Raster through all pixels and copy each value and coordinates to arrays
    rc_cntr = 0
    for r in range(0,img_rows):
                for c in range(0,img_cols):
                    pixel = dcmarray[r, c]
                    rArray[rc_cntr] = r
                    cArray[rc_cntr] = c
                    zArray[rc_cntr] = z_cntr
                    imgArray[rc_cntr] = dcmarray[r,c]
                    rc_cntr = rc_cntr + 1;

    #Combine arrays into one file
            XYZV = numpy.column_stack([rArray,cArray,zArray, imgArray])
            numpy.savetxt(output_path,XYZV,'%0i','\t') #Save XYZV files for each image

我们将不胜感激任何有关此事的帮助。

干杯 AFH

【问题讨论】:

  • 如果您发布一些代码,您将增加获得有用答案的机会。

标签: python numpy pixel pydicom medical-imaging


【解决方案1】:

我对 DICOM 不是很熟悉,但是看着pydicom docs 我认为以下应该可以工作:

import dicom
import numpy as np

ds = dicom.read_file('your_file.dcm')
planes, rows, cols = ds.NumberofFrames, ds.Columns, ds.Rows
image = ds.pixel_array # should have shape (planes, rows, cols)

# to get data and coords to write to CSV
image_data = image.ravel()
z, y, x = np.meshgrid(np.arange(planes), np.arange(rows), np.arange(cols),
                      indexing='ij').T

# to write CSV read image back into DICOM file
planes, rows, cols = np.ptp(z)+1, np.ptp(y)+1, np.ptp(x)+1
image = np.zeros((planes, rows, cols), dtype=image_data.dtype)
image[z, y, x] = image_data

ds.NumberofFrames, ds.Columns, ds.Rows = planes, rows, cols
ds.PixelData = image.tostring()
ds.save_as('another_file.dcm')

【讨论】:

  • 我已经能够运行代码,只是很难保存到 .csv 并将其读回以完成循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
相关资源
最近更新 更多