Reading .nrrd and .dcm file formats for medical image visualization and preprocessing

nrrd data format

MITK will save medical images as images in the NRRD format by default. This data format contains:

1. A single data header file: accurately represents N-dimensional raster information for scientific visualization and medical image processing.
2. Image files that can be separated and merged.

nrrd_options output
{u'dimension': 3, # Dimension
u'encoding': 'raw', # Encoding
u'endian': 'little', #
u'keyvaluepairs': {},
u'kinds': ['domain ', 'domain', 'domain'], # Types of three dimensions
u'sizes': [30, 30, 30], # Sizes of three dimensions
u'space': 'left-posterior-superior', # Spatial information
u'space directions': [['1', '0', '0'], ['0', '1', '0'], ['0', '0', '1'] ],
u'space origin': ['0', '0', '0'],
u'type': 'short'}

dcm data format

DICOM (Digital Imaging and Communications in Medicine) refers to the medical digital image transmission protocol. It is a set of common standard protocols for medical image processing, storage, printing, and transmission.
Files in DCM format store various important information. Among them, the commonly used ones are
(1) Patient ID: Patient ID (generally consistent with the patient's folder name);
(2) Series Number: The serial numbers belonging to the same three-dimensional data should be the same, for example, they are all in the same CT image. slice, then the value is the same. Use this value to determine whether it belongs to the same three-dimensional data (a patient may have taken multiple CT images); (3) Image Position: The
spatial position of the pixel in the upper left corner of the slice. In short, use Series Number to find all the slices belonging to the same three-dimensional image in the patient folder, and then use Image Position to sort these slices and reconstruct them into three-dimensional data.

Only 2D reading is shown here

Code

import nrrd
import pydicom
import numpy as np
import matplotlib.pyplot as plt

# dicom文件
path = ".dcm"
ds = pydicom.dcmread(path,force=True)
data = np.array(ds.pixel_array)
plt.figure()
plt.imshow(data,cmap='gray')
plt.axis('off')
plt.show()


#nrrd文件
nrrd_filename = '.nrrd'
nrrd_data, nrrd_options = nrrd.read(nrrd_filename)
print(nrrd_data.shape)
data =nrrd_data[:,:,50]#取切片
data = np.array(ds.pixel_array)
def WLww(image,WL,WW):
    min_v = (2 * WL - WW) / 2.0 
    max_v = (2 * WL + WW) / 2.0
    img1= (image-min_v)/WW
    img1[img1>1]  = 1
    img1[img1<0]  = 0
    img1=img1*255
    return img1
data = WLww(data,20,400)
plt.figure()
plt.imshow(data,cmap='gray')
plt.axis('off')
plt.show()

Results display

Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/fcxgfdjy/article/details/133394632