AttributeError: 'FileDataset' object has no attribute 'RescaleIntercept'
def get_pixels_hu(scans):
image = np.stack([s.pixel_array for s in scans])
# Convert to int16 (from sometimes int16),
# should be possible as values should always be low enough (<32k)
image = image.astype(np.int16)
# Set outside-of-scan pixels to 1
# The intercept is usually -1024, so air is approximately 0
image[image == -2000] = 0
# Convert to Hounsfield units (HU)
intercept = scans[0].RescaleIntercept
slope = scans[0].RescaleSlope
if slope != 1:
image = slope * image.astype(np.float64)
image = image.astype(np.int16)
image += np.int16(intercept)
return np.array(image, dtype=np.int16)
id=0
patient = load_scan(data_path)
imgs = get_pixels_hu(patient)
AttributeError Traceback (most recent call last)
23 id=0
24 patient = load_scan(data_path)
---> 25 imgs = get_pixels_hu(patient)
10
11 # Convert to Hounsfield units (HU)
---> 12 intercept = scans[0].RescaleIntercept
13 slope = scans[0].RescaleSlope
14
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pydicom/dataset.py in __getattr__(self, name)
516 if tag not in self: # DICOM DataElement not in the Dataset
517 # Try the base class attribute getter (fix for issue 332)
--> 518 return super(Dataset, self).__getattribute__(name)
519 else:
520 return self[tag].value
AttributeError: 'FileDataset' object has no attribute 'RescaleIntercept'
Please run the following snippet and paste the output below.
Platform: Darwin-18.2.0-x86_64-i386-64bit
Sys: Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
Pydicom Version: pydicom 1.1.0
Thank you! :-)
Perhaps I'm missing something in the error trace, but is it possible that RescaleIntercept truly is not there in the file? Has it been confirmed using a dicom viewer, for example? In some cases, collections of files can contain one or more that are not images, for example.
Sorry I have not been clear on that, I am trying to fix and implement this code, which uses the outdated version 'dicom'. I am looking for the equivalent of the function in pydicom to convert the pixel values to Hounsfield Unit but couldn't find it in the documentation. I am not sure if I am missing something or if the function just hasn't been included in the new version. Is there a similar function in pydicom which I am missing or another way to implement it? :-)
Thank you!
Are you using a file from the code example you linked to? Can you link to it, or upload a copy here, if it has no patient or other identifiers in it? Or, better, please view the file in one of the free DICOM viewer programs, which can also show the DICOM data elements (often called 'header' information, or DICOM tags). Please check if RescaleIntercept actually exists in the file.
Note that RecaleSlope / RescaleIntercept are mandatory only for some kinds of images (mostly CT, PET), and the example code is for CT images (you probably know this anyway).
If you are not sure that all your images are of this kind, or if some CT images may lack the tags, you have to make sure that the tag is actually there, for example:
# Convert to Hounsfield units (HU)
intercept = scans[0].RescaleIntercept if 'RescaleIntercept' in scans[0] else -1024
slope = scans[0].RescaleSlope if 'RescaleSlope' in scans[0] else 1
(note that these defaults are for CT images)
@NBekele - could you resolve your issue?
Note that RecaleSlope / RescaleIntercept are mandatory only for some kinds of images (mostly CT, PET), and the example code is for CT images (you probably know this anyway).
If you are not sure that all your images are of this kind, or if some CT images may lack the tags, you have to make sure that the tag is actually there, for example:# Convert to Hounsfield units (HU) intercept = scans[0].RescaleIntercept if 'RescaleIntercept' in scans[0] else -1024 slope = scans[0].RescaleSlope if 'RescaleSlope' in scans[0] else 1(note that these defaults are for CT images)
This is not an exact solution. Giving -1024 as a constant value can't solve the problem. Instead of using a constant value, you can try something different
I faced a similar error a while ago
(AttributeError: 'FileDataset' object has no attribute 'InstanceNumber'
and read all related discussions for the reason and finally, I found out that there is at least one file in my dataset that doesn't match the DICOM format and I apparently can't see it. So you can easily use print(s for s in os.listdir(path)) to find the ones that are not of type DCM. File '.DS_Store' was the reason behind my error and I prohibited its addition to the "scans" list using an IF statement:
if( s != '.DS_Store'):
slices.append(ds)
Most helpful comment
Note that RecaleSlope / RescaleIntercept are mandatory only for some kinds of images (mostly CT, PET), and the example code is for CT images (you probably know this anyway).
If you are not sure that all your images are of this kind, or if some CT images may lack the tags, you have to make sure that the tag is actually there, for example:
(note that these defaults are for CT images)