MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / ClipBoxToImage

Class ClipBoxToImage

monai/apps/detection/transforms/array.py:351–397  ·  view source on GitHub ↗

Clip the bounding boxes and the associated labels/scores to make sure they are within the image. There might be multiple arrays of labels/scores associated with one array of boxes. Args: remove_empty: whether to remove the boxes and corresponding labels that are actually empty

Source from the content-addressed store, hash-verified

349
350
351class ClipBoxToImage(Transform):
352 """
353 Clip the bounding boxes and the associated labels/scores to make sure they are within the image.
354 There might be multiple arrays of labels/scores associated with one array of boxes.
355
356 Args:
357 remove_empty: whether to remove the boxes and corresponding labels that are actually empty
358 """
359
360 backend = [TransformBackends.TORCH, TransformBackends.NUMPY]
361
362 def __init__(self, remove_empty: bool = False) -> None:
363 self.remove_empty = remove_empty
364
365 def __call__( # type: ignore
366 self,
367 boxes: NdarrayOrTensor,
368 labels: Sequence[NdarrayOrTensor] | NdarrayOrTensor,
369 spatial_size: Sequence[int] | int,
370 ) -> tuple[NdarrayOrTensor, tuple | NdarrayOrTensor]:
371 """
372 Args:
373 boxes: bounding boxes, Nx4 or Nx6 torch tensor or ndarray. The box mode is assumed to be ``StandardMode``
374 labels: Sequence of array. Each element represents classification labels or scores
375 corresponding to ``boxes``, sized (N,).
376 spatial_size: The spatial size of the image where the boxes are attached. len(spatial_size) should be in [2, 3].
377
378 Returns:
379 - clipped boxes, does not share memory with original boxes
380 - clipped labels, does not share memory with original labels
381
382 Example:
383 .. code-block:: python
384
385 box_clipper = ClipBoxToImage(remove_empty=True)
386 boxes = torch.ones(2, 6)
387 class_labels = torch.Tensor([0, 1])
388 pred_scores = torch.Tensor([[0.4,0.3,0.3], [0.5,0.1,0.4]])
389 labels = (class_labels, pred_scores)
390 spatial_size = [32, 32, 32]
391 boxes_clip, labels_clip_tuple = box_clipper(boxes, labels, spatial_size)
392 """
393 spatial_dims: int = get_spatial_dims(boxes=boxes)
394 spatial_size = ensure_tuple_rep(spatial_size, spatial_dims) # match the spatial image dim
395
396 boxes_clip, keep = clip_boxes_to_image(boxes, spatial_size, self.remove_empty)
397 return boxes_clip, select_labels(labels, keep)
398
399
400class BoxToMask(Transform):

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…