Casa > H > How To Read A Dicom Image In Python

How to read a DICOM image in Python

The below should give you an idea on how the Pydicom package works. This should be more than enough to extract the pixel data for post-processing.

Hope this helps!

  1. import pydicom as dicom 
  2.  
  3. # Dataset is the main dicom object and is derived from python dictionary data type (i.e. key:value pairs) 
  4. # key: is the DICOM (group,element) tag (as a Tag object) 
  5. # value: is a DataElement instance. It stores: 
  6. # tag - a DICOM tag 
  7. # VR – DICOM value representation (see http://dicom.nema.org/dicom/2013/output/chtml/part05/sect_6.2.html) 
  8. # VM – value multiplicity 
  9. # value – the actual value. 
  10. ds = dicom.dcmread("00000001.dcm") 
  11.  
  12. # Check for the existence of a particular Attributes 
  13. print("PatientName" in ds) 
  14.  
  15. # Access DataElement (recommended methods) 
  16. patient_name = ds.data_element("PatientName") 
  17. patient_name_tag = patient_name.tag 
  18. patient_name_VR = patient_name.VR 
  19. patient_name_VM = patient_name.VM 
  20. patient_name_value = patient_name.value 
  21.  
  22. # Access the raw pixel data 
  23. pixel_bytes = ds.PixelData 
  24.  
  25. # Access pixel data in more intelligent way for uncompressed images (recommended) 
  26. pixel_array_numpy = ds.pixel_array # returns a NumPy array for uncompressed images 
  27. dimensions = ds.pixel_array.shape 
  28.  
  29. # NumPy can be used to modify the pixel_array, but they must be written back to the PixelData to be saved 
  30. # example: set pixels with values < 300 to zero 
  31. for n, val in enumerate(ds.pixel_array.flat): 
  32. if val < 300: 
  33. ds.pixel_array.flat[n] = 0 
  34.  
  35. # construct Python bytes containing the raw data bytes in the array 
  36. ds.PixelData = ds.pixel_array.tostring() 
  37. ds.save_as("00000001_new.dcm") 

De Waldo Huether

É possível ter 100% SRGB em uma tela de 45% NTSC? :: O que arruinou o Disney Channel?