kornia.geometry.boxes#
Object-oriented API for 2D and 3D bounding boxes: the Boxes and
Boxes3D classes wrap a tensor of boxes and provide format conversions
(xyxy, xywh, vertices), transformations and padding.
- class kornia.geometry.boxes.Boxes(boxes, raise_if_not_floating_point=True, mode='vertices_plus')[source]#
2D boxes containing N or BxN boxes.
- Parameters:
boxes (
Tensor|list[Tensor]) – 2D boxes, shape of \((N, 4, 2)\), \((B, N, 4, 2)\) or a list of \((N, 4, 2)\). See below for more details.raise_if_not_floating_point (
bool, optional) – flag to control floating point casting behaviour when boxes is not a floating point tensor. True to raise an error when boxes isn’t a floating point tensor, False to cast to float. Default:Truemode (
str, optional) – Representation label reused as the default output mode byto_tensor(). The constructor does not convertboxes; usefrom_tensor()to import another representation. Default:"vertices_plus"
- Convention:
A box is a quadrilateral of four floating-point
(x, y)vertices, \((N, 4, 2)\) or \((B, N, 4, 2)\), built in clockwise top-left, top-right, bottom-right, bottom-left order;transform_boxes()can make it an arbitrary (rotated) quadrilateral.The stored form is inclusive (
'vertices_plus'):width = xmax - xmin + 1. The exclusive'xyxy','xywh'and'vertices'modes are converted byfrom_tensor()andto_tensor(); the constructor converts nothing and only recordsmode.to_tensor()reduces the vertices withamin/amax, so every export is an axis-aligned bounding box andto_tensor('vertices_plus')is not the identity for a rotated box.get_boxes_shape()returns(heights, widths); padding entries of a list-backed object report as 1-by-1 boxes (the inclusive+1, #3934).infer_bbox_shape()andbbox_to_mask()read their input as inclusive: pass them the'vertices_plus'export, unbatched.nms()takes exclusivexyxy.With
validate_boxes=True, every mode rejects a non-finite coordinate. The'xy*'modes raiseValueErrorand also reject non-positive extents. The vertex modes raiseRuntimeErrorthroughtorch._assert_async(on CPU and CUDA, skipped on MPS), so they stayfullgraph-compilable, and take no extent or shape check, so the arbitrary quadrilaterals oftransform_boxes()import unchanged.The constructor rejects an integer tensor unless
raise_if_not_floating_point=False(a list is checked by its first element’s dtype);from_tensor()casts integer input to the default dtype.merge()andindex_put()are non-mutating by default.
Warning
The inclusive
+1differs from torchvision, COCO and albumentations: #3934. A sub-unit extent (normalized[0, 1]boxes) is corrupted by the- 1of the converting modes: #4061. Passing the'vertices'export to the bbox helpers reads one pixel larger: #4009.compute_area()is the shoelace area of the stored inclusive vertices,(width - 1) * (height - 1): #4010.nms()is exclusive: #4008.to_mask()andbbox_to_mask()take opposite size orders: #4014. The integer-input split is #4012.'vertices'subtracts the inclusive offset at fixed vertex slots, so it deforms a quadrilateral that is not an axis-aligned rectangle in clockwise order: #3934.trim,translate(method='fast')and tuple-boundclampare unimplemented: #4017.- clamp(topleft=None, botright=None, inplace=False)[source]#
Clamp every box vertex inside per-image coordinate limits.
See the Convention block on
Boxes.- Convention:
Bounds are tensors with one
(x, y)pair per batch element, or a single row that broadcasts (an unbatched container needs exactly one row). Every vertex is clamped independently, so a box wholly outside the bounds collapses onto the boundary instead of being removed.
- Parameters:
topleft (
Union[Tensor,tuple[int,int],None], optional) – Tensor of shape \((B, 2)\) containing the minimumxandycoordinate allowed for each batch item. A single row broadcasts across the batch. Default:Nonebotright (
Union[Tensor,tuple[int,int],None], optional) – Tensor of shape \((B, 2)\) containing the maximumxandycoordinate allowed for each batch item. A single row broadcasts across the batch. Default:Noneinplace (
bool, optional) – IfTrue, clamp this object in place. Otherwise, return a newBoxesobject with clamped data. Default:False
- Return type:
- Returns:
Boxeswhose vertex coordinates are restricted to the provided bounds.
- compute_area()[source]#
Compute polygon area with the shoelace formula.
See the Convention block on
Boxes.- Convention:
Vertices are sorted by angle about their centroid, so a convex quadrilateral is assumed.
Warning
On the stored inclusive vertices an axis-aligned box gives
(width - 1) * (height - 1), not thewidth * heightofget_boxes_shape(): #4010.- Return type:
- Returns:
Area for each box, shaped \((N,)\) or \((B, N)\).
- property data: Tensor#
Return the raw quadrilateral coordinate tensor.
- Returns:
Tensor storing four vertices per box in
(x, y)order. The common shapes are \((N, 4, 2)\) for unbatched boxes and \((B, N, 4, 2)\) for batched boxes, where \(B\) is batch size and \(N\) is the number of boxes.
- filter_boxes_by_area(min_area=None, max_area=None, inplace=False)[source]#
Zero boxes whose polygon area is outside the requested range.
See the Convention block on
Boxes.The box area is computed from its four vertices. Boxes smaller than
min_areaor larger thanmax_areaare not dropped from the tensor; their coordinates are replaced with zeros so the original batch and box dimensions stay unchanged. Seecompute_area()for the area convention used by the thresholds. Filtered boxes export as exact zeros in every mode and draw no mask pixels, including after subsequent coordinate transforms.- Parameters:
min_area (
Optional[float], optional) – Optional lower inclusive area threshold. Boxes with area below this value are zeroed. Default:Nonemax_area (
Optional[float], optional) – Optional upper inclusive area threshold. Boxes with area above this value are zeroed. Default:Noneinplace (
bool, optional) – IfTrue, update this object in place. Otherwise, return a filtered clone. Default:False
- Return type:
- Returns:
Boxeswith the same shape as the input container and out-of-range boxes replaced by zero coordinates.
- classmethod from_tensor(boxes, mode='xyxy', validate_boxes=True)[source]#
Create
Boxesfrom boxes stored in another format.See the Convention block on
Boxes.- Parameters:
boxes (
Tensor|list) – 2D boxes, shape of \((N, 4)\), \((B, N, 4)\), \((N, 4, 2)\) or \((B, N, 4, 2)\), a nested numeric list with one of those shapes, or a list of \((N, 4)\) or \((N, 4, 2)\) tensors matchingmode.mode (
str, optional) –The format in which the boxes are provided: Default:
"xyxy"’xyxy’:
xmin, ymin, xmax, ymaxwith exclusive extent. With shape \((N, 4)\), \((B, N, 4)\).’xyxy_plus’:
xmin, ymin, xmax, ymaxwith inclusive extent. With shape \((N, 4)\), \((B, N, 4)\).’xywh’:
xmin, ymin, width, heightwith exclusive extent. With shape \((N, 4)\), \((B, N, 4)\).’vertices’: boxes are defined by their vertices points in the following
clockwiseorder: top-left, top-right, bottom-right, bottom-left. Vertices coordinates are in (x,y) order. This is the exclusive input form. With shape \((N, 4, 2)\), \((B, N, 4, 2)\).’vertices_plus’: the inclusive stored vertex form. With shape \((N, 4, 2)\), \((B, N, 4, 2)\).
validate_boxes (
bool, optional) – Reject a non-finite coordinate in every mode:ValueErrorfor the'xy*'modes, which also reject non-positive extents in each mode’s convention, andRuntimeErrorfromtorch._assert_asyncfor the vertex modes (skipped on MPS), which take no extent or shape check. Default:True
- Return type:
- Returns:
Boxescontaining the converted inclusive vertex representation.
Examples
>>> boxes_xyxy = torch.as_tensor([[0, 3, 1, 4], [5, 1, 8, 4]]) >>> boxes = Boxes.from_tensor(boxes_xyxy, mode='xyxy') >>> boxes.data # (2, 4, 2) tensor([[[0., 3.], [0., 3.], [0., 3.], [0., 3.]], [[5., 1.], [7., 1.], [7., 3.], [5., 3.]]])
- get_boxes_shape()[source]#
Compute boxes heights and widths.
See the Convention block on
Boxes.- Return type:
- Returns:
Boxes heights, shape of \((N,)\) or \((B,N)\).
Boxes widths, shape of \((N,)\) or \((B,N)\).
Example
>>> boxes_xyxy = torch.tensor([[[1,1,2,2],[1,1,3,2]]]) >>> boxes = Boxes.from_tensor(boxes_xyxy) >>> boxes.get_boxes_shape() (tensor([[1., 1.]]), tensor([[1., 2.]]))
- index_put(indices, values, inplace=False)[source]#
Write box coordinates at selected tensor indices.
See the Convention block on
Boxes.This mirrors
torch.Tensor.index_put_()for the internal quadrilateral tensor. It is useful when a subset of boxes in a batch must be replaced while keeping theBoxeswrapper and metadata.- Parameters:
indices (
tuple[Tensor,...] |list[Tensor]) – Index tuple or list accepted byTensor.index_put_. The indices address entries in the stored tensor, commonly shaped \((B, N, 4, 2)\) or \((N, 4, 2)\).values (
Tensor|Boxes) – Replacement coordinates. If aBoxesobject is passed, its coordinates and filtering validity are copied. Raw tensor writes change only coordinates and preserve the existing filtering validity.inplace (
bool, optional) – IfTrue, update this object and returnself. IfFalse, clone the current data first and return a newBoxesinstance. Default:False
- Return type:
- Returns:
Boxescontaining the updated coordinates.
- merge(boxes, inplace=False)[source]#
Merge boxes.
See the Convention block on
Boxes.For batched boxes, if the current instance holds \((B, N, 4, 2)\) and the incoming boxes holds \((B, M, 4, 2)\), the merge results in \((B, N + M, 4, 2)\).
For unbatched boxes, if the current instance holds \((N, 4, 2)\) and the incoming boxes holds \((M, 4, 2)\), the merge results in \((N + M, 4, 2)\).
- Parameters:
- Return type:
Note
When either input was created from a list, each batch row is repacked so that its real boxes precede all trailing padding. The merged object keeps the combined per-image padding counts in
_N.
- property mode: str#
Return the box format remembered by this container.
- Returns:
Mode string used as the default by
to_tensor(), such as"xyxy","xywh","vertices", or their"_plus"variants.
- pad(padding_size)[source]#
Pad every box in place.
See the Convention block on
Boxes.padding_sizeis ordered as(left, right, top, bottom). Onlyleftandtopchange the coordinate origin; this method returnsselfafter adding those two values to every vertex. Both the batched \((B, N, 4, 2)\) and the unbatched \((N, 4, 2)\) container are supported; the unbatched form carries a single image, sopadding_sizemust have exactly one row.
- property shape: tuple[int, ...] | Size#
Return the tensor shape used to store the boxes.
- Returns:
Shape of
data. For unbatched boxes this is usually \((N, 4, 2)\), where \(N\) is the number of boxes,4is the number of corner vertices, and2stores(x, y). For batched boxes the shape is usually \((B, N, 4, 2)\), where \(B\) is the batch size.
- to_mask(height, width)[source]#
Convert 2D boxes to masks. Covered area is 1 and the remaining is 0.
See the Convention block on
Boxes.- Convention:
The size is
(height, width)and the mask \((N, height, width)\) or \((B, N, height, width)\) in the box dtype. The exclusive'xyxy'export is clamped to the image, rounded to the nearest integer and filled over[xmin, xmax),[ymin, ymax). Padding entries of a list-backed object give empty masks. A box tensor that requires grad raisesRuntimeError.
Warning
bbox_to_mask()takes(width, height)(#4014) and does not round, so a fractional box can fill a different area (#4015).- Parameters:
- Return type:
- Returns:
the output mask tensor, shape of \((N, height, width)\) or \((B, N, height, width)\) and dtype of
Boxes.dtype()(it can be any floating point dtype).
Note
It is currently non-differentiable.
Examples
>>> boxes = Boxes(torch.tensor([[ # Equivalent to Boxes.from_tensor([[1, 1, 4, 3]], mode='xyxy_plus') ... [1., 1.], ... [4., 1.], ... [4., 3.], ... [1., 3.], ... ]])) # 1x4x2 >>> boxes.to_mask(5, 5) tensor([[[0., 0., 0., 0., 0.], [0., 1., 1., 1., 1.], [0., 1., 1., 1., 1.], [0., 1., 1., 1., 1.], [0., 0., 0., 0., 0.]]])
- to_tensor(mode=None, as_padded_sequence=False)[source]#
Cast
Boxesto a tensor.See the Convention block on
Boxes.modeselects the output representation.'xyxy','xywh', and'vertices'are exclusive exports; the'_plus'variants are inclusive. Every mode exports the axis-aligned bounds of the stored vertices, reduced withamin/amax, so the export is lossy for rotated boxes:to_tensor('vertices_plus')does not returndataunchanged after atransform_boxes()call that rotates, shears, or otherwise reorders the vertices. A quarter turn keeps the box axis-aligned and still yields an export that differs fromdata, because the reduction re-canonicalizes the vertex order.- Parameters:
mode (
Optional[str], optional) –the output box format, or
Noneto reusemode. That attribute depends on the construction path: the constructor defaults to'vertices_plus', whilefrom_tensor()records the mode its input was given in. It could be: Default:None’xyxy’:
xmin, ymin, xmax, ymaxwith exclusive extent.’xyxy_plus’:
xmin, ymin, xmax, ymaxwith inclusive extent.’xywh’:
xmin, ymin, width, heightwith exclusive extent.’vertices’: boxes are defined by their vertices points in the following
clockwiseorder: top-left, top-right, bottom-right, bottom-left. Vertices coordinates are in (x,y) order. This is the exclusive export form.’vertices_plus’: the inclusive stored vertex form.
as_padded_sequence (
bool, optional) – If this object was created from a list, return its padded tensor rather than a list of tensors trimmed to their original lengths. The padded values follow the selected output mode. Batch slicing, index tensors, and boolean masks preserve the list metadata. Selecting a single batch element with an integer or scalar tensor returns an unbatched object without list metadata. Default:False
- Return type:
- Returns:
Boxes tensor in the
modeformat, or a list of tensors when the object was created from a list andas_padded_sequence=False. The tensor shape depends on themodevalue:’vertices’ or ‘vertices_plus’: \((N, 4, 2)\) or \((B, N, 4, 2)\).
Any other value: \((N, 4)\) or \((B, N, 4)\).
Examples
>>> boxes_xyxy = torch.as_tensor([[0, 3, 1, 4], [5, 1, 8, 4]]) >>> boxes = Boxes.from_tensor(boxes_xyxy) >>> assert (boxes_xyxy == boxes.to_tensor(mode='xyxy')).all()
- transform_boxes(M, inplace=False)[source]#
Apply a transformation matrix to the 2D boxes.
See the Convention block on
Boxes.
- transform_boxes_(M)[source]#
Apply
transform_boxes()in place and returnself.See the Convention block on
Boxes.- Convention:
The internal tensor is rebound, not written: a tensor obtained from
databefore the call keeps the old coordinates (an empty container keeps its tensor).
- translate(size, method='warp', inplace=False)[source]#
Translate boxes by the provided size.
See the Convention block on
Boxes.sizesupplies one(x, y)translation per batch item. Only the"warp"method is implemented;"fast"raisesNotImplementedError.
- trim(correspondence_preserve=False, inplace=False)[source]#
Raise because trimming padded boxes is not implemented.
See the Convention block on
Boxes.- Parameters:
- Raises:
NotImplementedError – Always.
- Return type:
- unpad(padding_size)[source]#
Undo
pad()in place.See the Convention block on
Boxes.padding_sizeis ordered as(left, right, top, bottom). Onlyleftandtopchange the coordinate origin; this method returnsselfafter subtracting those two values from every vertex. Both the batched \((B, N, 4, 2)\) and the unbatched \((N, 4, 2)\) container are supported; the unbatched form carries a single image, sopadding_sizemust have exactly one row.
- class kornia.geometry.boxes.Boxes3D(boxes, raise_if_not_floating_point=True, mode='xyzxyz_plus')[source]#
3D boxes containing N or BxN boxes.
- Parameters:
boxes (
Tensor) – 3D boxes, shape of \((N,8,3)\) or \((B,N,8,3)\). See below for more details.raise_if_not_floating_point (
bool, optional) – flag to control floating point casting behaviour when boxes is not a floating point tensor. True to raise an error when boxes isn’t a floating point tensor, False to cast to float. Default:Truemode (
str, optional) – Representation label reported bymode. The constructor does not convertboxesandto_tensor()does not consult the label; usefrom_tensor()to import another representation. Default:"xyzxyz_plus"
- Convention:
A box is a hexahedron of eight floating-point
(x, y, z)vertices, \((N, 8, 3)\) or \((B, N, 8, 3)\): front-top-left, front-top-right, front-bottom-right, front-bottom-left, then the same four back vertices.The stored form is inclusive (
depth = zmax - zmin + 1), as forBoxes.from_tensor()takes \((N, 6)\) or \((B, N, 6)\) in'xyzxyz','xyzwhd'(exclusive) or'xyzxyz_plus'; the vertex modes exist forto_tensor()only. Modes are case-insensitive.to_tensor()exports theamin/amaxaxis-aligned bounding box and defaults to'xyzxyz'whatever the stored label.get_boxes_shape()returns(depths, heights, widths), inclusive.infer_bbox_shape3d()andbbox_to_mask3d()read their input as inclusive: pass them the'vertices_plus'export, unbatched.With
validate_boxes=True,from_tensor()rejects a non-finite coordinate and extents that are not positive in the given mode (xmax == xminfails in'xyzxyz', passes in'xyzxyz_plus').The constructor rejects an integer tensor unless
raise_if_not_floating_point=False;from_tensor()casts integer input to the default dtype.transform_boxes()returns a new object labelled'xyzxyz_plus';transform_boxes_()rebinds the internal tensor ofselfand keeps the label.
Warning
The inclusive
+1is #3934 and the'vertices'export trap #4009. The integer-input split is #4012 and the validator split withvalidate_bbox()is #4013. Boxes frombbox_generator3d()measure one larger than requested: #4018. Theto_tensor()default differs fromBoxes.to_tensor(), which defaults to the stored mode: #4251.- property data: Tensor#
Return the raw 3D corner-coordinate tensor.
- Returns:
Tensor containing eight 3D corner coordinates per box, usually shaped \((N, 8, 3)\) or \((B, N, 8, 3)\).
- classmethod from_tensor(boxes, mode='xyzxyz', validate_boxes=True)[source]#
Create
Boxes3Dfrom 3D boxes stored in another format.See the Convention block on
Boxes3D.- Parameters:
boxes (
Tensor) – 3D boxes, shape of \((N,6)\) or \((B,N,6)\). Integer input is cast totorch.get_default_dtype().mode (
str, optional) –The format in which the 3D boxes are provided, matched case-insensitively. Default:
"xyzxyz"’xyzxyz’: boxes are assumed to be in the format
xmin, ymin, zmin, xmax, ymax, zmaxwherewidth = xmax - xmin,height = ymax - yminanddepth = zmax - zmin.’xyzxyz_plus’: similar to ‘xyzxyz’ mode but where box width, length and depth are defined as
width = xmax - xmin + 1,height = ymax - ymin + 1anddepth = zmax - zmin + 1.’xyzwhd’: boxes are assumed to be in the format
xmin, ymin, zmin, width, height, depthwherewidth = xmax - xmin,height = ymax - yminanddepth = zmax - zmin.
validate_boxes (
bool, optional) – reject boxes with a non-finite coordinate, and boxes whose width, height or depth is not positive when measured in the given mode’s convention, soxmax == xminis rejected in'xyzxyz'and accepted in'xyzxyz_plus'. Default:True
- Return type:
- Returns:
Boxes3Dcontaining the converted inclusive vertex representation, labelled withmode.
Examples
>>> boxes_xyzxyz = torch.as_tensor([[0, 3, 6, 1, 4, 8], [5, 1, 3, 8, 4, 9]]) >>> boxes = Boxes3D.from_tensor(boxes_xyzxyz, mode='xyzxyz') >>> boxes.data # (2, 8, 3) tensor([[[0., 3., 6.], [0., 3., 6.], [0., 3., 6.], [0., 3., 6.], [0., 3., 7.], [0., 3., 7.], [0., 3., 7.], [0., 3., 7.]], [[5., 1., 3.], [7., 1., 3.], [7., 3., 3.], [5., 3., 3.], [5., 1., 8.], [7., 1., 8.], [7., 3., 8.], [5., 3., 8.]]])
- get_boxes_shape()[source]#
Compute boxes depths, heights and widths.
See the Convention block on
Boxes3D.- Return type:
- Returns:
Boxes depths, shape of \((N,)\) or \((B,N)\).
Boxes heights, shape of \((N,)\) or \((B,N)\).
Boxes widths, shape of \((N,)\) or \((B,N)\).
Example
>>> boxes_xyzxyz = torch.tensor([[ 0, 1, 2, 10, 21, 32], [3, 4, 5, 43, 54, 65]]) >>> boxes3d = Boxes3D.from_tensor(boxes_xyzxyz) >>> boxes3d.get_boxes_shape() (tensor([30., 60.]), tensor([20., 50.]), tensor([10., 40.]))
- property mode: str#
Return the 3D box format remembered by this container.
- Returns:
Mode string describing how this container should be interpreted during tensor conversion.
- property shape: tuple[int, ...] | Size#
Return the tensor shape used to store 3D boxes.
- Returns:
Shape of
data. For unbatched boxes this is usually \((N, 8, 3)\), where \(N\) is the number of boxes,8is the number of cuboid corners, and3stores(x, y, z). For batched boxes the shape is usually \((B, N, 8, 3)\).
- to_mask(depth, height, width)[source]#
Convert 3D boxes to masks. Covered area is 1 and the remaining is 0.
See the Convention block on
Boxes3D.- Convention:
The size is
(depth, height, width)and the mask \((N, depth, height, width)\) or \((B, N, depth, height, width)\) in the box dtype, with no channel axis. The exclusive'xyzxyz'export is clamped to the volume, rounded to the nearest integer and filled over half-open ranges. A box tensor that requires grad raisesRuntimeError.
Warning
bbox_to_mask3d()truncates instead of rounding, so a fractional box can fill a different volume: #4015.- Parameters:
- Return type:
- Returns:
the output mask tensor, shape of \((N, depth, height, width)\) or \((B, N, depth, height, width)\) and dtype of
Boxes3D.dtype()(it can be any floating point dtype).
Note
It is currently non-differentiable.
Examples
>>> boxes = Boxes3D(torch.tensor([[ # Same as Boxes3D.from_tensor([[1, 1, 1, 3, 3, 2]], 'xyzxyz_plus') ... [1., 1., 1.], ... [3., 1., 1.], ... [3., 3., 1.], ... [1., 3., 1.], ... [1., 1., 2.], ... [3., 1., 2.], ... [3., 3., 2.], ... [1., 3., 2.], ... ]])) # 1x8x3 >>> boxes.to_mask(4, 5, 5) tensor([[[[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 1., 1., 1., 0.], [0., 1., 1., 1., 0.], [0., 1., 1., 1., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 1., 1., 1., 0.], [0., 1., 1., 1., 0.], [0., 1., 1., 1., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]]])
- to_tensor(mode='xyzxyz')[source]#
Cast
Boxes3Dto a tensor.See the Convention block on
Boxes3D.- Convention:
modedefaults to'xyzxyz'regardless of the stored label (#4251). Every export is theamin/amaxaxis-aligned bounding box of the stored vertices.
- Parameters:
mode (
str, optional) –The format in which the boxes are provided, matched case-insensitively. Default:
"xyzxyz"’xyzxyz’: boxes are assumed to be in the format
xmin, ymin, zmin, xmax, ymax, zmaxwherewidth = xmax - xmin,height = ymax - yminanddepth = zmax - zmin.- ’xyzxyz_plus’: similar to ‘xyzxyz’ mode but where box width, length and depth are defined as
width = xmax - xmin + 1,height = ymax - ymin + 1anddepth = zmax - zmin + 1.
’xyzwhd’: boxes are assumed to be in the format
xmin, ymin, zmin, width, height, depthwherewidth = xmax - xmin,height = ymax - yminanddepth = zmax - zmin.’vertices’: boxes are defined by their vertices points in the following
clockwiseorder: front-top-left, front-top-right, front-bottom-right, front-bottom-left, back-top-left, back-top-right, back-bottom-right, back-bottom-left. Vertices coordinates are in (x,y, z) order. Finally, box width, height and depth are defined aswidth = xmax - xmin,height = ymax - yminanddepth = zmax - zmin.’vertices_plus’: similar to ‘vertices’ mode but where box width, height and depth are defined as
width = xmax - xmin + 1,height = ymax - ymin + 1anddepth = zmax - zmin + 1; this is the stored form.
- Returns:
‘vertices’ or ‘vertices_plus’: \((N, 8, 3)\) or \((B, N, 8, 3)\).
Any other value: \((N, 6)\) or \((B, N, 6)\).
- Return type:
3D Boxes tensor in the
modeformat. The shape depends with themodevalue
Note
The
amin/amaxreduction ties on every face of an axis-aligned box (4 vertices share each extremum), so the gradient there is a subgradient shared among the tied vertices; it sums to one over them but need not match a finite-difference estimate such astorch.autograd.gradcheck().Examples
>>> boxes_xyzxyz = torch.as_tensor([[0, 3, 6, 1, 4, 8], [5, 1, 3, 8, 4, 9]]) >>> boxes = Boxes3D.from_tensor(boxes_xyzxyz, mode='xyzxyz') >>> assert (boxes.to_tensor(mode='xyzxyz') == boxes_xyzxyz).all()
- transform_boxes(M, inplace=False)[source]#
Apply a transformation matrix to the 3D boxes.
See the Convention block on
Boxes3D.- Parameters:
- Returns:
a new
Boxes3Dlabelled'xyzxyz_plus'wheninplaceis false, otherwiseself.- Return type:
The transformed boxes
- transform_boxes_(M)[source]#
Apply
transform_boxes()in place and returnself.See the Convention block on
Boxes3D.- Convention:
The internal tensor is rebound, not written: a tensor obtained from
databefore the call keeps the old coordinates. The stored mode label is kept.
- class kornia.geometry.boxes.VideoBoxes(boxes, raise_if_not_floating_point=True, mode='vertices_plus')[source]#
2D boxes with an explicit temporal channel for video sequences.
Accepts and returns box corners for a batch of videos as \((B, T, N, 4, 2)\) in
vertices_plusmode. Internally the corners are stored flattened as \((B \cdot T, N, 4, 2)\).AugmentationSequentialuses this wrapper when the pipeline contains a video sequential so thatto_tensorrestores the temporal axis after geometric transforms.See the Convention block on
Boxes.- Convention:
from_tensor()stores \((B, T, N, 4, 2)\) input unchanged as \((B \cdot T, N, 4, 2)\)'vertices_plus'data, with no mode or conversion; integer input is cast to the default dtype, and another shape or a list raisesValueError.validate_boxes=Truerejects a non-finite coordinate asBoxes.from_tensor()does for'vertices_plus'.to_tensor()accepts everyBoxesmode and restores the temporal axis (to_tensor('xyxy')is \((B, T, N, 4)\)).Indexing selects the video batch axis while preserving the temporal axis. An integer index returns a one-video
VideoBoxessoto_tensor()keeps the shape \((1, T, \ldots)\). Index assignment,video_boxes[key] = other, writes the same whole videos.A transformation matrix is \((B \cdot T, 3, 3)\); a \((3, 3)\) matrix raises
ValueErrorunless \(B \cdot T = 1\).transform_boxes(),translate(),clamp(),filter_boxes_by_area()andmerge()return a newVideoBoxes;pad(),unpad(),to()andtype()updateselfin place.
- Variables:
temporal_channel_size – Number of frames \(T\) stored with the boxes.
- clone()[source]#
Create an independent copy of the video box container.
- Return type:
- Returns:
New
VideoBoxeswith cloned tensor storage and the sametemporal_channel_size, mode, and batch metadata.
- classmethod from_tensor(boxes, validate_boxes=True)[source]#
Create
VideoBoxesfrom a video box tensor.- Parameters:
boxes (
Tensor|list[Tensor]) – Box corners with shape \((B, T, N, 4, 2)\) invertices_plusorder (top-left, top-right, bottom-right, bottom-left), stored unchanged; integer input is cast totorch.get_default_dtype(). Lists of tensors are not supported yet.validate_boxes (
bool, optional) – Reject a non-finite coordinate withRuntimeError(torch._assert_async, skipped on MPS). There is no extent or shape check, as for'vertices_plus'infrom_tensor(). Default:True
- Return type:
- Returns:
VideoBoxeswithtemporal_channel_sizeset toboxes.size(1).- Raises:
ValueError – If
boxesis a list or does not have shape \((B, T, N, 4, 2)\).
- to_tensor(mode=None, as_padded_sequence=False)[source]#
Cast
VideoBoxesto a tensor with the temporal axis restored.- Parameters:
mode (
Optional[str], optional) – Output box format forwarded toBoxes.to_tensor(). WhenNone, uses the stored mode (vertices_plusby default). Default:Noneas_padded_sequence (
bool, optional) – Forwarded toBoxes.to_tensor(). It only changes the result of a list-backed container; aVideoBoxesbuilt from a \((B, T, N, 4, 2)\) tensor is not list-backed, so the value makes no difference there. The keyword is accepted because the inheritedget_boxes_shape()andto_mask()pass it. Default:False
- Return type:
- Returns:
Tensor shaped \((B, T, \ldots)\) where \(T\) is
temporal_channel_size.