Home > Article > Backend Development > Sharing of code examples for reading DICOM images in python
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##Installation:pip install matplotlib
pip install opencv-python #opencv的安装,小度上基本都是要下载包,安装包后把包复制到某个文件夹下,
#后来我在https://pypi.python.org/pypi/opencv-python找到这种pip的安装方法,亲测可用
pip install pydicom
pip install numpy
If I remember correctly, numpy will also be automatically installed when pydicom is installed.
#-*-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()
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 aSummary
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!