Exemplo n.º 1
0
    def run_test(self, test_value, image_file):
        global chemical_property
        global interpolation_metric

        test_file = 'unit/' + chemical_property + '/' + image_file
        img = get_img('file', test_file)

        value = None

        if interpolation_metric == Metric.RGB:
            value = interpolate_chemical_property_from_img_rgb(
                chemical_property, img)
        else:
            value = interpolate_chemical_property_from_img_linear(
                chemical_property, img, interpolation_metric)

        print("[TEST]: Testing %f = %f" % (value, test_value))

        try:
            self.errors.append(abs(value - test_value))
            self.results[test_value] = value
            self.assertLess(abs(value - test_value), test_accuracy)
        except Exception:
            if visualize_fails:
                r, g, b = get_average_rgb_from_img(img)
                scale = get_scale_map(chemical_property)
                visualize([r, g, b], scale)

        return abs(value - test_value)
def create_interpolated_rgb_graph(chemical, increments=10, labels=False):
    scale = get_scale_map(chemical)

    red = []
    green = []
    blue = []
    values = []

    # 3D Plot
    fig = plt.figure()
    ax = Axes3D(fig)

    # turn keys into floats to sort
    keys = sorted([float(val) for val in list(scale.keys())])
    # turn keys back into strings
    keys = [str(val) for val in keys]
    previous_key = None
    print(keys)
    print(scale.keys())
    for key in keys:
        # interpolate increments amount towards next key

        if len(red) > 0:
            inc_size = (float(key) - float(previous_key)) / float(increments)
            for i in range(1, increments):
                print("Interpolating " +
                      str(float(previous_key) + inc_size * i))
                actual, low, high = interpolate_rgb_values(
                    chemical,
                    float(previous_key) + inc_size * i)
                red.append(actual[1][0])
                green.append(actual[1][1])
                blue.append(actual[1][2])
                if labels:
                    ax.text(red[-1], green[-1], blue[-1],
                            str(float(previous_key) + inc_size * i))

        if key == '0.0':
            key = '0'
        red.append(scale[key][0])
        green.append(scale[key][1])
        blue.append(scale[key][2])
        ax.text(red[-1], green[-1], blue[-1], key)

        previous_key = key

    ax.scatter(red, green, blue)

    ax.set_title(chemical + '_rgb_3d')
    ax.set_xlabel("Red")
    ax.set_ylabel("Green")
    ax.set_zlabel("Blue")

    plt.savefig('./research/' + chemical + '/rgb_interpolated_3d.png')
    plt.show()
def interpolate_rgb_values(chemical, value):
    scale = get_scale_map(chemical)

    # find closest two
    count = 0

    low_value = 0
    high_value = 9999999

    for key in scale:
        dif = value - float(key)

        print(float(key))

        if dif == 0:
            # return rgb values
            print
            return [value, scale[key]], [value,
                                         scale[key]], [value, scale[key]]
        elif dif > 0 and float(key) > low_value:
            low_value = float(key)
#            print("Low: " + str(low_value))
        elif dif < 0 and float(key) < high_value:
            high_value = float(key)


#            print("High: " + str(high_value))

    print(
        str(value) + " between " + str(low_value) + " and " + str(high_value))

    # interpolate rgb values
    ratio = (float(value) - low_value) / (float(high_value) - float(low_value))
    inter_r = float(255 * scale[str(low_value)][0] +
                    (scale[str(high_value)][0] - scale[str(low_value)][0]) *
                    ratio * 255) / 255.0
    inter_g = float(255 * scale[str(low_value)][1] +
                    (scale[str(high_value)][1] - scale[str(low_value)][1]) *
                    ratio * 255) / 255.0
    inter_b = float(255 * scale[str(low_value)][2] +
                    (scale[str(high_value)][2] - scale[str(low_value)][2]) *
                    ratio * 255) / 255.0

    print(inter_r, inter_g, inter_b)

    low_rgb = scale[str(
        low_value)]  # [float(255*val) for val in scale[str(low_value)]]
    high_rgb = scale[str(
        high_value)]  # [float(255*val) for val in scale[str(high_value)]]

    print(low_rgb)
    print(high_rgb)

    return [value, [inter_r, inter_g,
                    inter_b]], [low_value, low_rgb], [high_value, high_rgb]
def visualize_scale(chemical):
    scale = get_scale_map(chemical)

    # create palette of rgb values
    palette = []
    indices = [[]]
    index = 0
    for key in sorted(scale.keys()):
        #np.append(palette, scale[key])
        palette.append(scale[key])
        indices[0].append(index)
        index += 1

    print(indices)
    palette = np.array(palette)
    indices = np.array(indices)

    io.imshow(palette[indices])
    plt.title(chemical + " scale")
    plt.savefig('./research/' + chemical + '/scale.png')
    plt.show()
def create_scale_hue_graph(chemical):
    scale = get_scale_map(chemical)

    hues = []
    sat = []
    val = []
    red = []
    green = []
    blue = []
    values = []

    # 3D Plot
    fig = plt.figure()
    ax = Axes3D(fig)

    for key in scale:
        values.append(float(key))

        h, s, v = colorsys.rgb_to_hsv(*(scale[key]))
        if h > 0.5:
            h -= 1

        hues.append(h)
        sat.append(s)
        val.append(v)

        ax.text(hues[-1], sat[-1], val[-1], str(key))

    ax.scatter(hues, sat, val)

    ax.set_title(chemical + '_hsv_3d')
    ax.set_xlabel("Hue")
    ax.set_ylabel("Saturation")
    ax.set_zlabel("Value")

    plt.savefig('./research/' + chemical + '/hsv_3d.png')
    plt.show()
    ax.clear()

    # Linear plots
    plt.scatter(values, hues)
    plt.scatter(values, sat)
    plt.scatter(values, val)
    plt.legend(('Hue', 'Saturation', 'Value'))

    plt.title(chemical + '_hsv_linear')
    plt.xlabel(chemical)
    plt.ylabel('value')
    plt.savefig('./research/' + chemical + '/hsv_scale.png')
    plt.show()

    # 3D Plot
    fig = plt.figure()
    ax = Axes3D(fig)

    for key in scale:
        red.append(scale[key][0])
        green.append(scale[key][1])
        blue.append(scale[key][2])

        ax.text(red[-1], green[-1], blue[-1], str(key))

    ax.scatter(red, green, blue)

    ax.set_title(chemical + '_rgb_3d')
    ax.set_xlabel("Red")
    ax.set_ylabel("Green")
    ax.set_zlabel("Blue")

    plt.savefig('./research/' + chemical + '/rgb_3d.png')
    plt.show()
    ax.clear()

    # Linear plots
    plt.scatter(values, red)
    plt.scatter(values, green)
    plt.scatter(values, blue)
    plt.legend(('Red', 'Green', 'Blue'))

    plt.title(chemical + '_rgb_linear')
    plt.xlabel(chemical)
    plt.ylabel('value')
    plt.savefig('./research/' + chemical + '/rgb_linear.png')
    plt.show()