def IoU(X, Y):
    """
    Calculates the Intersection over Union for two bounding boxes, X and Y, which are 4D vectos containing the top left and bottom right positions of the bounding box
    """

    max_tl_x = max(X[0], Y[0])
    max_tl_y = max(X[1], Y[1])
    min_br_x = min(X[2], Y[2])
    min_br_y = min(X[3], Y[3])

    intersection_area = max(0, min_br_x - max_tl_x + 1) * max(0, min_br_y - max_tl_y + 1)

    X_area = (X[2]-X[0]+1)*(X[3]-X[1]+1)
    Y_area = (Y[2]-Y[0]+1)*(Y[3]-Y[1]+1)

    return intersection_area / (float(X_area + Y_area - intersection_area))