torchio/utils.py
def nib_to_sitk(
        data: TypeData,
        affine: TypeData,
        squeeze: bool = False,
        force_3d: bool = False,
        force_4d: bool = False,
        ) -> sitk.Image:
    """Create a SimpleITK image from a tensor and a 4x4 affine matrix.

    Args:
        data: PyTorch tensor or NumPy array
        affine: # TODO
    """
    if data.ndim != 4:
        raise ValueError(f'Input must be 4D, but has shape {tuple(data.shape)}')
    # Possibilities
    # (1, w, h, 1)
    # (c, w, h, 1)
    # (1, w, h, 1)
    # (c, w, h, d)
    array = np.asarray(data)
    affine = np.asarray(affine).astype(np.float64)

    is_multichannel = array.shape[0] > 1 and not force_4d
    is_2d = array.shape[3] == 1 and not force_3d
    if is_2d:
        array = array[..., 0]
    if not is_multichannel and not force_4d:
        array = array[0]
    array = array.transpose()  # (W, H, D, C) or (W, H, D)
    image = sitk.GetImageFromArray(array, isVector=is_multichannel)

    rotation, spacing = get_rotation_and_spacing_from_affine(affine)
    origin = np.dot(FLIP_XY, affine[:3, 3])
    direction = np.dot(FLIP_XY, rotation)
    if is_2d:  # ignore first dimension if 2D (1, W, H, 1)
        direction = direction[:2, :2]
    image.SetOrigin(origin)  # should I add a 4th value if force_4d?
    image.SetSpacing(spacing)
    image.SetDirection(direction.flatten())
    if data.ndim == 4:
        assert image.GetNumberOfComponentsPerPixel() == data.shape[0]
    num_spatial_dims = 2 if is_2d else 3
    assert image.GetSize() == data.shape[1: 1 + num_spatial_dims]
    return image
Create a SimpleITK image from a tensor and a 4x4 affine matrix.

    Args:
        data: PyTorch tensor or NumPy array
        affine: # TODO
    
Create a SimpleITK image from a tensor and a 4x4 affine matrix.
Create a SimpleITK image from a tensor and a 4x4 affine matrix .
def nib_to_sitk(
        data: TypeData,
        affine: TypeData,
        squeeze: bool = False,
        force_3d: bool = False,
        force_4d: bool = False,
        ) -> sitk.Image:
    if data.ndim != 4:
        raise ValueError(f'Input must be 4D, but has shape {tuple(data.shape)}')
    array = np.asarray(data)
    affine = np.asarray(affine).astype(np.float64)
    is_multichannel = array.shape[0] > 1 and not force_4d
    is_2d = array.shape[3] == 1 and not force_3d
    if is_2d:
        array = array[..., 0]
    if not is_multichannel and not force_4d:
        array = array[0]
    array = array.transpose()  
    image = sitk.GetImageFromArray(array, isVector=is_multichannel)
    rotation, spacing = get_rotation_and_spacing_from_affine(affine)
    origin = np.dot(FLIP_XY, affine[:3, 3])
    direction = np.dot(FLIP_XY, rotation)
    if is_2d:  
        direction = direction[:2, :2]
    image.SetOrigin(origin)  
    image.SetSpacing(spacing)
    image.SetDirection(direction.flatten())
    if data.ndim == 4:
        assert image.GetNumberOfComponentsPerPixel() == data.shape[0]
    num_spatial_dims = 2 if is_2d else 3
    assert image.GetSize() == data.shape[1: 1 + num_spatial_dims]
    return image
def nib_to_sitk ( data : TypeData , affine : TypeData , squeeze : bool = False , force_3d : bool = False , force_4d : bool = False , ) -> sitk . Image : if data . ndim != 4 : raise ValueError ( f'Input must be 4D, but has shape {tuple(data.shape)}' ) array = np . asarray ( data ) affine = np . asarray ( affine ) . astype ( np . float64 ) is_multichannel = array . shape [ 0 ] > 1 and not force_4d is_2d = array . shape [ 3 ] == 1 and not force_3d if is_2d : array = array [ ... , 0 ] if not is_multichannel and not force_4d : array = array [ 0 ] array = array . transpose ( ) image = sitk . GetImageFromArray ( array , isVector = is_multichannel ) rotation , spacing = get_rotation_and_spacing_from_affine ( affine ) origin = np . dot ( FLIP_XY , affine [ : 3 , 3 ] ) direction = np . dot ( FLIP_XY , rotation ) if is_2d : direction = direction [ : 2 , : 2 ] image . SetOrigin ( origin ) image . SetSpacing ( spacing ) image . SetDirection ( direction . flatten ( ) ) if data . ndim == 4 : assert image . GetNumberOfComponentsPerPixel ( ) == data . shape [ 0 ] num_spatial_dims = 2 if is_2d else 3 assert image . GetSize ( ) == data . shape [ 1 : 1 + num_spatial_dims ] return image
Create a SimpleITK image from a tensor and a 4x4 affine matrix.
Create a SimpleITK image from a tensor and a 4x4 affine matrix .
"""Create a SimpleITK image from a tensor and a 4x4 affine matrix.

    Args:
        data: PyTorch tensor or NumPy array
        affine: # TODO
    """ # Possibilities # (1, w, h, 1) # (c, w, h, 1) # (1, w, h, 1) # (c, w, h, d) # (W, H, D, C) or (W, H, D) # ignore first dimension if 2D (1, W, H, 1) # should I add a 4th value if force_4d?
data
TypeData
PyTorch tensor or NumPy array
PyTorch tensor or NumPy array
affine
TypeData
None
squeeze
bool
force_3d
bool
force_4d
bool