(ax: Axes, keypoints: torch.Tensor, flow_cov: torch.Tensor | None, scale: float=3., colors=(0.2078431373, 0.6745098039, 0.6431372549, 0.5))
| 123 | @as_chain_func |
| 124 | @IgnoreException |
| 125 | def plot_flow_cov(ax: Axes, keypoints: torch.Tensor, flow_cov: torch.Tensor | None, scale: float=3., colors=(0.2078431373, 0.6745098039, 0.6431372549, 0.5)) -> Axes: |
| 126 | if flow_cov is None: return ax |
| 127 | |
| 128 | offsets = keypoints.detach().cpu() |
| 129 | |
| 130 | # Extract elements from the Nx3 covariance tensor |
| 131 | sigma_uu = flow_cov[..., 0] # Variance in u |
| 132 | sigma_vv = flow_cov[..., 1] # Variance in v |
| 133 | sigma_uv = flow_cov[..., 2] # Covariance term |
| 134 | |
| 135 | # Construct full covariance matrices in batch |
| 136 | cov_matrices = torch.empty(flow_cov.size(0), 2, 2, device=flow_cov.device) |
| 137 | cov_matrices[:, 0, 0] = sigma_uu |
| 138 | cov_matrices[:, 1, 1] = sigma_vv |
| 139 | cov_matrices[:, 0, 1] = sigma_uv |
| 140 | cov_matrices[:, 1, 0] = sigma_uv |
| 141 | |
| 142 | # Compute eigenvalues and eigenvectors for all covariance matrices |
| 143 | eigvals, eigvecs = torch.linalg.eigh(cov_matrices) # Batched eigen decomposition |
| 144 | |
| 145 | # Eigenvalues correspond to semi-axis lengths (variance, so take sqrt for stddev) |
| 146 | semi_axes = torch.sqrt(eigvals) # Shape: (N, 2) |
| 147 | major_axes = semi_axes[:, 1] # Larger eigenvalue -> major axis |
| 148 | minor_axes = semi_axes[:, 0] # Smaller eigenvalue -> minor axis |
| 149 | |
| 150 | # Compute angles from eigenvectors |
| 151 | angles = torch.atan2(eigvecs[:, 1, 1], eigvecs[:, 0, 1]) # Angle in radians |
| 152 | angles = torch.rad2deg(angles) # Convert to degrees |
| 153 | |
| 154 | # Prepare ellipse properties |
| 155 | widths = (major_axes * scale).detach().cpu().numpy() |
| 156 | heights = (minor_axes * scale).detach().cpu().numpy() |
| 157 | angles = angles.detach().cpu().numpy() |
| 158 | |
| 159 | # Set face colors |
| 160 | colors = colors |
| 161 | |
| 162 | # Create EllipseCollection |
| 163 | ellipses = EllipseCollection( |
| 164 | widths=widths, |
| 165 | heights=heights, |
| 166 | angles=angles, |
| 167 | units="xy", |
| 168 | offsets=offsets, |
| 169 | transOffset=ax.transData, |
| 170 | facecolors=colors, |
| 171 | edgecolors="none", |
| 172 | ) |
| 173 | |
| 174 | # Add ellipses to the plot |
| 175 | ax.add_collection(ellipses) |
| 176 | return ax |
| 177 | |
| 178 | @as_chain_func |
| 179 | @IgnoreException |
nothing calls this directly
no outgoing calls
no test coverage detected