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

Method _get_affine

monai/data/image_reader.py:331–352  ·  view source on GitHub ↗

Get or construct the affine matrix of the image, it can be used to correct spacing, orientation or execute spatial transforms. Args: img: an ITK image object loaded from an image file. lps_to_ras: whether to convert the affine matrix from "LPS" to "R

(self, img, lps_to_ras: bool = True)

Source from the content-addressed store, hash-verified

329 return meta_dict
330
331 def _get_affine(self, img, lps_to_ras: bool = True):
332 """
333 Get or construct the affine matrix of the image, it can be used to correct
334 spacing, orientation or execute spatial transforms.
335
336 Args:
337 img: an ITK image object loaded from an image file.
338 lps_to_ras: whether to convert the affine matrix from "LPS" to "RAS". Defaults to True.
339
340 """
341 direction = itk.array_from_matrix(img.GetDirection())
342 spacing = np.asarray(img.GetSpacing())
343 origin = np.asarray(img.GetOrigin())
344
345 direction = np.asarray(direction)
346 sr = min(max(direction.shape[0], 1), 3)
347 affine: np.ndarray = np.eye(sr + 1)
348 affine[:sr, :sr] = direction[:sr, :sr] @ np.diag(spacing[:sr])
349 affine[:sr, -1] = origin[:sr]
350 if lps_to_ras:
351 affine = orientation_ras_lps(affine)
352 return affine
353
354 def _get_spatial_shape(self, img):
355 """

Callers 1

get_dataMethod · 0.95

Calls 3

orientation_ras_lpsFunction · 0.90
minFunction · 0.85
maxFunction · 0.85

Tested by

no test coverage detected