def remove_degenerate_bboxes(boxes, dim0: int, dim1: int, min_boxside=0):
    """Remove bboxes beyond image or smaller than a minimum size.
    This assumes a format where columns 0, 1 are min dim0 and dim1, while
    columns 2, 3 are max dim0 and dim1, respectively.
    """
    # adjust boxes
    boxes[boxes < 0] = 0
    boxes[boxes[:, 0] > dim0, 0] = dim0
    boxes[boxes[:, 2] > dim0, 2] = dim0
    boxes[boxes[:, 1] > dim1, 1] = dim1
    boxes[boxes[:, 3] > dim1, 3] = dim1

    # remove boxes outside cropped region
    keep1 = boxes[:, 3] - boxes[:, 1] > min_boxside
    keep2 = boxes[:, 2] - boxes[:, 0] > min_boxside
    keep = keep1 & keep2
    boxes = boxes[keep]

    return boxes, keep