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!
- import pydicom as dicom
- # Dataset is the main dicom object and is derived from python dictionary data type (i.e. key:value pairs)
- # key: is the DICOM (group,element) tag (as a Tag object)
- # value: is a DataElement instance. It stores:
- # tag - a DICOM tag
- # VR – DICOM value representation (see http://dicom.nema.org/dicom/2013/output/chtml/part05/sect_6.2.html)
- # VM – value multiplicity
- # value – the actual value.
- ds = dicom.dcmread("00000001.dcm")
- # Check for the existence of a particular Attributes
- print("PatientName" in ds)
- # Access DataElement (recommended methods)
- patient_name = ds.data_element("PatientName")
- patient_name_tag = patient_name.tag
- patient_name_VR = patient_name.VR
- patient_name_VM = patient_name.VM
- patient_name_value = patient_name.value
- # Access the raw pixel data
- pixel_bytes = ds.PixelData
- # Access pixel data in more intelligent way for uncompressed images (recommended)
- pixel_array_numpy = ds.pixel_array # returns a NumPy array for uncompressed images
- dimensions = ds.pixel_array.shape
- # NumPy can be used to modify the pixel_array, but they must be written back to the PixelData to be saved
- # example: set pixels with values < 300 to zero
- for n, val in enumerate(ds.pixel_array.flat):
- if val < 300:
- ds.pixel_array.flat[n] = 0
- # construct Python bytes containing the raw data bytes in the array
- ds.PixelData = ds.pixel_array.tostring()
- ds.save_as("00000001_new.dcm")
Articoli simili
- To set an image to fill 50% of the screen on all Android devices, what size of image should I keep in a resource folder?
- How to detect an object from static image and crop it from the image using openCV
- Cos'è il "Derek Zoolander Center for Kids Who Can't Read Good and Who Wanna Learn to Do Other Stuff Good Too"?
- How to read a text file in Android studio when I have the path of that file