2013-07-24 93 views
3

這裏是我想在Python實現(請記住,我是比較新的蟒蛇):變換DICOM圖像到XYZ座標和它們的值的列表 - 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

+1

如果發佈一些你的代碼,你會增加你的機會得到一個有用的答案。 –

回答

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') 
+0

感謝代碼Jaime ...這是我到目前爲止: – Masrawy

+0

我已經能夠運行代碼,只需要很長時間保存到.csv並閱讀回來完成周期。 – Masrawy