Example #1
0
def get_color_ration(pixels_statistic):
    """计算图中最主要的四种颜色的分布 / Get the 4 main colors' ratio in image
    一幅4字的彩色验证码中,我们把占图片比例最多的四种颜色,根据面积由大到小排列为:first, second, third, fourth
    考虑到噪点和干扰线,四个字颜色分布可能为
    Type_A - 4个字分为4种不同颜色:first : second ∈ [1, 1.5) && second : third ∈ [1,1.5)
    Type_B - 4个字分为3种不同颜色,其中2个字颜色一样: first : second ∈ [1.5, 2.5) && second : third ∈ [1, 1.5)
    Type_C - 4个字分为2种不同颜色,每种颜色2个字: first:second ∈ [1, 1.5) && second : third ∈ [3.5, ∞)
    Type_D - 4个字分为2种不同颜色,其中1种颜色3个字,1种颜色1个字: first : second  ∈ [2.5, 3.5)
    Type_E - 4个字颜色相同: first : second ∈ [3.5, ∞)


    :param
        pixels_statistic: 像数统计结果 / The result of the statistic of pixel
    :return:
        string color_ratio_type:
    """

    if isinstance(pixels_statistic, dict):
        pixels_statistic_temp = pixels_statistic.copy()

        first = CustomDataStructureUtil.get_max(pixels_statistic_temp)
        pixels_statistic_temp.pop(first[0])

        second = CustomDataStructureUtil.get_max(pixels_statistic_temp)
        pixels_statistic_temp.pop(second[0])

        rotia_first_second = first[1] / second[1]

        if 1 <= rotia_first_second < 1.5:
            third = CustomDataStructureUtil.get_max(pixels_statistic_temp)
            pixels_statistic_temp.pop(third[0])
            rotia_second_third = second[1] / third[1]
            if 1 <= rotia_second_third < 1.5:
                return ColorRatioType.__TYPE_A__
            elif rotia_second_third > 3.5:
                return ColorRatioType.__TYPE_C__
        elif 1.5 <= rotia_first_second < 2.5:
            return ColorRatioType.__TYPE_B__
        elif 2.5 <= rotia_first_second < 3.5:
            return ColorRatioType.__TYPE_D__
        elif 3.5 <= rotia_first_second:
            return ColorRatioType.__TYPE_E__