poses_to_bboxes#
- movement.transforms.poses_to_bboxes(position, padding=0.0)[source]#
Compute bounding box centroid and shape from a poses position array.
This function computes bounding boxes from pose estimation keypoints by finding the minimum and maximum coordinates across all keypoints for each individual at each time point. The resulting bounding box is represented by its centroid (center point) and shape (width and height).
- Parameters:
position (
DataArray) – A 2D poses position array with dimensions(time, space, keypoints, individuals), where thespacecoordinate contains exactly["x", "y"].padding (
float) – Number of pixels to add as padding around the bounding box in all directions. The padding increases both width and height by2 * padding. Default is 0.0 (no padding).
- Returns:
A tuple
(position, shape)where:position: bounding box centroids with dimensions(time, space, individuals).shape: bounding box width and height with dimensions(time, space, individuals).
- Return type:
- Raises:
TypeError – If
positionis not anxarray.DataArrayor ifpaddingis not numeric.ValueError – If the position array is missing required dimensions or coordinates, is not 2D, or
paddingis negative.
Notes
Keypoints with NaN in any spatial coordinate are excluded from bounding box calculation. If all keypoints for an individual at a given time are NaN, the resulting centroid and shape are NaN.
The centroid is calculated as the midpoint of the bounding box:
(min + max) / 2for each spatial dimension.The shape is calculated as the span of coordinates plus padding:
width = max_x - min_x + 2*paddingandheight = max_y - min_y + 2*padding.When there is only one valid keypoint, the bounding box will have zero width and/or height (before padding is applied).
Examples
Compute bounding boxes from a poses dataset with zero padding:
>>> from movement.transforms import poses_to_bboxes >>> bbox_position, bbox_shape = poses_to_bboxes(poses_ds["position"])
Compute bounding boxes from a poses dataset with 10 pixels of padding:
>>> bbox_position, bbox_shape = poses_to_bboxes( ... poses_ds["position"], padding=10 ... )
See also
movement.transforms.scaleScale spatial coordinates