Processing medical images data files for deep learning applications
- Admin

- Jan 13, 2023
- 5 min read
By Dr Mabrouka Abuhmida
A CT (Computed Tomography) scan is a medical imaging procedure that uses x-rays and computer processing to create detailed images of the inside of the body. The resulting images are typically stored in a digital format, such as a DICOM (Digital Imaging and Communications in Medicine) file.
An MRI (Magnetic Resonance Imaging) scan is another medical imaging procedure that uses a magnetic field and radio waves to create detailed images of the inside of the body. The resulting images are typically stored in a digital format, such as a DICOM file or a proprietary file format specific to the MRI machine used.
The choice of which procedure to use depends on the specific medical question being asked and the characteristics of the body part being imaged.

CT scan files are typically stored in the DICOM (Digital Imaging and Communications in Medicine) file format, which is a standard for storing and transmitting medical images. DICOM files contain both image data and metadata, such as the patient’s name, the date of the scan, and the image acquisition parameters.
A DICOM file consists of a header and a payload. The header contains metadata about the image, such as its modality (e.g., CT, MRI, PET), the patient’s name and ID, the acquisition parameters (e.g., slice thickness, kVp, mAs), and the image dimensions. The payload contains the actual image data, which is typically stored as a series of 2D image slices.
Here is an example of the structure of a DICOM file:
Header:
- File Meta Information Group Length
- File Meta Information Version
- Media Storage SOP Class UID
- Media Storage SOP Instance UID
- Transfer Syntax UID
- Implementation Class UID
- Implementation Version Name
- Source Application Entity Title
- Private Information Creator UID
- Private Information
Payload:
- (Image Data)To access and process CT scan data stored in DICOM files, you can use a library like pydicom, which provides functions for reading, writing, and manipulating DICOM files.
Here is an example of how you might use pydicom to read a CT scan from a DICOM file and convert it to a NumPy array:
import pydicom
import numpy as np
# Load the DICOM file
dcm_file = pydicom.dcmread('ct_scan.dcm')
# Extract the image data from the DICOM file
image_data = dcm_file.pixel_array
# Convert the image data to a NumPy array
image_array = np.array(image_data)General process you can follow to process CT scan files for use in deep learning models:
Pre-process the CT scan data:
Convert the CT scan data into a suitable format, such as a 3D NumPy array or a PyTorch tensor.
Normalize the data by subtracting the mean and dividing by the standard deviation. This can help improve the performance of your model.
Split the data into training, validation, and test sets.
Here is an example of how you might pre-process CT scan data using PyTorch:
import torch
import numpy as np
# Load the CT scan data
ct_scan = load_ct_scan_data()
# Convert the data to a PyTorch tensor
ct_scan = torch.from_numpy(ct_scan).float()
# Normalize the data
mean = ct_scan.mean()
std = ct_scan.std()
ct_scan = (ct_scan - mean) / std
# Split the data into training, validation, and test sets
train_data, val_data, test_data = split_data(ct_scan)Here is an example of how you might pre-process CT scan data using NumPy and Keras:
import numpy as np
# Load the CT scan data
ct_scan = load_ct_scan_data()
# Normalize the data
mean = ct_scan.mean()
std = ct_scan.std()
ct_scan = (ct_scan - mean) / std
# Split the data into training, validation, and test sets
train_data, val_data, test_data = split_data(ct_scan)Define a deep learning model:
Choose a suitable deep learning model, such as a convolutional neural network (CNN) or a 3D convolutional neural network (3D CNN).
Define the model architecture, including the number of layers, the type of layers, and the number of filters or units in each layer.
Here is an example of how you might define a 3D CNN model using PyTorch:
import torch.nn as nn
classCTScanModel(nn.Module):
def__init__(self):
super(CTScanModel, self).__init__()
self.conv1 = nn.Conv3d(1, 32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv3d(32, 64, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv3d(64, 128, kernel_size=3, stride=1, padding=1)
self.conv4 = nn.Conv3d(128, 256, kernel_size=3, stride=1, padding=1)
self.pool = nn.MaxPool3d(2, 2)
self.fc1 = nn.Linear(256 * 5 * 5 * 5, 512)
self.fc2 = nn.Linear(512, 1)
self.sigmoid = nn.Sigmoid()
defforward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = self.pool(F.relu(self.conv3(x)))
x = self.pool(F.relu(self.conv4(x)))
x = x.view(-1, 256 * 5 * 5 * 5)
x = F.relu(self.fc1(x))
x = self.fc2(x)
x = self.sigmoid(x)Here is an example of how you might define a 3D CNN model using Keras:
from keras.layers import Conv3D, MaxPooling3D, Flatten, Dense
from keras.models import Sequential
model = Sequential()
model.add(Conv3D(32, kernel_size=3, activation='relu', input_shape=(32, 32, 32, 1)))
model.add(MaxPooling3D(pool_size=2))
model.add(Conv3D(64, kernel_size=3, activation='relu'))
model.add(MaxPooling3D(pool_size=2))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
Train the model:
Compile the model with a suitable loss function and optimizer.
Train the model on the training data using the fit() method, specifying the validation data and the number of epochs.
Here is an example of how you might train the 3D CNN model using Keras:
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(train_data, train_labels, validation_data=(val_data, val_labels), epochs=10)Evaluate the model:
Use the evaluate() method to evaluate the model on the test data.
Plot the training and validation loss and accuracy to visualize the model’s performance.
Here is an example of how you might evaluate the model using Keras:
test_loss, test_acc = model.evaluate(test_data, test_labels)
print('Test loss:', test_loss)
print('Test accuracy:', test_acc)
# Plot the training and validation loss and accuracy
import matplotlib.pyplot as pltThere are several public datasets available that contain brain CT and MRI scans, as well as associated clinical and demographic information. Here are a few examples:
The National Institutes of Health (NIH) National Library of Medicine’s National Training and Extension Grant for Biomedical Informatics (NIH grant number 5U24EB020404) has a collection of over 1,500 anonymized brain MRI and CT scans, along with associated clinical and demographic data. This dataset, known as the “NIH MRI/CT dataset,” is available for download and use by researchers.
The Alzheimer’s Disease Neuroimaging Initiative (ADNI) is a long-term study that aims to identify early biomarkers of Alzheimer’s disease using various imaging techniques, including MRI and CT. The ADNI dataset includes brain MRI and CT scans, as well as associated clinical and demographic data, for over 2,000 subjects.
The Open Access Series of Imaging Studies (OASIS) is a publicly available dataset that includes brain MRI scans and associated demographic and cognitive data for over 400 healthy elderly subjects and over 100 subjects with mild cognitive impairment or Alzheimer’s disease.
These are just a few examples of the many brain CT and MRI datasets that are available for research purposes. It is important to note that these datasets typically have strict rules regarding use and distribution, and researchers must obtain the appropriate permissions before accessing or using the data.





Comments