Medical image analysis with Python 1 (Read MRI image with SimpleITK)

Introduction

There are several libraries that read DICOM, but this article will explain how to use SimpleITK to read medical images such as MRI and CT.

What is Simple ITK

SimpleITK is a library that makes it easy to use ITK, a format for medical image analysis, in Python and other languages. (Https://en.wikipedia.org/wiki/SimpleITK) It is also used in software such as 3D Slicer, ImageJ, and ITK-Snap.

Medical image format

For example, MRI images are often used as DICOM data, and Simple ITK can read DICOM directly. Also, DICOM images are often saved as NIFTI (.nii, .nii.gz) or NRRD format (.nrrd), but can be read as well. (Https://simpleitk.readthedocs.io/en/master/IO.html) However, regarding the output, it seems that it cannot be output in DICOM format.

Purpose

Images read in SimpleITK format can be displayed and converted to ndarray format for processing and analysis. The purpose of using this library is that it was necessary to load images in SimpleITK format on another library for image analysis called PyRadiomics. I have summarized it as my own learning record.

environment

You can do the same with Google Collaboratory, but uploading local DICOM data etc. is troublesome, so we built the environment.

Install SimpleITK

conda install -c simpleitk simpleitk

You can install it normally with pip.

Loading images

It is assumed that multiple DICOM images are saved in the same folder.


import SimpleITK as sitk
import numpy as np
import matplotlib.pyplot as plt

#Load Dicom
imgdir = "Directory name where DICOM exists"
sys.argv[1] = imgdir
reader = sitk.ImageSeriesReader()

dicom_names = reader.GetGDCMSeriesFileNames(sys.argv[1])
reader.SetFileNames(dicom_names)
image = reader.Execute() #Image read in ITK format

#When reading NIFTI or NRRD
image = sitk.ReadImage("Image directory")

#Get image size
size = pre.GetSize()
print("Image size:", size[0], size[1], size[2])

#Convert ITK format to ndarray format
ndImage = sitk.GetArrayFromImage(image)
#Display image
plt.imshow(ndImage[n], cmap='gray') #n shows for any slice

Summary & Usage of loaded images

In the above code, we checked the size and displayed it briefly. There are various uses after loading an image. For example, since it is in 3D ndarray format, you can see the signal value of any voxel with print. I would like to summarize in another article what kind of analysis can be done and output of images.

Recommended Posts

Medical image analysis with Python 1 (Read MRI image with SimpleITK)
Data analysis with python 2
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
Voice analysis with python
Image processing with Python
Voice analysis with python
Data analysis with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
Read QR code from image file with Python (Mac)
Image processing with Python (Part 2)
[Python] Morphological analysis with MeCab
Read image coordinates with Python-matplotlib
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Sentiment analysis with Python (word2vec)
Sorting image files with Python (2)
Sorting image files with Python (3)
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Planar skeleton analysis with Python
Japanese morphological analysis with Python
Image processing with Python (Part 3)
Muscle jerk analysis with Python
[Python] Image processing with scikit-image
Read json data with python
Cut out an image with python
[Python] Using OpenCV with Python (Image Filtering)
3D skeleton structure analysis with Python
[Python] Using OpenCV with Python (Image transformation)
Impedance analysis (EIS) with python [impedance.py]
Text mining with Python ① Morphological analysis
Image processing with Python 100 knocks # 3 Binarization
[python] Read information with Redmine API
Let's do image scraping with Python
Read files in parallel with Python
Find image similarity with Python + OpenCV
Image processing with Python 100 knocks # 2 Grayscale
Data analysis starting with python (data visualization 1)
Introduction to image analysis opencv python
Read fbx from python with cinema4d
Logistic regression analysis Self-made with python
Data analysis starting with python (data visualization 2)
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Marketing analysis with Python ① Customer analysis (decyl analysis, RFM analysis)
Two-dimensional saturated-unsaturated osmotic flow analysis with Python
Create a dummy image with Python + PIL.
Image processing with Python 100 knocks # 8 Max pooling
Machine learning with python (2) Simple regression analysis
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
2D FEM stress analysis program with Python
[C] [python] Read with AquesTalk on Linux
Let's read the RINEX file with Python ①
Use cryptography library cryptography with Docker Python image
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Image acquisition from camera with Python + OpenCV
Tweet analysis with Python, Mecab and CaboCha