def recolor(img, original, new_palette): h, w, d = img.shape newimg = np.empty((h, w, 3)) for i in range(h): for j in range(w): r, g, b = hex2rgb(new_palette[original.index( get_color(img, i, j, original))]) newimg[i, j] = [r, g, b] return np.uint8(newimg)
def perturb(palette, temp, fixed=None): #fixed is list of palette indeces that should not be changed unfixed = [] if fixed is None: unfixed = [i for i in range(len(palette))] else: for i in range(len(palette)): if i not in fixed: unfixed.append(i) num = len(unfixed) #swap 2 colors color1 = np.random.randint(0, num) color2 = np.random.randint(0, num) oopsies = palette[unfixed[color1]] palette[unfixed[color1]] = palette[unfixed[color2]] palette[unfixed[color2]] = oopsies # perturb randomly chosen color rate = 1 sigma = rate * temp color = np.random.randint(0, num) r, g, b = hex2rgb(palette[unfixed[color]]) rp = np.random.normal(0, sigma) gp = np.random.normal(0, sigma) bp = np.random.normal(0, sigma) if r + rp > 255 or r + rp < 0: r = r - rp else: r = r + rp if g + gp > 255 or g + gp < 0: g = g - gp else: g = g + gp if b + bp > 255 or b + bp < 0: b = b - bp else: b = b + bp r = max(0, min(255, int(r))) g = max(0, min(255, int(g))) b = max(0, min(255, int(b))) newhex = rgb2hex((r, g, b)) palette[unfixed[color]] = newhex return palette
def hex2lab(color): r, g, b = hex2rgb(color) rgb = [[[r / 255, g / 255, b / 255]]] return rgb2lab(rgb)[0][0] / 100.0
def compat_features(c1, c2, c3, c4, c5): colors = [c1, c2, c3, c4, c5] rgb = np.empty((5, 3)) lab = np.empty((5, 3)) hsv = np.empty((5, 3)) chsv = np.empty((5, 3)) for idx, c in enumerate(colors): r, g, b = hex2rgb(c) rgb1 = np.array([r / 255, g / 255, b / 255]) lab1 = hex2lab(c) hsv1 = rgb2hsv([[rgb1]])[0][0] chsv1 = [hsv1[1] * cos(hsv1[0]), hsv1[0] * sin(hsv1[0]), hsv1[2]] rgb[idx] = rgb1 lab[idx] = lab1 hsv[idx] = hsv1 chsv[idx] = chsv1 sort_idx = np.argsort(lab[:, 0]) rgb_sorted = rgb[sort_idx].flatten() lab_sorted = lab[sort_idx].flatten() hsv_sorted = hsv[sort_idx].flatten() chsv_sorted = chsv[sort_idx].flatten() rgb_diff = np.zeros((3, 4)) lab_diff = np.zeros((3, 4)) hsv_diff = np.zeros((3, 4)) chsv_diff = np.zeros((3, 4)) for i in range(1, 5): rgb_diff[0, i - 1] = rgb[i, 0] - rgb[i - 1, 0] rgb_diff[1, i - 1] = rgb[i, 1] - rgb[i - 1, 1] rgb_diff[2, i - 1] = rgb[i, 2] - rgb[i - 1, 2] lab_diff[0, i - 1] = lab[i, 0] - lab[i - 1, 0] lab_diff[1, i - 1] = lab[i, 1] - lab[i - 1, 1] lab_diff[2, i - 1] = lab[i, 2] - lab[i - 1, 2] minSatVal = min(np.concatenate((hsv[i - 1:i, 1], hsv[i - 1:i, 2]))) if minSatVal >= 0.2: pts = np.sort([hsv[i, 1], hsv[i - 1, 1]]) hsv_diff[0, i - 1] = min(pts[1] - pts[0], 1 - (pts[1] - pts[0])) hsv_diff[1, i - 1] = hsv[i, 1] - hsv[i - 1, 1] hsv_diff[2, i - 1] = hsv[i, 2] - hsv[i - 1, 2] chsv_diff[0, i - 1] = chsv[i, 0] - chsv[i - 1, 0] chsv_diff[1, i - 1] = chsv[i, 1] - chsv[i - 1, 1] chsv_diff[2, i - 1] = chsv[i, 2] - chsv[i - 1, 2] sort_rgb_diff = np.concatenate( (np.sort(rgb_diff[0]), np.sort(rgb_diff[1]), np.sort(rgb_diff[2]))) sort_lab_diff = np.concatenate( (np.sort(lab_diff[0]), np.sort(lab_diff[1]), np.sort(lab_diff[2]))) sort_hsv_diff = np.concatenate( (np.sort(hsv_diff[0]), np.sort(hsv_diff[1]), np.sort(hsv_diff[2]))) sort_chsv_diff = np.concatenate( (np.sort(chsv_diff[0]), np.sort(chsv_diff[1]), np.sort(chsv_diff[2]))) rgb_mean = np.mean(rgb, axis=0) lab_mean = np.mean(lab, axis=0) hsv_mean = np.mean(hsv, axis=0) chsv_mean = np.mean(chsv, axis=0) rgb_std = np.std(rgb, axis=0) lab_std = np.std(lab, axis=0) hsv_std = np.std(hsv, axis=0) chsv_std = np.std(chsv, axis=0) rgb_median = np.median(rgb, axis=0) lab_median = np.median(lab, axis=0) hsv_median = np.median(hsv, axis=0) chsv_median = np.median(chsv, axis=0) rgb_max = np.amax(rgb, axis=0) lab_max = np.amax(lab, axis=0) hsv_max = np.amax(hsv, axis=0) chsv_max = np.amax(chsv, axis=0) rgb_min = np.amin(rgb, axis=0) lab_min = np.amin(lab, axis=0) hsv_min = np.amin(hsv, axis=0) chsv_min = np.amin(chsv, axis=0) rgb_range = rgb_max - rgb_min lab_range = lab_max - lab_min hsv_range = hsv_max - hsv_min chsv_range = chsv_max - chsv_min return np.concatenate( (chsv.flatten(), chsv_sorted.flatten(), chsv_diff.flatten(), sort_chsv_diff.flatten(), chsv_mean.flatten(), chsv_std.flatten(), chsv_median.flatten(), chsv_max.flatten(), chsv_min.flatten(), chsv_range.flatten(), lab.flatten(), lab_sorted.flatten(), lab_diff.flatten(), sort_lab_diff.flatten(), lab_mean.flatten(), lab_std.flatten(), lab_median.flatten(), lab_max.flatten(), lab_min.flatten(), lab_range.flatten(), hsv.flatten(), hsv_sorted.flatten(), hsv_diff.flatten(), sort_hsv_diff.flatten(), hsv_mean.flatten(), hsv_std.flatten(), hsv_median.flatten(), hsv_max.flatten(), hsv_min.flatten(), hsv_range.flatten(), rgb.flatten(), rgb_sorted.flatten(), rgb_diff.flatten(), sort_rgb_diff.flatten(), rgb_mean.flatten(), rgb_std.flatten(), rgb_median.flatten(), rgb_max.flatten(), rgb_min.flatten(), rgb_range.flatten()))