title: Transform DICOM Image to a list of XYZ coordinates and their values - Python
body: <p>Here is what I am trying to accomplish in Python (please keep in mind that I'm relatively new to Python):</p>
  
  <ol>
  <li>Convert a  DICOM image into a list of xyz coordinates along with their respective pixel values and export the list to a .csv file.</li>
  <li>Regenerate the same image from the list of xyz coordinates and pixel values generated in the previous task.</li>
  </ol>
  
  <p>So far, I have been able to read in the dicom images and convert them into an array through the use of pydicom and numpy. I've also been able to extract pixel and coordinate values through several for loops and export that list as a .csv. But there has to be a better way of doing this to maintain some sort of quality control, because when I try to regenerate the images (through the use of another set of for loops), I don't get the original image.</p>
  
  <p>I need both functions to run separately in different python scripts.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>    #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
  </code></pre>
  
  <p>Any help on this matter would be greatly appreciated.</p>
  
  <p>Cheers
    AFH</p>