90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
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_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 = [white, red, green, blue, black]
|
|
|
|
pallete = PalleteSchema('./palletes/colored_pallete.json')
|
|
|
|
i = 0
|
|
row = []
|
|
rows = []
|
|
for color in pallete.colors :
|
|
cell = create_colored_image(60, 40, color['color'])
|
|
row.append(cell)
|
|
i += 1
|
|
if i == 10 :
|
|
i = 0
|
|
rows.append(np.hstack(row))
|
|
row = []
|
|
show = np.vstack(rows)
|
|
cv2.imshow('pallete', show)
|
|
cv2.waitKey(0)
|
|
cv2.destroyAllWindows()
|
|
|
|
color_spaces = ['RGB', 'BGR', 'LAB', 'HSV']
|
|
for space in color_spaces :
|
|
print(f'Comparing in color space {space}')
|
|
|
|
colors = self.get_colors(pallete.colors, space)
|
|
show = []
|
|
|
|
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)
|
|
|
|
show = np.vstack(show)
|
|
cv2.imshow(space, show)
|
|
cv2.waitKey(0)
|
|
cv2.destroyAllWindows()
|
|
|
|
def get_colors (self, pallete, space) :
|
|
colors = []
|
|
for color in pallete :
|
|
colors.append(convert_color(color['color'], color['space'], space))
|
|
return colors
|
|
|
|
if __name__ == "__main__":
|
|
ComparisonComparison() |