Transform points between image coordinates and world coordinates. The input coordinates are assumed to be in the shape (C, N, 2 or 3), where C represents the number of channels and N denotes the number of points. It will return a tensor with the same shape as the input. Args:
| 1829 | |
| 1830 | |
| 1831 | class ApplyTransformToPoints(InvertibleTransform, Transform): |
| 1832 | """ |
| 1833 | Transform points between image coordinates and world coordinates. |
| 1834 | The input coordinates are assumed to be in the shape (C, N, 2 or 3), where C represents the number of channels |
| 1835 | and N denotes the number of points. It will return a tensor with the same shape as the input. |
| 1836 | |
| 1837 | Args: |
| 1838 | dtype: The desired data type for the output. |
| 1839 | affine: A 3x3 or 4x4 affine transformation matrix applied to points. This matrix typically originates |
| 1840 | from the image. For 2D points, a 3x3 matrix can be provided, avoiding the need to add an unnecessary |
| 1841 | Z dimension. While a 4x4 matrix is required for 3D transformations, it's important to note that when |
| 1842 | applying a 4x4 matrix to 2D points, the additional dimensions are handled accordingly. |
| 1843 | The matrix is always converted to float64 for computation, which can be computationally |
| 1844 | expensive when applied to a large number of points. |
| 1845 | If None, will try to use the affine matrix from the input data. |
| 1846 | invert_affine: Whether to invert the affine transformation matrix applied to the points. Defaults to ``True``. |
| 1847 | Typically, the affine matrix is derived from an image and represents its location in world space, |
| 1848 | while the points are in world coordinates. A value of ``True`` represents transforming these |
| 1849 | world space coordinates to the image's coordinate space, and ``False`` the inverse of this operation. |
| 1850 | affine_lps_to_ras: Defaults to ``False``. Set to `True` if your point data is in the RAS coordinate system |
| 1851 | or you're using `ITKReader` with `affine_lps_to_ras=True`. |
| 1852 | This ensures the correct application of the affine transformation between LPS (left-posterior-superior) |
| 1853 | and RAS (right-anterior-superior) coordinate systems. This argument ensures the points and the affine |
| 1854 | matrix are in the same coordinate system. |
| 1855 | |
| 1856 | Use Cases: |
| 1857 | - Transforming points between world space and image space, and vice versa. |
| 1858 | - Automatically handling inverse transformations between image space and world space. |
| 1859 | - If points have an existing affine transformation, the class computes and |
| 1860 | applies the required delta affine transformation. |
| 1861 | |
| 1862 | """ |
| 1863 | |
| 1864 | def __init__( |
| 1865 | self, |
| 1866 | dtype: DtypeLike | torch.dtype | None = None, |
| 1867 | affine: torch.Tensor | None = None, |
| 1868 | invert_affine: bool = True, |
| 1869 | affine_lps_to_ras: bool = False, |
| 1870 | ) -> None: |
| 1871 | self.dtype = dtype |
| 1872 | self.affine = affine |
| 1873 | self.invert_affine = invert_affine |
| 1874 | self.affine_lps_to_ras = affine_lps_to_ras |
| 1875 | |
| 1876 | def _compute_final_affine(self, affine: torch.Tensor, applied_affine: torch.Tensor | None = None) -> torch.Tensor: |
| 1877 | """ |
| 1878 | Compute the final affine transformation matrix to apply to the point data. |
| 1879 | |
| 1880 | Args: |
| 1881 | data: Input coordinates assumed to be in the shape (C, N, 2 or 3). |
| 1882 | affine: 3x3 or 4x4 affine transformation matrix. |
| 1883 | |
| 1884 | Returns: |
| 1885 | Final affine transformation matrix. |
| 1886 | """ |
| 1887 | |
| 1888 | affine = convert_data_type(affine, dtype=torch.float64)[0] |
no outgoing calls
searching dependent graphs…