def delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1):
    """
    Calculates the Delta E (CIE2000) of two colors.
    """
    color1_vector = _get_lab_color1_vector(color1)
    color2_matrix = _get_lab_color2_matrix(color2)
    delta_e = color_diff_matrix.delta_e_cie2000(
        color1_vector, color2_matrix, Kl=Kl, Kc=Kc, Kh=Kh)[0]
    return numpy.asscalar(delta_e)
示例#2
0
    def delta_e_matrix(self, lab_color_matrix, mode='cie2000', *args, **kwargs):
        """
        Compares this object with all colors in lab_color_matrix via Delta E.
        The matrix must be of shape (n,3) and must be composed of floats.
        Returns a distance vector of shape (n,).

        Valid modes:
         cie2000
         cie1976
         cie1994
         cmc
        """

        lab_color_vector = np.array([self.lab_l, self.lab_a, self.lab_b])

        mode = mode.lower()

        if mode == 'cie2000':
            return color_diff_matrix.delta_e_cie2000(lab_color_vector, lab_color_matrix)
        elif mode == 'cie1994':
            return color_diff_matrix.delta_e_cie1994(lab_color_vector, lab_color_matrix, **kwargs)
        elif mode == 'cie1976':
            return color_diff_matrix.delta_e_cie1976(lab_color_vector, lab_color_matrix)
        elif mode == 'cmc':
            return color_diff_matrix.delta_e_cmc(lab_color_vector, lab_color_matrix, **kwargs)
        else:
            raise InvalidDeltaEMode(mode)
def labColor2LystColor(target_color):
    # target_color is a lab converted color
    lab_color = getLystLabColor()
    lab_matrix = getLystColorMatrix()
    lab_color_vector = np.array(
        [target_color.lab_l, target_color.lab_a, target_color.lab_b])
    delta = delta_e_cie2000(lab_color_vector, lab_matrix)
    color = lab_color[np.argmin(delta)]
    return color
示例#4
0
def is_close(x0, y0, x1, y1):
    global thresh, image
    color_vec = np.array(image[x0, y0, :])
    color_mat = np.array([(image[x1, y1, 0], image[x1, y1, 1], image[x1, y1,
                                                                     2])])
    dist = np.asscalar(delta_e_cie2000(color_vec, color_mat)[0])
    print(dist)
    if dist < thresh:
        return True
    return False
示例#5
0
def delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1):
    """
    Calculates the Delta E (CIE2000) of two colors.
    """
    from warnings import warn
    warn("The current form of this class being depreciated in favor of not passing in arguments for Kl, Kc, and Kh when unity 1 is used. For applications wishing to retain the ability to change Kl, Kc, and/or Kh from default value of 1 see delta_e_cie_2000_nonunity. In a future release, the updated format as seen in delta_e_cie2000_update will be used")
    color1_vector = _get_lab_color1_vector(color1)
    color2_matrix = _get_lab_color2_matrix(color2)
    delta_e = color_diff_matrix.delta_e_cie2000(
        color1_vector, color2_matrix, Kl=Kl, Kc=Kc, Kh=Kh)[0]
    return numpy.asscalar(delta_e)
示例#6
0
def get_color_name(rgb_vector):
    ##Find color - https://making.lyst.com/2014/02/22/color-detection/
    # load list of 1000 random colors from the XKCD color chart

    # with open('colors.csv', 'rb') as csvfile:
    #     reader = csv.reader(csvfile)
    #     rgb_matrix = np.array([map(float, row[0:3]) for row in reader],dtype=float)

    # with open('colors.csv', 'rb') as csvfile:
    #     reader = csv.reader(csvfile)
    #     color_labels= np.array([row[3:] for row in reader])
    #     # the reference color
    #     lab_matrix=np.array([convert_color(sRGBColor(row[0],row[1],row[2],is_upscaled=True),LabColor) for row in rgb_matrix])
    # print lab_matrix
    # with open('lab_colors.csv', 'wb') as csvfile:
    #     writer = csv.writer(csvfile, delimiter=',',
    #                         quotechar='|', quoting=csv.QUOTE_MINIMAL)
    #     for idx,row in enumerate(lab_matrix):
    #          writer.writerow([row.lab_l,row.lab_a,row.lab_b,color_labels[idx][0]])
    with open(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'lab_colors.csv'), 'rb') as csvfile:
        reader = csv.reader(csvfile)
        lab_matrix = np.array([map(float, row[0:3]) for row in reader],
                              dtype=float)

    color_index = 0
    lab_color = convert_color(
        sRGBColor(rgb_vector[0],
                  rgb_vector[1],
                  rgb_vector[2],
                  is_upscaled=True), LabColor)
    #          writer.writerow([row.lab_l,row.lab_a,row.lab_b,color_labels[idx][0]])
    with open(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         'lab_colors.csv'), 'rb') as csvfile:
        reader = csv.reader(csvfile)
        color_labels = np.array([row[3:] for row in reader])

    _color = []
    _color.append(lab_color.lab_l)
    _color.append(lab_color.lab_a)
    _color.append(lab_color.lab_b)

    nearest_color = lab_matrix[np.argmin(delta_e_cie2000(_color, lab_matrix))]
    color_idx = 0
    for idx, item in enumerate(lab_matrix):
        if item[0] == nearest_color[0] and item[1] == nearest_color[
                1] and item[2] == nearest_color[2]:
            color_idx = idx
    # Find the color difference
    #print '%s OR %s is closest to %s %s' % (rgb_vector,_color, nearest_color,color_labels[color_idx])
    return color_labels[color_idx]
