def judge_area_similar(foreground_area,
                       background_area,
                       similar_threshold=0.5):
    """
    判断两个区域是否相似,这有利于poisson编辑的效果,这只是固定位置判断,输入的都是两个mask区域

    1.判断直方图
    2. 判断
    :param foreground_area:
    :param background_area:
    :param similar_threshold:有百分之多少的相似 默认 0.5
    :return: 返回一个bool数,表示该选择的预期ok否
    """

    # 直方图
    similar_score = myCalImage.calc_similar(foreground_area, background_area)

    print('直方图相似程度:', similar_score)
    if similar_score > 0.5 * 100:
        return True
    else:
        return False
def splicing_tamper_one_image(input_foreground,
                              input_background,
                              mask,
                              similar_judge=True):
    """
    在输入这个函数之前,输入的两个参数需要以及经过挑选了的,需要import poisson融合的代码
    :param foreground:
    :param background:
    :return: 返回两个参数:直接篡改图, poisson融合篡改图, GT
    """

    I = input_foreground
    I1 = I
    # mask 是 01 蒙版
    I1[:, :, 0] = np.array(I[:, :, 0] * mask)
    I1[:, :, 1] = np.array(I[:, :, 1] * mask)
    I1[:, :, 2] = np.array(I[:, :, 2] * mask)
    # differece_8是background的edge
    difference_8 = mask_to_outeedge(mask)

    difference_8_dilation = dilation.binary_dilation(difference_8,
                                                     np.ones((3, 3)))
    difference_8_dilation = np.where(difference_8_dilation == True, 1, 0)
    double_edge_candidate = difference_8_dilation + mask
    double_edge = np.where(double_edge_candidate == 2, 1, 0)
    ground_truth = np.where(double_edge == 1, 255, 0) + np.where(
        difference_8 == 1, 100, 0) + np.where(mask == 1, 50,
                                              0)  # 所以内侧边缘就是100的灰度值
    b1 = input_background

    background = Image.fromarray(b1, 'RGB')
    foreground = Image.fromarray(I1, 'RGB').convert('RGBA')
    datas = foreground.getdata()

    newData = []
    for item in datas:
        if item[0] == 0 and item[1] == 0 and item[2] == 0:
            newData.append((0, 0, 0, 0))
        else:
            newData.append(item)
    foreground.putdata(newData)
    background = background.resize((foreground.size[0], foreground.size[1]),
                                   Image.ANTIALIAS)
    # input_background = np.resize(input_background,(foreground.size[1], foreground.size[0],3))
    background_area = np.array(background)

    try:

        if similar_judge:
            foreground_area = I1
            background_area[:, :,
                            0] = np.array(background_area[:, :, 0] * mask)
            background_area[:, :,
                            1] = np.array(background_area[:, :, 1] * mask)
            background_area[:, :,
                            2] = np.array(background_area[:, :, 2] * mask)

            foreground_area = Image.fromarray(foreground_area)
            background_area = Image.fromarray(background_area)

            if myCalImage.calc_similar(foreground_area, background_area) > 0.2:
                pass
            else:
                return False, False, False
    except Exception as e:
        print(e)
        traceback.print_exc()

    try:
        mask = Image.fromarray(mask)
    except Exception as e:
        print('mask to Image error', e)
    # 在这里的时候,mask foreground background 尺寸都是一致的了,poisson融合时,offset置为0
    try:
        poisson_foreground = cv2.cvtColor(
            np.asarray(foreground.convert('RGB')), cv2.COLOR_RGB2BGR)
        poisson_background = cv2.cvtColor(np.asarray(background),
                                          cv2.COLOR_RGB2BGR)
        poisson_mask = np.asarray(mask)
        poisson_mask = np.where(poisson_mask == 1, 255, 0)
        poisson_fusion_image = poisson_image_editing.poisson_fusion(
            poisson_foreground, poisson_background, poisson_mask)
        poisson_fusion_image = Image.fromarray(
            cv2.cvtColor(poisson_fusion_image, cv2.COLOR_BGR2RGB))
        background.paste(foreground, (0, 0), mask=foreground.split()[3])
        return background, poisson_fusion_image, ground_truth
    except Exception as e:
        traceback.print_exc()