There is clearly a bug

This commit is contained in:
mmcwilliams 2023-11-03 21:46:40 -04:00
parent 3649e53850
commit 1a4273d08c
2 changed files with 64 additions and 23 deletions

View File

@ -1,6 +1,6 @@
import cv2
import numpy as np
from math import sqrt
from math import sqrt, pow
def convert_color (color, color_space_a, color_space_b) :
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
@ -64,32 +64,39 @@ def closest_color (colors, color):
return smallest_distance[0]
# Works for RGB, BGR, LAB and HSV(?)
def closest_color_pythagorean (colors, color) :
def closest_color_euclidean (colors, color) :
#print(len(colors))
mDist = float('inf')
mIdx = -1
color = [float(i) for i in list(color)]
for idx, comp in enumerate(colors) :
dist = pythagorean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2])
comp = [float(i) for i in list(comp)]
dist = euclidean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2])
#print(f'{color} -> {comp} = {dist}')
if dist < mDist :
mDist = dist
mIds = idx
return color[mIdx], mDist
return colors[mIdx], mDist
def closest_color_weighted_pythagorean (colors, color, space) :
def closest_color_weighted_euclidean (colors, color, space) :
#print(len(colors))
mDist = float('inf')
mIdx = -1
color = [float(i) for i in list(color)]
for idx, comp in enumerate(colors) :
comp = [float(i) for i in list(comp)]
if space == 'BGR' :
dist = weighed_pythagorean_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[1])
elif space == 'RGB' :
dist = weighed_pythagorean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2])
dist = weighted_euclidean_distance(comp[0], comp[1], comp[2], color[0], color[1], color[2])
else :
raise Exception(f'closest_color_weighted_pythagorean does not support color space {space}')
raise Exception(f'closest_color_weighted_euclidean does not support color space {space}')
break
#print(f'{color} -> {comp} = {dist}')
if dist < mDist :
mDist = dist
mIds = idx
return color[mIdx], mDist
return colors[mIdx], mDist
def create_colored_image (width, height, bgr_color):
image = np.zeros((height, width, 3), np.uint8)
@ -109,11 +116,28 @@ def list_match (a, b) :
return False
return True
def pythagorean_distance (r1, g1, b1, r2, g2, b2) :
return sqrt(pow(r1-r2, 2) + pow(g1-g2, 2) + pow(b1-b2, 2))
def rgb_euclidean_distance(rgba, rgbb) :
return euclidean_distance(rgba[0], rgba[1], rgba[2], rgbb[0], rgbb[1], rgbb[2])
def weighted_pythagorean_distance (r1, g1, b1, r2, g2, b2) :
def bgr_euclidean_distance(bgra, bgrb) :
return euclidean_distance(bgra[2], bgra[1], bgra[0], bgrb[2], bgrb[1], bgrb[0])
def numpy_distance (r1, g1, b1, r2, g2, b2) :
p0 = np.array([r1, g1, b1])
p1 = np.array([r2, g2, b2])
d = np.linalg.norm(p0 - p1)
return d
#return sqrt(pow(abs(r1-r2), 2) + pow(abs(g1-g2), 2) + pow(abs(b1-b2), 2))
def euclidean_distance (r1, g1, b1, r2, g2, b2):
d = 0.0
d = sqrt((r2 - r1)**2 + (g2 - g1)**2 + (b2 - b1)**2)
return d
def weighted_euclidean_distance (r1, g1, b1, r2, g2, b2) :
R = 0.30
G = 0.59
B = 0.11
return sqrt(pow((r1-r2) * R, 2) + pow((g1-g2) * G, 2) + pow((b1-b2) * B, 2))
#print(type(r1))
return sqrt( ((r2-r1) * R)**2 + ((g2-g1) * G)**2 + ((b2-b1) * B)**2 )

View File

@ -1,16 +1,30 @@
import cv2
import numpy as np
from pallete_schema import PalleteSchema
from common import convert_color, closest_color, create_colored_image, remove_from_list, closest_color_pythagorean, closest_color_weighted_pythagorean
from common import convert_color, closest_color, create_colored_image, remove_from_list, closest_color_euclidean, closest_color_weighted_euclidean, euclidean_distance, weighted_euclidean_distance
class ComparisonComparison:
def __init__ (self) :
self.compare_a()
# self.compare_b()
def compare_b (self) :
red = [0, 0, 255]
green = [0, 255, 0]
blue = [255, 0, 0]
white = [255, 255, 255]
black = [0, 0, 0]
print(euclidean_distance(red[0], red[1], red[2], green[0], green[1], green[2]))
def compare_a (self) :
white = [255, 255, 255]
red = [0, 10, 200]
green = [5, 250, 5]
blue = [240, 0, 20]
black = [0, 0, 0]
comp_colors = [red, green, blue]
comp_colors = [white, red, green, blue, black]
pallete = PalleteSchema('./palletes/printed_pallete.json')
@ -26,8 +40,9 @@ class ComparisonComparison:
rows.append(np.hstack(row))
row = []
show = np.vstack(rows)
cv2.imshow('image', show)
cv2.waitKey(0)
cv2.imshow('pallete', show)
cv2.waitKey(0)
cv2.destroyAllWindows()
color_spaces = ['RGB', 'BGR', 'LAB', 'HSV']
for space in color_spaces :
@ -38,9 +53,10 @@ class ComparisonComparison:
for cc in comp_colors :
cccompare = convert_color(cc, 'RGB', space)
print(cccompare)
closest = closest_color_pythagorean(colors, cccompare)
print(closest)
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')
@ -49,14 +65,15 @@ class ComparisonComparison:
original = create_colored_image(100, 100, ccbgr)
chosen = create_colored_image(100, 100, chosenbgr)
print(f'{ccbgr} => {chosenbgr}')
print(f'{ccbgr} => {chosenbgr} = {dist}')
combined = np.hstack([original, chosen])
show.append(combined)
show = np.vstack(show)
cv2.imshow('image', show)
cv2.imshow(space, show)
cv2.waitKey(0)
cv2.destroyAllWindows()
def get_colors (self, pallete, space) :
colors = []