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)
| 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 | """ |
no test coverage detected