Working with DICOM images in Python

Trigger

At the Kaggle Competition, CT scan images and DICOM images of the chest came out and I didn't know how to handle them.

What you can do

・ What is a DICOM image? ・ How to display DICOM images and handle data

What is a DICOM image?

DICOM is a standard that defines the formats of medical images taken by CT, MRI, CR, etc. and the communication protocol between the medical image devices that handle them. wiki

Example of use

Installation

%pip install pydicom

dataset contains metadata

import pydicom
import matplotlib.pyplot as plt
from pydicom.data import get_testdata_files

filename = get_testdata_files('CT_small.dcm')[0]
dataset = pydicom.dcmread(filename)
print(dataset)

image.png

The value can be displayed and accessed by key name (.Rows) or number (0x0010,0x0010).

dataset[0x0010, 0x0010]
dataset.Rows

image.png

Until the image is displayed

pat_name = dataset.PatientName
display_name = pat_name.family_name + ", " + pat_name.given_name

print("Patient's name...:", display_name)
print("Patient id.......:", dataset.PatientID)
print("Modality.........:", dataset.Modality)
print("Study Date.......:", dataset.StudyDate)
print(dataset[0x0018, 0x0050])

if 'PixelData' in dataset:
    rows = int(dataset.Rows)
    cols = int(dataset.Columns)
    print("Image size.......: {rows:d} x {cols:d}, {size:d} bytes".format(
        rows=rows, cols=cols, size=len(dataset.PixelData)))
    if 'PixelSpacing' in dataset:
        print("Pixel spacing....:", dataset.PixelSpacing)

# use .get() if not sure the item exists, and want a default value if missing
print("Slice location...:", dataset.get('SliceLocation', "(missing)"))


# plot the image using matplotlib
plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)
plt.show()

image.png

reference

pydicom

Recommended Posts

Working with DICOM images in Python
Working with LibreOffice in Python
Working with sounds in Python
Number recognition in images with Python
Try working with binary data in Python
Post Test 3 (Working with PosgreSQL in Python)
Get rid of DICOM images in Python
Read text in images with python OCR
Convert PDFs to images in bulk with Python
Getting rid of DICOM images in Python Part 2
Try working with Mongo in Python on Mac
[Introduction for beginners] Working with MySQL in Python
Bordering images with python Part 1
Base64 encoding images in Python 3
Scraping with selenium in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Python: Working with Firefox with selenium
Scraping with Selenium in Python
Tweet with image in Python
Combined with permutations in Python
Specific sample code for working with SQLite3 in Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Scraping with Selenium in Python (Basic)
How to collect images in Python
CSS parsing with cssutils in Python
Open UTF-8 with BOM in Python
Extract text from images in Python
Post multiple Twitter images with python
Animate multiple still images with Python
Use Python in pyenv with NeoVim
Load gif images with Python + OpenCV
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Object extraction in images by pattern matching using OpenCV with Python
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Exclusive control with lock file in Python
Device monitoring with On-box Python in IOS-XE
Amplify images for machine learning with python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Capturing images with Pupil, python and OpenCV
Page cache in Python + Flask with Flask-Caching
Put Japanese fonts in images with Colaboratory
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Working with OpenStack using the Python SDK
Working with Azure CosmosDB from Python Part.2
Dealing with "years and months" in Python
Process multiple lists with for in Python