def clamp_bbox(bbox, shape):
    """
    Clamp a bounding box to the dimensions of an array
    Parameters
    ----------
    bbox: ndarray([i1, i2, j1, j2])
        A bounding box
    shape: tuple
        Dimensions to which to clamp
    """
    [i1, i2, j1, j2] = bbox
    j1 = max(0, int(j1))
    i1 = max(0, int(i1))
    j2 = min(shape[1]-1, int(j2))
    i2 = min(shape[0]-1, int(i2))
    bbox[0:4] = [i1, i2, j1, j2]
    return bbox