Displaying DICOM images in rudimentary Python as a medical professional

I am a radiological technologist and a medical physicist. I will leave a memorandum of the process of learning programming in the article. I hope it will be helpful to someone in the future.

I want to display a DICOM image

What do you want to do with Python in the medical setting? Python is widely used in medical image processing, and there are many themes such as automatic recognition of lesions from images in the field of machine learning. Images used in the medical field are data in the format of DICOM. It can be said that knowledge of DICOM is indispensable for programming in the medical field, mainly image processing. And the first barrier I want to try for the time being is ** "Display images in Python" **.

What is DICOM

First, let's organize DICOM (for myself).

DICOM is an abbreviation that connects the acronyms ** "Digital Imaging and Communications in Medicine" **. It is a standard used for communicating medical image information, etc., and is used worldwide. In the data to be communicated, not only the image data that is generally imaged, but also many items such as patient name, patient ID, examination date, device name, and imaging conditions are defined as incidental information.

There are various manufacturers of machines and communication equipment used for inspection, but data is smoother by using common rules that do not depend on manufacturers such as DICOM, instead of handling data with original rules. You can manage it.

In the past (still in some facilities), film X-rays were used for diagnosis, but the shift from analog to digital medical images has made it easier to manage vast amounts of medical data. The existence of DICOM also helps with that.

As an aside, the radiation therapy field has some special DICOM data that is not found in the diagnostic field. This is not necessary knowledge in the process of displaying images, but I am engaged in the radiotherapy department myself, so I will touch on a little. DICOM data (DICOM RT) for radiotherapy includes image-based data like DICOM of other image modality, and non-image-based data such as coordinate information and device setting information. alt RT dose represents dose information, RT Structure represents contool information, and RT Plan represents device operation information, each of which is linked as information for treatment.

[Reference] DICOM reverse lookup BOOK (You can view the seminar slides → DICOM study session materials)

In addition to this, there is a lot of useful information about DICOM in Japan Imaging Medical System Industry Association [JIRA]: The World of DICOM. It is posted, so please refer to it.

Image display is super easy with Python!

There was a time when I was studying C # for a while, I couldn't even display one slice of the CT image. So it's a pretty high hurdle for me to even display an image ... I thought it wasn't something I could do for the time being.

However!! It was easy with Python! I was surprised. Is it so easy? Thanks to the rich library, you can display it with just knowing the simple code. Thanks to those who made the library.

This time I will use DICOM images that can be used for free, so It doesn't matter if you don't have a DICOM image at hand. I am once again indebted to DICOM World. I'm just grateful.

Environment (Mac)

python 3.7.4 matplotlib 3.1.1 pydicom 1.2.2 Terminal (I don't use Jupyter Notebook) Atom (any text editor is acceptable)

Whole code

show_dicom.py


from matplotlib import pyplot as plt
import pydicom

cr = pydicom.read_file("CR_LEE_IR6a.dcm")

plt.imshow(cr.pixel_array, cmap = "gray")
plt.show()

** * Save the DICOM data (.dcm) to be used in the same directory as the code (.py). ** **

Output image

スクリーンショット 2020-04-11 19.16.57.png

that's all. Isn't it super easy? I was able to display the image in just 5 lines. You can do this with simple code by leveraging the great existing modules such as matplotlib and pydicom.

I will omit the Python grammar. Please try it.

Finally, I would like to introduce a very useful site. ** Euromedic Medical Physics Room ** Medical Physics Room Blog It contains detailed information that will be very helpful when using Python in the field of radiation therapy. There is no doubt that it will be useful, so if you are interested, check it out!

Recommended Posts

Displaying DICOM images in rudimentary Python as a medical professional
Working with DICOM images in Python
Get rid of DICOM images in Python
Getting rid of DICOM images in Python Part 2
Specify a subcommand as a command line argument in Python
Take a screenshot in Python
Base64 encoding images in Python 3
Create a function in Python
Create a dictionary in Python
Published a library that hides character data in Python images
Grayscale image is displayed as a color image in OpenCV / Python
Make a bookmarklet in Python
Draw a heart in Python
Investigating what could be used as a Markdown parser in Python
Get the formula in an excel file as a string in Python
Number recognition in images with Python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
Use pymol as a python library
[python] Manage functions in a list
Hit a command in Python (Windows)
Use fabric as is in python (fabric3)
Create a DI Container in Python
How to collect images in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Create a binary file in Python
Extract text from images in Python
Use blender as a python module
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Launch a Python script as a service
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Schedule a Zoom meeting in Python
When writing a program in Python
If you want a singleton in python, think of the module as a singleton
How to install python package in local environment as a general user
The eval () function that calculates a string as an expression in python
What happens if you do "import A, B as C" in Python?
Generate a first class collection in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Solve ABC175 A, B, C in Python
Use print in a Python2 lambda expression
Displaying candlestick charts in Python (matplotlib edition)
A simple HTTP client implemented in Python
Install Python as a Framework with pyenv
Do a non-recursive Euler Tour in Python
I made a payroll program in Python!
Precautions when pickling a function in python
Mosaic images in various shapes (Python, OpenCV)
Write the test in a python docstring
Display a list of alphabets in Python 3
Try sending a SYN packet in Python