示例#7
0
def delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1):
    """
    Calculates the Delta E (CIE2000) of two colors.
    """
    from warnings import warn
    warn(
        "The current form of this class being depreciated in favor of not passing in arguments for Kl, Kc, and Kh when unity 1 is used. For applications wishing to retain the ability to change Kl, Kc, and/or Kh from default value of 1 see delta_e_cie_2000_nonunity. In a future release, the updated format as seen in delta_e_cie2000_update will be used"
    )
    color1_vector = _get_lab_color1_vector(color1)
    color2_matrix = _get_lab_color2_matrix(color2)
    delta_e = color_diff_matrix.delta_e_cie2000(color1_vector,
                                                color2_matrix,
                                                Kl=Kl,
                                                Kc=Kc,
                                                Kh=Kh)[0]
    return numpy.asscalar(delta_e)
def extract_colors(img_path, color_lab_matrix_path, color_names_path, labels,
                   merge_labels):
    quality = 1

    lab_matrix = pickle.load(open(color_lab_matrix_path, 'rb'))
    color_names = pickle.load(open(color_names_path, 'rb'))

    img = imread(img_path)
    num_rows = img.shape[0]
    num_cols = img.shape[1]
    num_pixels = num_rows * num_cols

    ce = ColorExtractor()
    image_attributes = {}
    image_attributes['height'] = num_rows
    image_attributes['width'] = num_cols
    #image_attributes['name'] = im_prefix +  ext
    category_labels = category_original_labels

    img_flat = img.transpose(2, 0, 1).reshape(3, -1).T

    if merge_labels == True:
        labels = label_merging(labels, category_labels)
        category_labels = category_merged_labels

    labels_flat = np.ravel(labels)

    uniq_labels = np.unique(labels_flat)
    color_pallete = {}

    for ind in list(uniq_labels):
        img_region = img_flat[labels_flat == ind]
        pixel_list = list(tuple(map(tuple, img_region)))
        num_colors = 5

        labels_region = (labels == ind)
        ymax, xmax = np.max(np.where(labels_region == True), 1)
        ymin, xmin = np.min(np.where(labels_region == True), 1)
        region_box = [xmin, ymin, xmax, ymax]

        pallete = ce.get_palette(pixel_list,
                                 color_count=num_colors,
                                 quality=quality)

        pallete = pallete[0]
        pixel_area = round((100.0 * img_region.shape[0]) / num_pixels, 2)

        if pixel_area < 1:
            continue

        r = pallete['r'] / 255.0
        g = pallete['g'] / 255.0
        b = pallete['b'] / 255.0

        color_rgb = sRGBColor(r, g, b)
        color_lab = convert_color(color_rgb, LabColor)
        color_lab_vec = np.array(
            [color_lab.lab_l, color_lab.lab_a, color_lab.lab_b])
        delta = delta_e_cie2000(color_lab_vec, lab_matrix)
        color_name = color_names[np.argmin(delta)]

        rgb_values = {}
        rgb_values['r'] = pallete['r']
        rgb_values['g'] = pallete['g']
        rgb_values['b'] = pallete['b']

        color_pallete[category_labels[ind]] = {
            'color': color_name,
            'pixel_area': pixel_area,
            'box': region_box,
            'rgb': rgb_values
        }

    image_attributes['info'] = color_pallete
    return image_attributes
示例#9
0
cPickled (n,3) numpy array LAB values such that row q maps to
index q in the lab color list
"""

import sys
import csv
import bz2

import numpy as np

# Does some sys.path manipulation so we can run examples in-place.
# noinspection PyUnresolvedReferences
import example_config

from colormath.color_diff_matrix import delta_e_cie2000
from colormath.color_objects import LabColor

# load list of 1000 random colors from the XKCD color chart
if sys.version_info >= (3, 0):
    reader = csv.DictReader(bz2.open('lab_matrix.csv.bz2', mode='rt'))
    lab_matrix = np.array([list(map(float, row.values())) for row in reader])
else:
    reader = csv.DictReader(bz2.BZ2File('lab_matrix.csv.bz2'))
    lab_matrix = np.array([map(float, row.values()) for row in reader])

color = LabColor(lab_l=69.34, lab_a=-0.88, lab_b=-52.57)
lab_color_vector = np.array([color.lab_l, color.lab_a, color.lab_b])
delta = delta_e_cie2000(lab_color_vector, lab_matrix)

print('%s is closest to %s' % (color, lab_matrix[np.argmin(delta)]))
index q in the lab color list
"""

import sys
import csv
import bz2

import numpy as np

# Does some sys.path manipulation so we can run examples in-place.
# noinspection PyUnresolvedReferences
import example_config

from colormath.color_diff_matrix import delta_e_cie2000
from colormath.color_objects import LabColor


# load list of 1000 random colors from the XKCD color chart
if sys.version_info >= (3, 0):
    reader = csv.DictReader(bz2.open('lab_matrix.csv.bz2', mode='rt'))
    lab_matrix = np.array([list(map(float, row.values())) for row in reader])
else:
    reader = csv.DictReader(bz2.BZ2File('lab_matrix.csv.bz2'))
    lab_matrix = np.array([map(float, row.values()) for row in reader])

color = LabColor(lab_l=69.34, lab_a=-0.88, lab_b=-52.57)
lab_color_vector = np.array([color.lab_l, color.lab_a, color.lab_b])
delta = delta_e_cie2000(lab_color_vector, lab_matrix)

print('%s is closest to %s' % (color, lab_matrix[np.argmin(delta)]))