#Load the libraries
import imageio #a python dicom and image library
import scipy.ndimage as ndi
import matplotlib.pyplot as plt #main graphing library in Python
#Load a DICOM files in an imageio object
im=imageio.imread('./data/IMG/CT-MONO2-16-brain.dcm')
#this property shape will show the dimensions of the image
# eg. (512,512) is 512 columns and 512 rows
# note (3,512,512) would be a stack of images (like CT)
im.shape
(512, 512)
#The total number of pixel or elements in the array
im.size
262144
#minimum pixel intensity (good for mapping image display)
im.min()
-1057
#maximum pixel intensity
im.max()
1440
#this lists all the dicom meta data as a list of keys
im.meta
Dict([('PixelRepresentation', 1), ('StudyDate', '1999.05.05'), ('SOPInstanceUID', '2.16.840.1.113662.2.1.4519.41582.4105152.419990505.410523251'), ('PatientOrientation', 'L\\P '), ('PixelData', b'Data converted to numpy array, raw data removed to preserve memory'), ('SeriesTime', '10:52:34.530000 '), ('ContentDate', '1999.05.05'), ('RescaleSlope', 1.0), ('TransferSyntaxUID', '1.2.840.10008.1.2.1'), ('InstitutionName', '105 HOSPITAL'), ('InstanceNumber', 8), ('Columns', 512), ('StudyDescription', ''), ('ImageOrientationPatient', (1.0, 0.0, 0.0, 0.0, 1.0, 0.0)), ('RescaleIntercept', 0.0), ('SeriesInstanceUID', '2.16.840.1.113662.2.1.2519.21582.2990505.2105152.2381633.20'), ('AcquisitionTime', '10:52:34.530000 '), ('ContentTime', '10:52:32.510000 '), ('BitsAllocated', 16), ('Manufacturer', 'Picker International, Inc.'), ('SeriesNumber', 3513), ('ImagePositionPatient', (-119.7656, -399.7656, -280.0)), ('SeriesDate', '1999.05.05'), ('AcquisitionNumber', 3513), ('shape', (512, 512)), ('SOPClassUID', '1.2.840.10008.5.1.4.1.1.2'), ('HighBit', 15), ('StudyTime', '10:52:34.530000 '), ('AcquisitionDate', '1999.05.05'), ('SamplesPerPixel', 1), ('BitsStored', 16), ('PatientName', 'Anonymized'), ('Rows', 512), ('PixelSpacing', (0.46875, 0.46875)), ('sampling', (0.46875, 0.46875)), ('StudyInstanceUID', '2.16.840.1.113662.2.1.1519.11582.1990505.1105152'), ('Modality', 'CT')])
#pull a single key from the DICOM meta data
im.meta['Modality']
'CT'
#plot the image using a grayscale lookup table
plt.imshow(im,cmap='gray')
plt.show()
#this applies a threshold mask on the image by the intensity of each pixel
# a mask returns a binary value levels of 0 or 1 if the pixel is below
# or above the threshold
mask1=im>200
plt.imshow(mask1,cmap='gray')
plt.show()
#This block copies the image to a new object
#and then sets the value of the center region a new value
im2=im
im2[200:300,200:300]=1000
plt.imshow(im2,cmap='gray')
plt.show()
#This creates a new image by rotating the original one.
#very useful for registration for longtidunal analysis
im3=ndi.rotate(im,30)
plt.imshow(im3,cmap='gray')
plt.show()