Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将DICOM图像转换为XYZ坐标及其值的列表-Python_Python_Numpy_Pixel_Pydicom_Medical Imaging - Fatal编程技术网

将DICOM图像转换为XYZ坐标及其值的列表-Python

将DICOM图像转换为XYZ坐标及其值的列表-Python,python,numpy,pixel,pydicom,medical-imaging,Python,Numpy,Pixel,Pydicom,Medical Imaging,以下是我试图用Python实现的目标(请记住,我对Python比较陌生): 将DICOM图像转换为xyz坐标及其各自像素值的列表,并将该列表导出为.csv文件 从上一个任务中生成的xyz坐标和像素值列表中重新生成相同的图像 到目前为止,我已经能够读入dicom图像,并通过使用pydicom和numpy将它们转换成一个数组。我还能够通过几个for循环提取像素和坐标值,并将该列表导出为.csv。但必须有更好的方法来保持某种质量控制,因为当我尝试重新生成图像(通过使用另一组for循环)时,我不会得到原

以下是我试图用Python实现的目标(请记住,我对Python比较陌生):

  • 将DICOM图像转换为xyz坐标及其各自像素值的列表,并将该列表导出为.csv文件
  • 从上一个任务中生成的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

    我对DICOM不是很熟悉,但从以下几点来看,我认为应该可行:

    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并将其读回以完成整个循环。