Home > Backend Development > Python Tutorial > Sharing of code examples for reading DICOM images in python

Sharing of code examples for reading DICOM images in python

黄舟
Release: 2017-07-17 14:40:48
Original
3272 people have browsed it

DICOM (Digital Imaging and Communications in Medicine) is the international standard for medical images and related information (ISO 12052). The following article mainly introduces to you the relevant information about python reading DICOM images. Friends in need can refer to it. Let’s take a look together.

DICOM introduction

DICOM3.0 images are generated by medical imaging equipment to produce standard medical imaging images. DICOM is widely used in radiology medicine, cardiology Vascular imaging and radiological diagnosis and treatment diagnostic equipment (X-ray, CT, MRI, ultrasound, etc.), and are increasingly used in other medical fields such as ophthalmology and dentistry. With tens of thousands of medical imaging devices in use, DICOM is one of the most widely deployed medical information standards. Currently, there are approximately tens of billions of medical images that comply with DICOM standards for clinical use.

How to read the seemingly mysterious image file? A casual search on the Internet will reveal many methods, but there is a lack of a more systematic method of use. The following article will combine Baidu information and python2.7 to explain how to read and use DICOM images.

Reading DICOM images requires the following libraries: pydicom, CV2, numpy, matplotlib. pydicom is a python special package that specializes in processing dicom images, numpy is a package that efficiently handles scientific calculations, and is a library for drawing

based on data.

##Installation:

pip install matplotlib
Copy after login
pip install opencv-python #opencv的安装,小度上基本都是要下载包,安装包后把包复制到某个文件夹下,
#后来我在https://pypi.python.org/pypi/opencv-python找到这种pip的安装方法,亲测可用
Copy after login
pip install pydicom
Copy after login
pip install numpy
Copy after login
If I remember correctly, numpy will also be automatically installed when pydicom is installed.

After installing these libraries, you can operate dicom files.

Look at the following code for details:

#-*-coding:utf-8-*-
import cv2
import numpy
import dicom
from matplotlib import pyplot as plt

dcm = dicom.read_file("AT0001_100225002.DCM")
dcm.image = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept

slices = []
slices.append(dcm)
img = slices[ int(len(slices)/2) ].image.copy()
ret,img = cv2.threshold(img, 90,3071, cv2.THRESH_BINARY)
img = numpy.uint8(img)

im2, contours, _ = cv2.findContours(img,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
mask = numpy.zeros(img.shape, numpy.uint8)
for contour in contours:
 cv2.fillPoly(mask, [contour], 255)
img[(mask > 0)] = 255


kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2))
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)


img2 = slices[ int(len(slices)/2) ].image.copy()
img2[(img == 0)] = -2000


plt.figure(figsize=(12, 12))
plt.subplot(131)
plt.imshow(slices[int(len(slices) / 2)].image, 'gray')
plt.title('Original')
plt.subplot(132)
plt.imshow(img, 'gray')
plt.title('Mask')
plt.subplot(133)
plt.imshow(img2, 'gray')
plt.title('Result')
plt.show()
Copy after login
In the DICOM image, it contains a dictionary of patient-related information. We can view DICOM through dir What information does the file have? Related values ​​can be returned through the dictionary.
import dicom
import json
def loadFileInformation(filename):
 information = {}
 ds = dicom.read_file(filename)
 information['PatientID'] = ds.PatientID
 information['PatientName'] = ds.PatientName
 information['PatientBirthDate'] = ds.PatientBirthDate
 information['PatientSex'] = ds.PatientSex
 information['StudyID'] = ds.StudyID
 information['StudyDate'] = ds.StudyDate
 information['StudyTime'] = ds.StudyTime
 information['InstitutionName'] = ds.InstitutionName
 information['Manufacturer'] = ds.Manufacturer
 print dir(ds)
 print type(information)
 return information

a=loadFileInformation('AT0001_100225002.DCM')
print a
Copy after login

Summary

The above is the detailed content of Sharing of code examples for reading DICOM images in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template