I am trying to make some segmentation tasks on CT images which are in DICOM format, using python.
The main problem is that when I am trying to read the DICOM file (https://www.dicomlibrary.com/meddream/md5/index.html?study=1.2.826.0.1.3680043.8.1055.1.20111102150758591.92402465.76095170) using pydicom library and use the pixel_array attribute to display the image, I`m getting an image with a much worse quality than the original (please see the code and screenshots from below).
dicom_file = pydicom.read_file('Anonymized20191102.dcm')
plt.imshow(dicom_file.pixel_array, cmap='gray')
plt.show()
My question, in order to better understand the problem is:
- What does pixel_array attribute represents? I have seen that it takes values in range [0, 2250].
Observations:
I also tried to get the hounsfield unit(HU) values by multiplying with slope and adding the bias, as following:
def convert_to_hu(dicom_file):
bias = dicom_file.RescaleIntercept
slope = dicom_file.RescaleSlope
pixel_values = dicom_file.pixel_array
new_pixel_values = (pixel_values * slope) + bias
return new_pixel_values
hu_pixels = convert_to_hu(dicom_file)
plt.imshow(hu_pixels, cmap='gray')
plt.show()
The result in terms of visualization is almost the same with the one with the original values from pixel_array. Also the values are in range of [-1000, 1250] (which I think is good given the fact that HU is usually between -1000 and 1000).