Caught insane bug: was alwasy selecting last element. mIdx not mIdx

This commit is contained in:
mmcwilliams 2023-11-05 07:08:28 -05:00
parent 1a4273d08c
commit 983494a2a1
2 changed files with 16 additions and 3 deletions

View File

@ -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)

View File

@ -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)