Warp a list of points with the given homography. Arguments: points: list of N points, shape (N, 2(x, y))). homography: batched or not (shapes (B, 3, 3) and (...) respectively). Returns: a Tensor of shape (N, 2) or (B, N, 2(x, y)) (depending on whether the homography
(points, homographies, device='cpu')
| 239 | return homography |
| 240 | |
| 241 | def warp_points(points, homographies, device='cpu'): |
| 242 | """ |
| 243 | Warp a list of points with the given homography. |
| 244 | |
| 245 | Arguments: |
| 246 | points: list of N points, shape (N, 2(x, y))). |
| 247 | homography: batched or not (shapes (B, 3, 3) and (...) respectively). |
| 248 | |
| 249 | Returns: a Tensor of shape (N, 2) or (B, N, 2(x, y)) (depending on whether the homography |
| 250 | is batched) containing the new coordinates of the warped points. |
| 251 | |
| 252 | """ |
| 253 | # expand points len to (x, y, 1) |
| 254 | no_batches = len(homographies.shape) == 2 |
| 255 | homographies = homographies.unsqueeze(0) if no_batches else homographies |
| 256 | |
| 257 | batch_size = homographies.shape[0] |
| 258 | points = torch.cat((points.float(), torch.ones((points.shape[0], 1)).to(device)), dim=1) |
| 259 | points = points.to(device) |
| 260 | homographies = homographies.view(batch_size*3,3) |
| 261 | |
| 262 | warped_points = homographies@points.transpose(0,1) |
| 263 | |
| 264 | # normalize the points |
| 265 | warped_points = warped_points.view([batch_size, 3, -1]) |
| 266 | warped_points = warped_points.transpose(2, 1) |
| 267 | warped_points = warped_points[:, :, :2] / warped_points[:, :, 2:] |
| 268 | return warped_points[0,:,:] if no_batches else warped_points |
| 269 | |
| 270 | def inv_warp_image_batch(img, mat_homo_inv, device='cpu', mode='bilinear'): |
| 271 | ''' |
no outgoing calls
no test coverage detected