map(float for Resampling DICOM images throwing Multivalue error
Asked Answered
B

3

5

I am getting "can only concatenate list (not "MultiValue") to list" highlighting map (float portion, while running below resampling, this code is very commonly used throughout image segmentation like lungs etc, I am thinking maybe this is issue with Python 3 and was working for earlier versions, any help is much appreciated:

id = 0
imgs_to_process = 
np.load(output_path+'fullimages_{}.npy'.format(id))
def resample(image, scan, new_spacing=[1,1,1]):
    # Determine current pixel spacing
    spacing = map(float, ([scan[0].SliceThickness] + scan[0].PixelSpacing))
    spacing = np.array(list(spacing))

    resize_factor = spacing / new_spacing
    new_real_shape = image.shape * resize_factor
    new_shape = np.round(new_real_shape)
    real_resize_factor = new_shape / image.shape
    new_spacing = spacing / real_resize_factor

    image = scipy.ndimage.interpolation.zoom(image, real_resize_factor)

    return image, new_spacing

print ("Shape before resampling\t", imgs_to_process.shape)
imgs_after_resamp, spacing = resample(imgs_to_process, patient, [1,1,1])
print ("Shape after resampling\t", imgs_after_resamp.shape)
Block answered 23/4, 2018 at 2:59 Comment(0)
S
9

Change

spacing = map(float, ([scan[0].SliceThickness] + scan[0].PixelSpacing))

To

spacing = map(float, ([scan[0].SliceThickness] + list(scan[0].PixelSpacing)))

Basically scan[0].PixelSpacing is a MultiValue and need to be converted into list before concatenation to another list.

Spadiceous answered 9/5, 2018 at 17:8 Comment(0)
M
0

this is the fix :

def resample(image, scan, new_spacing=[1,1,1]):
    spacing = np.array([float(scan[0].SliceThickness)] + [float(x) for x in scan[0].PixelSpacing], dtype=np.float32)
    resize_factor = spacing / new_spacing
    new_real_shape = image.shape * resize_factor
    new_shape = np.round(new_real_shape)
    real_resize_factor = new_shape / image.shape
    new_spacing = spacing / real_resize_factor
    image = scipy.ndimage.interpolation.zoom(image, real_resize_factor, mode='nearest')
    return image, new_spacing

pix_resampled, spacing = resample(first_patient_pixels, first_patient, [1,1,1])
print("Shape before resampling\t", first_patient_pixels.shape)
print("Shape after resampling\t", pix_resampled.shape)

basically, making scan[0].SliceThickness, as well as scan[0].PixelSpacing :

"list" of "float" values.

Messenia answered 18/9, 2020 at 19:26 Comment(0)
G
0

This is the solution:

def resample(image, scan, new_spacing=[1,1,1]):
    # Determine current pixel spacing
    spacing = [scan[0].SliceThickness]
    spacing.extend(scan[0].PixelSpacing)
    spacing = np.array(spacing, dtype=np.float32)
    
    resize_factor = spacing / new_spacing
    new_real_shape = image.shape * resize_factor
    new_shape = np.round(new_real_shape)
    real_resize_factor = new_shape / image.shape
    new_spacing = spacing / real_resize_factor
    
    image = scipy.ndimage.interpolation.zoom(image, real_resize_factor)
    
    return image, new_spacing
Gerek answered 28/1, 2022 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.