I'm working with multiple DICOM files, most of which are vastly different pixel wise. For example one ranges from -1024 to 2815 and another ranges from 0 to 2378. Is there a way to standardise them all into the same range. Also to note I'm utilising python and the pydicom library. Thanks in advance.
If you're dealing with the same IOD for all your data (e.g. CT Image) and the image data is already in HU (and to be certain of this you can use the apply_modality_lut() function in pydicom):
from pydicom import dcmread
from pydicom.pixel_data_handlers.util import apply_modality_lut
ds = dcmread('filename.dcm')
arr = ds.pixel_array # Raw unitless pixel data
hu = apply_modality_lut(arr, ds) # Pixel data has been converted to HU (for CT)
Then your data is already in a specific quantity (namely HU). To convert to a specific range then just means deciding what to do with values outside your chosen range:
import numpy as np
# Clip values outside the range to the min/max of the range
clipped_hu = np.clip(hu, -1024, 1024)
Of course this means any clipped pixels should no longer be considered an accurate representation of your input data.
I'm going to add a bit of an explanation about the difference between rescaling DICOM data and windowing it.
Imagine you have two rulers, one in cm and one in inches. You apply the DICOM rescale operation to both - in pydicom you use apply_modality_lut()
- and you'll get back two rulers in cm. Now you can use your rulers to measure stuff and you'll get the same values back from both, neat!
Now you take your rulers and apply the same DICOM windowing operation to both. The windowing operation is a bit less analogy friendly, in essence you're taking a small window of your rulers (say the section from 10 to 12 cm) and stretching it to be the same length as the entire ruler was beforehand. Your rulers are no longer in cm and can't be used to measure stuff, but perhaps by expanding that section you see some detail that wasn't obvious before (like a tumour). The other nice thing about applying the same rescale + windowing operations to both is that you can still meaningfully compare your two rulers against each other because they've both been stretched the same amount.
So the rescale operation is all about converting raw data to a quantity to allow direct intercomparison while the windowing operation is about visualising something. If you want to know the highest density region of a CT scan you'd use a rescale operation with no windowing. If you want to see what a radiologist saw when writing their report, you'd apply both rescale and windowing operations.