tf.keras.utils.bounding_boxes.crop

Crops bounding boxes based on the given offsets and dimensions.

This function crops bounding boxes to a specified region defined by top, left, height, and width. The boxes are first converted to xyxy format, cropped, and then returned.

boxes The bounding boxes to crop. A NumPy array or tensor of shape (N, 4) or (batch_size, N, 4).
top The vertical offset of the top-left corner of the cropping region.
left The horizontal offset of the top-left corner of the cropping region.
height The height of the cropping region. Defaults to None.
width The width of the cropping region. Defaults to None.
bounding_box_format The format of the input bounding boxes. Defaults to "xyxy".

The cropped bounding boxes.

Example:

boxes = np.array([[10, 20, 50, 60], [70, 80, 100, 120]])  # xyxy format
cropped_boxes = keras.utils.bounding_boxes.crop(
    boxes, bounding_box_format="xyxy", top=10, left=20, height=40, width=30
)  # Cropping a 30x40 region starting at (20, 10)
print(cropped_boxes)
# Expected output:
# array([[ 0., 10., 30., 50.],
#        [50., 70., 80., 110.]])