{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em\"><b>Aim of this notebook 📚</b></p>\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">If you're a beginner in Data Science like me, you probably have not worked with DICOM files before. This notebooks explains what DICOM files are and how to access all the metadata that comes with them.</p>","metadata":{}},{"cell_type":"markdown","source":"<h1><center>What is DICOM?</center></h1>\n\n<img src='https://www.dicomstandard.org/images/librariesprovider2/default-album/hero-1.jpg'/>\n\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">DICOM® — <span style=\"color:crimson;\">Digital Imaging and Communications in Medicine</span> — is the international standard for medical images and related information. It defines the formats for medical images that can be exchanged with the data and quality necessary for clinical use.</p>\n\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">DICOM® is implemented in almost every radiology, cardiology imaging, and radiotherapy device (X-ray, CT, MRI, ultrasound, etc.), and increasingly in devices in other medical domains such as ophthalmology and dentistry. With hundreds of thousands of medical imaging devices in use, DICOM® is one of the most widely deployed healthcare messaging Standards in the world. There are literally billions of DICOM® images currently in use for clinical care.</p>\n\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">Since its first publication in 1993, DICOM® has revolutionized the practice of radiology, allowing the replacement of X-ray film with a fully digital workflow. Much as the Internet has become the platform for new consumer information applications, DICOM® has enabled advanced medical imaging applications that have “changed the face of clinical medicine”. From the emergency department, to cardiac stress testing, to breast cancer detection, DICOM® is the standard that makes medical imaging work — for doctors and for patients.</p>\n\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">DICOM® is recognized by the International Organization for Standardization as the ISO 12052 standard.</p>\n\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">Learn more about it here: <a href=\"https://www.dicomstandard.org/\" target=\"_blank\">dicomstandard.org</a></p>","metadata":{}},{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em\"><h3>Reading a DICOM File</h3></p>\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">DICOM files can be read using the <code>pydicom</code> module in Python.</p>\n","metadata":{}},{"cell_type":"code","source":"import pydicom as dicom\n\n# DCM FIle path\nfile_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR/Image-105.dcm'\n\n# Reading the DCM File\nd_file = dicom.dcmread(file_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T13:42:17.125912Z","iopub.execute_input":"2021-07-14T13:42:17.126351Z","iopub.status.idle":"2021-07-14T13:42:17.141792Z","shell.execute_reply.started":"2021-07-14T13:42:17.126314Z","shell.execute_reply":"2021-07-14T13:42:17.140246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em\"><h3>Plotting The Image</h3></p>\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">This is simple to do using the <code>matplotlib</code> module.</p>","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plotting the image\nplt.imshow(d_file.pixel_array, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T13:42:17.144336Z","iopub.execute_input":"2021-07-14T13:42:17.144820Z","iopub.status.idle":"2021-07-14T13:42:17.340300Z","shell.execute_reply.started":"2021-07-14T13:42:17.144772Z","shell.execute_reply":"2021-07-14T13:42:17.339051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em\"><h3>DICOM Metadata</h3></p>\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">If you notice the code above, we use <code>d_file.pixel_array</code> to plot the image. <code>pixel_array</code> stores the image information in 2D arrays, similar to how we often read images using OpenCV. Lets try printing out <code>d_file</code> itself.</p>","metadata":{}},{"cell_type":"code","source":"print(d_file)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T13:42:17.342545Z","iopub.execute_input":"2021-07-14T13:42:17.342927Z","iopub.status.idle":"2021-07-14T13:42:17.352390Z","shell.execute_reply.started":"2021-07-14T13:42:17.342894Z","shell.execute_reply":"2021-07-14T13:42:17.350848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em\"><h3>Accessing DICOM Metadata</h3></p>\n<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">As seen above, a single dicom file has a ton of metadata stored about the image which can be really helpful for building models. How do we access this information? Use <code>dir(d_file)</code> to see a list of all the metadata that can be accessed about the image.</p>","metadata":{}},{"cell_type":"code","source":"dir(d_file)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T13:42:17.354167Z","iopub.execute_input":"2021-07-14T13:42:17.354484Z","iopub.status.idle":"2021-07-14T13:42:17.370462Z","shell.execute_reply.started":"2021-07-14T13:42:17.354454Z","shell.execute_reply":"2021-07-14T13:42:17.369306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">Once you know all information that is available, it is easy to access it. It is similar to how you would access a column in a Pandas Dataframe.</p>","metadata":{}},{"cell_type":"code","source":"print('Modality:', d_file.Modality)\nprint('Patient ID:', d_file.PatientID)\nprint('Imaging Frequency:', d_file.ImagingFrequency)\nprint('Patient Position:', d_file.PatientPosition)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T13:42:17.372233Z","iopub.execute_input":"2021-07-14T13:42:17.372575Z","iopub.status.idle":"2021-07-14T13:42:17.384328Z","shell.execute_reply.started":"2021-07-14T13:42:17.372544Z","shell.execute_reply":"2021-07-14T13:42:17.383424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"font-size:15px; font-family:verdana; line-height: 1.7em; text-align: justify\">Checkout this <a href='https://www.kaggle.com/xhlulu/siim-covid-19-convert-to-jpg-256px?scriptVersionId=63196459'>notebook</a> for the script to convert DICOM images to JPG</p>","metadata":{}}]}