Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ragavsachdeva/CYWS-3D/llms.txt
Use this file to discover all available pages before exploring further.
geometry.py provides the mathematical building blocks that the rest of CYWS-3D relies on for spatial reasoning. The module is split into two groups of functions: coordinate utilities that project points between image space, camera space, and 3D world space; and bounding box utilities that filter, de-duplicate, and cross-match predicted change regions across the two views.
Coordinate utilities
get_index_grid()
Returns a dense normalised coordinate grid covering the full image, which is used internally to unproject every pixel to world space during feature warping.Height of the desired grid in pixels.
Width of the desired grid in pixels.
If provided, the grid is expanded and repeated along a leading batch dimension, producing shape
(B, H, W, 2). When None the output is (H, W, 2).If provided, the output tensor is cast to the same dtype and device as this tensor.
Tensor of shape (H, W, 2) or (B, H, W, 2) with values normalised to [0, 1] along each spatial axis.
convert_image_coordinates_to_world()
Unprojects 2D image coordinates to 3D world positions using per-pixel depth and camera matrices.Normalised pixel coordinates, shape
(B, N, 2), with values in [0, 1].Depth at each coordinate, shape
(B, N).Inverse intrinsic matrix, shape
(B, 3, 3).Camera-to-world rigid transform, shape
(B, 4, 4).3D world coordinates, shape
(B, N, 3).convert_world_to_image_coordinates()
Projects 3D world points into a camera’s image plane.3D world positions, shape
(B, N, 3).Inverse intrinsic matrix of the target camera, shape
(B, 3, 3).World-to-camera rigid transform, shape
(B, 4, 4).When
True, the projected depth is appended as a third channel, returning shape (B, N, 3). When False, only the 2D pixel coordinates are returned, shape (B, N, 2).transform_points()
Applies a homogeneous or affine transformation matrix to a set of points.Transform to apply, shape
(B, 4, 4) or (4, 4).Input points, shape
(B, N, 3).If
True, the transformed depth (z-coordinate) is retained in the output.Tensor, shape (B, N, 2) or (B, N, 3) when keep_depth=True.
get_relative_pose()
Computes the relative rotation and translation between two camera poses expressed in a shared world frame.Rotation matrix of the first camera, shape
(B, 3, 3).Rotation matrix of the second camera, shape
(B, 3, 3).World-space position of the first camera, shape
(B, 3).World-space position of the second camera, shape
(B, 3).Controls the return format (see below).
estimate_linear_warp()
Fits a least-squares linear (affine) warp between two sets of corresponding points. Used by the 2D registration path when no explicit transformation matrix is provided in the batch.Source points in homogeneous coordinates, shape
(B, N, 3).Target points in homogeneous coordinates, shape
(B, N, 3).Best-fit warp matrix
M of shape (B, 4, 4) such that applying M to X minimises the residual to Y in the least-squares sense:sample_depth_for_given_points()
Samples a dense depth map at a set of normalised 2D coordinates using bilinear interpolation.Dense depth map, shape
(B, H, W).Query coordinates normalised to
[0, 1], shape (B, N, 2).(B, N). Internally uses torch.nn.functional.grid_sample.
Bounding box utilities
bbox_iou_single_pair()
Computes the Intersection-over-Union (IoU) for a single pair of axis-aligned bounding boxes.First bounding box as
[x1, y1, x2, y2], shape (4,).Second bounding box as
[x1, y1, x2, y2], shape (4,).Tensor in [0, 1].
remove_bboxes_with_area_less_than()
Filters out degenerate or tiny bounding boxes by Shapely polygon area.Array of bounding boxes, shape
(N, 4), format [x1, y1, x2, y2].Minimum area threshold. Boxes whose Shapely polygon area is strictly less than this value are removed.
np.ndarray of shape (M, 4) where M ≤ N.
suppress_overlapping_bboxes()
Greedy non-maximum suppression (NMS): iteratively keeps the highest-scoring box and removes any remaining boxes that overlap it aboveiou_threshold.
Candidate bounding boxes, shape
(N, 4).Confidence score for each box, shape
(N,).IoU threshold above which a lower-scoring box is suppressed.
(kept_bboxes, kept_scores) in the same type (torch or numpy) as the inputs.
The default
iou_threshold of 0.2 is intentionally low. Because changed regions in the same scene are rarely identical duplicates, aggressive suppression is preferred over the more lenient thresholds common in object detection.keep_matching_bboxes()
Cross-view consistency filter: for each predicted bbox in the left image, projects its centre into the right image and keeps the pair only if the projected centre falls inside a predicted bbox on the right side (and vice versa).Must contain
batch["transform_points_1_to_2"] and batch["transform_points_2_to_1"] callables populated by FeatureRegisterationModule.forward().Index into the batch for which to perform matching.
Predicted bounding boxes for the left/first image, shape
(N, 4).Predicted bounding boxes for the right/second image, shape
(M, 4).Confidence scores for left predictions, shape
(N,).Confidence scores for right predictions, shape
(M,).Boxes with scores below this value are ignored before matching.
(left_bboxes, right_bboxes) as np.ndarray objects containing only the geometrically consistent pairs.
bboxes_to_masks()
Rasterises a batch of bounding box lists into binary spatial masks.A Python list of length
B, where each element is a Tensor of shape (N_i, 4) containing boxes for that sample.(height, width) of the output mask.Tensor of shape (B, 1, H, W) where pixels inside any box are 1 and all others are 0.
remove_invalid_bboxes()
Clamps boxes to lie within the image boundary and discards any that become degenerate after clamping.Bounding boxes, shape
(N, 4), format [x1, y1, x2, y2].The image side length used as the upper clamp bound (assumes a square image or that the same value applies to both axes).
If
True and all boxes are removed, a single dummy zero-area box is inserted so that downstream code never receives an empty tensor.Tensor of valid boxes.