From 983494a2a1e764f5ddac9e1e40eebe73d25a68f0 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Sun, 5 Nov 2023 07:08:28 -0500 Subject: [PATCH] Caught insane bug: was alwasy selecting last element. mIdx not mIdx --- py/common.py | 14 +++++++++++--- py/comparison_comparison.py | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/py/common.py b/py/common.py index b7c3895..db689a6 100644 --- a/py/common.py +++ b/py/common.py @@ -75,7 +75,7 @@ def closest_color_euclidean (colors, color) : #print(f'{color} -> {comp} = {dist}') if dist < mDist : mDist = dist - mIds = idx + mIdx = idx return colors[mIdx], mDist def closest_color_weighted_euclidean (colors, color, space) : @@ -86,7 +86,7 @@ def closest_color_weighted_euclidean (colors, color, space) : for idx, comp in enumerate(colors) : comp = [float(i) for i in list(comp)] if space == 'BGR' : - dist = weighted_euclidean_distance(comp[2], comp[1], comp[0], color[2], color[1], color[1]) + dist = weighted_euclidean_distance(comp[2], comp[1], comp[0], color[2], color[1], color[0]) elif space == 'RGB' : dist = weighted_euclidean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2]) else : @@ -95,7 +95,7 @@ def closest_color_weighted_euclidean (colors, color, space) : #print(f'{color} -> {comp} = {dist}') if dist < mDist : mDist = dist - mIds = idx + mIdx = idx return colors[mIdx], mDist def create_colored_image (width, height, bgr_color): @@ -141,3 +141,11 @@ def weighted_euclidean_distance (r1, g1, b1, r2, g2, b2) : #print(type(r1)) return sqrt( ((r2-r1) * R)**2 + ((g2-g1) * G)**2 + ((b2-b1) * B)**2 ) +def rgb_to_luma (r, g, b) : + R = 0.2126 + G = 0.7152 + B = 0.0722 + + return (r * R) + (g * G) + (b * B) + + diff --git a/py/comparison_comparison.py b/py/comparison_comparison.py index 537ba27..1871d4b 100644 --- a/py/comparison_comparison.py +++ b/py/comparison_comparison.py @@ -53,19 +53,24 @@ class ComparisonComparison: for cc in comp_colors : cccompare = convert_color(cc, 'RGB', space) + if space == 'RGB' or space == 'BGR' : closest, dist = closest_color_weighted_euclidean(colors, cccompare, space) else : closest, dist = closest_color_euclidean(colors, cccompare) + colors = remove_from_list(colors, closest) ccbgr = convert_color(cc, 'RGB', 'BGR') chosenbgr = convert_color(closest, space, 'BGR') + dcheck = euclidean_distance(ccbgr[2], ccbgr[1], ccbgr[0], chosenbgr[2], chosenbgr[1], chosenbgr[0]) + original = create_colored_image(100, 100, ccbgr) chosen = create_colored_image(100, 100, chosenbgr) print(f'{ccbgr} => {chosenbgr} = {dist}') + print(f'dcheck = {dcheck}') combined = np.hstack([original, chosen]) show.append(combined)