Hello,
I'm just getting into image processing and using pydicom. I've got a list of DICOM images that I'm pulling in for analysis.
However, when I run the code below I get the error :
/******************************************************************************************************/
ERROR:
File "/anaconda3/lib/python3.6/site-packages/pydicom/dataset.py", line 480, in __getattr__
return super(Dataset, self).__getattribute__(name)
AttributeError: 'FileDataset' object has no attribute 'InstanceNumber'
/******************************************************************************************************/
Here's the code I'm running, which was borrowed from
/******************************************************************************************************/
CODE:
def load_scan(path):
slices = [dicom.read_file(path + '/' + s,force = True) for s in os.listdir(path)]
slices.sort(key = lambda x: int(x.InstanceNumber))
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
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(filepath)
)
imgs = get_pixels_hu(patient)
/******************************************************************************************************/
Anybody have an idea what's causing this or if there's a modification I can make in dataset.py to get around this? Is InstanceNumber unique to my dataset?
Thanks