Get normalized (range 0 to 1) coordinates and integer indices for an image.
(
shape: tuple[int, ...],
device: torch.device = torch.device("cpu"),
)
| 91 | |
| 92 | |
| 93 | def sample_image_grid( |
| 94 | shape: tuple[int, ...], |
| 95 | device: torch.device = torch.device("cpu"), |
| 96 | ) -> tuple[ |
| 97 | Float[Tensor, "*shape dim"], # float coordinates (xy indexing) |
| 98 | Int64[Tensor, "*shape dim"], # integer indices (ij indexing) |
| 99 | ]: |
| 100 | """Get normalized (range 0 to 1) coordinates and integer indices for an image.""" |
| 101 | |
| 102 | # Each entry is a pixel-wise integer coordinate. In the 2D case, each entry is a |
| 103 | # (row, col) coordinate. |
| 104 | indices = [torch.arange(length, device=device) for length in shape] |
| 105 | stacked_indices = torch.stack(torch.meshgrid(*indices, indexing="ij"), dim=-1) |
| 106 | |
| 107 | # Each entry is a floating-point coordinate in the range (0, 1). In the 2D case, |
| 108 | # each entry is an (x, y) coordinate. |
| 109 | coordinates = [(idx + 0.5) / length for idx, length in zip(indices, shape)] |
| 110 | coordinates = reversed(coordinates) |
| 111 | coordinates = torch.stack(torch.meshgrid(*coordinates, indexing="xy"), dim=-1) |
| 112 | |
| 113 | return coordinates, stacked_indices |
| 114 | |
| 115 | |
| 116 | def reproject_points( |
no outgoing calls
no test coverage detected