import imageio
import matplotlib.pyplot as plt
import os
Here imageio is used to process the DICOM files under the folder '5.T1_GRE_3D_AXIAL' from graded-Assignment_5
data = imageio.volread('graded-Assignment_5/5.T1_GRE_3D_AXIAL/')
Reading DICOM (examining files): 1/161 files (0.6%) DICOM file has no SamplesPerPixel (perhaps this is a report?) Reading DICOM (examining files): 1/161 files (0.6%) DICOM file has no SamplesPerPixel (perhaps this is a report?) Reading DICOM (examining files): 1/161 files (0.6%) Warning: missing file after '/home/user/graded-Assignment_5/5.T1_GRE_3D_AXIAL/Anat001.20040930.145131.5.T1_GRE_3D_AXIAL.0011.dcm' Reading DICOM (examining files): 1/161 files (0.6%) Warning: missing file after '/home/user/graded-Assignment_5/5.T1_GRE_3D_AXIAL/Anat001.20040930.145131.5.T1_GRE_3D_AXIAL.0157.dcm' Reading DICOM (examining files): 1/161 files (0.6%)161/161 files (100.0%) Found 1 correct series. Reading DICOM (loading data): 158/158 (100.0%)
print(data.meta)
Dict([('TransferSyntaxUID', '1.2.840.10008.1.2'), ('SOPClassUID', '1.2.840.10008.5.1.4.1.1.4'), ('SOPInstanceUID', '1.3.12.2.1107.5.2.13.20590.4.0.3355828215321939'), ('StudyDate', '20040930'), ('SeriesDate', '20040930'), ('AcquisitionDate', '20040930'), ('ContentDate', '20040930'), ('StudyTime', '142047.390000 '), ('SeriesTime', '145131.390000 '), ('AcquisitionTime', '144507.372494 '), ('ContentTime', '145131.406000 '), ('Modality', 'MR'), ('Manufacturer', 'SIEMENS'), ('InstitutionName', 'UNIVERSITY OF ROCHESTER'), ('StudyDescription', 'ACHTMAN^FMRI'), ('SeriesDescription', 'T1 GRE 3D AXIAL'), ('PatientName', 'Anat001'), ('PatientID', '04.09.30-14:20:32-DST-1.3.12.2.1107.5.2.13.20590'), ('PatientBirthDate', '19730905'), ('PatientSex', 'M '), ('PatientAge', '031Y'), ('PatientWeight', 77.18), ('StudyInstanceUID', '1.3.12.2.1107.5.2.13.20590.4.0.3352789032273021'), ('SeriesInstanceUID', '1.3.12.2.1107.5.2.13.20590.4.0.335582689961491'), ('SeriesNumber', 5), ('AcquisitionNumber', 1), ('InstanceNumber', 1), ('ImagePositionPatient', (-97.828758, -189.15324, -65.96143)), ('ImageOrientationPatient', (1.0, -2.0510349e-10, 0.0, 2.0510349e-10, 1.0, 0.0)), ('SamplesPerPixel', 1), ('Rows', 256), ('Columns', 192), ('PixelSpacing', (1.0, 1.0)), ('BitsAllocated', 16), ('BitsStored', 12), ('HighBit', 11), ('PixelRepresentation', 0), ('PixelData', b'Deferred loading of pixel data'), ('shape', (158, 256, 192)), ('sampling', (1.0127388535031847, 1.0, 1.0))])
This is done by indexing the Modality row of the meta column in the data imageio array.
print(data.meta['Modality'])
MR
Here we use plt.imshow() to show a slice of the brain such that the Corpus callosum is clearly visible in the image. The axis is turned off and annotate is used to provide a clean arrow and caption of anatomy region using a curved arrow to not distract the viewer.
plt.imshow(data[:,:,90], cmap='gray', origin='lower')
plt.axis('off')
plt.annotate("Corpus callosum",xy=(150,95), xytext=(220,150), arrowprops=dict(color='orange', connectionstyle='angle3', arrowstyle="->"), bbox= dict(fc='yellow'))
plt.show()
In this cell I first start by ensuring that there is no mp4 file already stored by using try if the file isn't there already then i simply continue on the except statement. A list is used for image file storage. I then iterate though the size of the data and use matplotlib.pyplot to plot the sagittal layers of the brain. If during this time I'm in the range of 80-100 then I annotate the the image. I then save the image using plt.savefig and storing it to cocalc's servers as a png. Next using imageio.imread the file is then reread from cocalc servers at which it is added to the images list, if traversal occurs in range of the Corpus callosum I add extra copies to the list to later simulate a slow motion effect. Calling plt.close() ensures the image will not interfere with the next iteration and os.remove() is called to free up storage space. Lastly imageio.mimsave is used to construct an mp4 file at 55 fps by iterating through the list filled with the sagittal plane png images through out the brain.
The code below demonstrates how I have learned and become proficient at input and output / file handling / annotations / matplotlib.pyplot and imageio methods beyond the original scope of the course.
try:
os.remove('Portfolio_Images/brain.mp4')
except:
a=0
images = []
for i in range(0, data.meta.shape[0]):
plt.imshow(data[:,:,i], cmap="gray", origin='lower')
if i in range(80, 100):
plt.annotate("Corpus callosum",xy=(150,95), xytext=(220,150), arrowprops=dict(color='orange', connectionstyle='angle3', arrowstyle="->"), bbox= dict(fc='yellow'))
plt.axis('off')
plt.savefig('Portfolio_Images/image.png', dpi= 100)
images.append(imageio.imread('Portfolio_Images/image.png'))
if i in range(80,95):
images.append(imageio.imread('Portfolio_Images/image.png'))
if i in range(85, 95):
images.append(imageio.imread('Portfolio_Images/image.png'))
plt.close()
os.remove('Portfolio_Images/image.png')
imageio.mimsave('Portfolio_Images/brain.mp4', images,fps=55)
IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1200, 700) to (1200, 704) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).