s.sowmiya
s.sowmiya

Reputation: 279

read and open dicom images using python

how to read and display dicom images using python. I am new to python and IT field. please some one briefly tell me about the packages and libraries needed for dicom image processing and codes for opening dicom images.

Upvotes: 27

Views: 57845

Answers (2)

Vartika Sharma
Vartika Sharma

Reputation: 269

If you are working on Windows, I suggest you install Anaconda for working with Python. It has most of the libraries and packages.

I suggest you start with installing the following libraries:

  1. pydicom (for python version > 3.0) Link: https://anaconda.org/conda-forge/pydicom

  2. Matplotlib (if not working in Anaconda)

  3. Numpy (if not working in Anaconda)

The following code should display your images:

import numpy as np
import matplotlib.pyplot as plt
import os, glob
import pydicom
import pylab as pl
import sys
import matplotlib.path as mplPath

class IndexTracker(object):
    def __init__(self, ax, X):
        self.ax = ax
        ax.set_title('Scroll to Navigate through the DICOM Image Slices')

        self.X = X
        rows, cols, self.slices = X.shape
        self.ind = self.slices//2

        self.im = ax.imshow(self.X[:, :, self.ind])
        self.update()

    def onscroll(self, event):
        print("%s %s" % (event.button, event.step))
        if event.button == 'up':
            self.ind = (self.ind + 1) % self.slices
        else:
            self.ind = (self.ind - 1) % self.slices
        self.update()

    def update(self):
        self.im.set_data(self.X[:, :, self.ind])
        ax.set_ylabel('Slice Number: %s' % self.ind)
        self.im.axes.figure.canvas.draw()

fig, ax = plt.subplots(1,1)

os.system("tree C:/Users/Dicom_ROI")

plots = []

for f in glob.glob("C:/Users/Dicom_ROI/AXIAL_2/*.dcm"):
    pass
    filename = f.split("/")[-1]
    ds = pydicom.dcmread(filename)
    pix = ds.pixel_array
    pix = pix*1+(-1024)
    plots.append(pix)

y = np.dstack(plots)

tracker = IndexTracker(ax, y)

fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()

Upvotes: 7

Malgo
Malgo

Reputation: 2168

DICOM images are generally used to store medical images. They are highly informative. Along with image data it stores lots of key patient information, such as, patient’s name, age, sex, doctor’s name etc.

If you just want to preview dicom images without extracting any information then use the following code. You need to install pydicom python package, you can install using pip install pydicom -

import pydicom as dicom
import matplotlib.pylab as plt

# specify your image path
image_path = 'data/train_images/sample.dcm'
ds = dicom.dcmread(image_path)

plt.imshow(ds.pixel_array)

If you want to convert the image to png/jpg then you can do the following -

import pydicom as dicom
import cv2   

# specify your image path
image_path = 'data/train_images/sample.dcm'
ds = dicom.dcmread(image_path)

pixel_array_numpy = ds.pixel_array

image_format = '.jpg' # or '.png'
image_path = image_path.replace('.dcm', image_format)

cv2.imwrite(image_path, pixel_array_numpy)

source: https://medium.com/@vivek8981/dicom-to-jpg-and-extract-all-patients-information-using-python-5e6dd1f1a07d

Upvotes: 26

Related Questions