def get_foreground_roi(foreground_img, background_img, background_rect):
    """
    获取前景图需要的区域
    :param foreground_img: 前景图
    :param background_img: 背景图
    :param background_rect: 背景区域的left top和right bottom 2点坐标
    :return:
    """
    foreground_h, foreground_w = foreground_img.shape[:2]
    background_h, background_w = background_img.shape[:2]

    # 左上角坐标
    left_top = (
        0 if background_rect[0][0] >= 0 else (-background_rect[0][0]),
        0 if background_rect[0][1] >= 0 else (-background_rect[0][1])
    )

    # 右下角
    right_bottom = (
        foreground_w if background_rect[1][0] <= background_w else (
                foreground_w - (background_rect[1][0] - background_w)),
        foreground_h if background_rect[1][1] <= background_h else (
                foreground_h - (background_rect[1][1] - background_h))
    )

    # 获取分割后的帽子ROI
    return foreground_img[
           left_top[1]:right_bottom[1], left_top[0]:right_bottom[0]
           ]