Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read and open dicom images using python

Tags:

python-3.x

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.

like image 359
s.sowmiya Avatar asked Jan 10 '18 10:01

s.sowmiya


People also ask

How do I read a DICOM image?

DICOM files are images that come digitally from medical scans, such as MRIs and ultrasounds. You can view these files with a free online viewer called Jack Image viewer on any computer. If you'd prefer an app, you can download MicroDicom (PC only) or open the files in Adobe Photoshop (PC and Mac).

What is DICOM in Python?

DICOM files were introduced to maintain uniformity among varied types of medical image modalities. It is a standard format to view, store, share and retrieve medical images. Python offers a powerful module, pydicom to work with the DICOM files such as medical images, reports, and radiotherapy objects.

How do you read a DICOM tag?

Each DICOM file contains much more information than can be displayed as annotations on the image. To view the DICOM tags associated with the image displayed click Show DICOM tags menu item or use Ctrl + Alt + T . A complete list of DICOM tags, their descriptions and values will be displayed.

Can OpenCV read DICOM?

OpenCV does not support DICOM images so that you will have to find a suitable libary (like http://dicom.offis.de/dcmtk.php.en ) and convert the loaded image to a cv::Mat.


2 Answers

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

like image 185
Malgo Avatar answered Sep 20 '22 16:09

Malgo


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()
like image 34
Vartika Avatar answered Sep 18 '22 16:09

Vartika