以下是我在Python中尝试完成的任务(请记住,我对Python相对较新):
到目前为止,我已经能够读取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
答案 0 :(得分: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')