MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / scale_affine

Function scale_affine

monai/transforms/utils.py:2100–2132  ·  view source on GitHub ↗

Compute the scaling matrix according to the new spatial size Args: spatial_size: original spatial size. new_spatial_size: new spatial size. centered: whether the scaling is with respect to the image center (True, default) or corner (False). Ignored when

(spatial_size, new_spatial_size, centered: bool = True, align_corners: bool = False)

Source from the content-addressed store, hash-verified

2098
2099
2100def scale_affine(spatial_size, new_spatial_size, centered: bool = True, align_corners: bool = False):
2101 """
2102 Compute the scaling matrix according to the new spatial size
2103
2104 Args:
2105 spatial_size: original spatial size.
2106 new_spatial_size: new spatial size.
2107 centered: whether the scaling is with respect to the image center (True, default) or corner (False).
2108 Ignored when ``align_corners=True``, since corner-aligned scaling is inherently centered.
2109 align_corners: if True, use (size-1) based scaling to match torch.nn.functional.interpolate behavior.
2110
2111 Returns:
2112 the scaling matrix.
2113
2114 """
2115 r = max(len(new_spatial_size), len(spatial_size))
2116 if spatial_size == new_spatial_size:
2117 return np.eye(r + 1)
2118 if align_corners:
2119 # Match interpolate behavior: (src-1)/(dst-1); when dst == 1 the scale collapses to 0
2120 s = np.array(
2121 [0.0 if float(n) == 1 else (float(o) - 1) / (float(n) - 1) for o, n in zip(spatial_size, new_spatial_size)],
2122 dtype=float,
2123 )
2124 else:
2125 # Standard scaling: src/dst
2126 s = np.array([float(o) / float(max(n, 1)) for o, n in zip(spatial_size, new_spatial_size)], dtype=float)
2127 scale = create_scale(r, s.tolist())
2128 if centered and not align_corners:
2129 # For align_corners=False, add offset to center the scaling
2130 # For align_corners=True, the scaling is inherently centered (corners map to corners)
2131 scale[:r, -1] = (np.diag(scale)[:r] - 1) / 2.0 # type: ignore
2132 return scale
2133
2134
2135def attach_hook(func, hook, mode="pre"):

Callers 3

resizeFunction · 0.90
zoomFunction · 0.90
__call__Method · 0.90

Calls 3

maxFunction · 0.85
create_scaleFunction · 0.85
arrayMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…