## 1. Common medical image formats The data obtained after medical examination needs to be saved for subsequent reference and use. The examination data cannot usually be saved directly as image data because a lot of key information will be lost, so it needs to be saved in a dedicated medical image format. In actual use, the most commonly used medical image formats are DICOM and Nifti. DICOM and NIfTI are two image data formats widely used in the field of medical imaging, each of which has different characteristics and application scenarios. The comparison of these two formats is as follows: ### DICOM format 1 Comprehensiveness: DICOM is an international standard for the acquisition, storage, printing and transmission of medical images. It contains not only image data, but also rich patient information, examination information and equipment parameters. 2 Compatibility: Files in DICOM format are widely accepted, and almost all medical imaging equipment and image processing software support DICOM format. 3 Structurality: DICOM files consist of a series of two-dimensional images, each file represents a separate slice, and can contain multiple slices to form a series of images. 4 Information storage: A large amount of metadata is stored in DICOM files, including patient information, examination parameters, image pixel data, etc. ## NIfTI format 1. Specialization: The NIfTI format was originally designed for neuroimaging, and it is particularly suitable for storing three-dimensional or four-dimensional brain imaging data. 2. Simplicity: The NIfTI format usually uses a single file to store image data and related metadata, which makes file management easier. 3. Extensibility: The NIfTI format supports a single file with the extension .nii or two files .img/.hdr, the latter of which maintains compatibility with the ANALYZE format. 4. Analysis-friendly: The NIfTI format is very popular in neuroscience research because it facilitates image analysis and processing, especially when performing three-dimensional or higher-dimensional image analysis. ### Comparison 1. Storage method: DICOM is usually stored in multiple two-dimensional slices, while NIfTI is usually stored in a single three-dimensional or four-dimensional data set. 2. Information richness: DICOM contains richer patient information and equipment parameters, while NIfTI focuses on the image data itself, with necessary spatial positioning information. 3. Usage scenarios: DICOM is suitable for the storage and transmission of clinical medical images, while NIfTI is more suitable for scientific research, especially in neuroimaging analysis. 4. Software support: Although both formats are widely supported, DICOM is more common in medical imaging equipment and hospital information systems, while NIfTI is more popular in scientific research software and image analysis tools. In Python, through the appropriate library, support for DICOM and Nifti image format data can be well provided. ## 2. DICOM image format In python, install pydicom to provide support for this format. ``` pip install pydicom ``` In addition, you need to install other libraries to provide support for subsequent learning: ``` pip install numpy pip install pillow pip install pytest ``` After installing the support library, a variety of test data will be provided by default:
You can call it using the following code: ``` import pydicom from pydicom.data import get_testdata_files # Get Pydicom built-in DICOM file filename = get_testdata_files('MR_small.dcm') #Read DICOM file ds = pydicom.dcmread(filename[0]) print(ds) ``` For actual learning, I downloaded the Covid_Scans dataset and copied two files for testing:
Referring to the above code, write a reading program: ``` import pydicom from pydicom.data import get_testdata_files # Read the downloaded test data file ds = pydicom.dcmread('data/56364397.dcm') # ds = pydicom.dcmread('data/56364823.dcm') print(ds) ``` After running, the output is as follows:
Through the description of the main parameters in the book, you can understand the specific meaning of the data fields:
However, just looking at these data is still not very intuitive. It would be more appropriate if they could be restored to pictures for presentation. After understanding, you can use the following code: ``` import numpy as np from PIL import Image new_image = ds.pixel_array.astype(float) scaled_image = (np.maximum(new_image, 0) / new_image.max()) * 255.0 scaled_image = np.uint8(scaled_image) final_image = Image.fromarray(scaled_image) final_image.show() ``` In the above code, the data is converted to np format, and then Pillow is used to read the data and convert it into the final image for presentation. The specific effect is as follows:
You can see that this file is the relevant information when the image data is detected. Using another data, the image obtained is as follows:
This is the image corresponding to the actual lung detection data. ## 3. Nifti image format Similarly, install several libraries to provide support first: ``` pip install nibabel pip install opencv-python pip install imageio pip install matplotlib ``` nibabel is a library that provides support for .nii files corresponding to Nifti, and also provides a test data set:
The code for calling the test data is as follows: ``` import os import nibabel as nib from nibabel.testing import data_path file_path = os.path.join(data_path, 'example4d.nii.gz') img = nib.load(file_path) ``` nibabel.testing.data_path indicates the corresponding test data set directory after the above installation. In order to actually learn, I downloaded a test data set and used it as follows:
Then use the following code to read it: ``` import os import nibabel as nib from nibabel.testing import data_path file_path = 'data/nifti/OBJECT_phantom_T2W_TSE_Tra_17_1.nii' img = nib.load(file_path) ``` The specific information corresponding to the data can be read in the following way: ``` #Radiation matrix affine = img.affine print(affine) # Metadata header = img.header print(header) # Access header metadata print(header['sizeof_hdr']) # Modify # header['sizeof_hdr'] = 200 # Read image data data = img.get_fdata() print(data.dtype, data.shape) ``` The output is as follows:
Similarly, I also studied and converted it into the corresponding picture for presentation. The specific code is as follows: ``` import cv2 import numpy as np img_fdata=(data-data.min())/(data.max()-data.min())*255 #Start converting image (x,y,z) = img.shape for i in range(z): #It is the image sequence of z slice = img_fdata[:, :, i] #Choose which direction of the slice to decide for yourself # print(os.path.join(img_f_path, '{}.png'.format(i))) # By understanding the main medical image formats used, we will be able to perform actual operations on the data set later, such as extracting key information and data, and performing secondary processing of the data.