2023-10-23 14:32:35 +00:00
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2023-10-24 02:46:18 +00:00
|
|
|
from pallete_schema import PalleteSchema
|
2023-11-04 01:46:40 +00:00
|
|
|
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
|
2023-10-23 14:32:35 +00:00
|
|
|
|
|
|
|
class ComparisonComparison:
|
2023-10-23 20:09:08 +00:00
|
|
|
def __init__ (self) :
|
2023-11-04 01:46:40 +00:00
|
|
|
self.compare_a()
|
|
|
|
# self.compare_b()
|
2023-10-24 23:42:03 +00:00
|
|
|
|
2023-11-04 01:46:40 +00:00
|
|
|
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]
|
2023-10-24 20:14:56 +00:00
|
|
|
red = [0, 10, 200]
|
|
|
|
green = [5, 250, 5]
|
|
|
|
blue = [240, 0, 20]
|
2023-11-04 01:46:40 +00:00
|
|
|
black = [0, 0, 0]
|
2023-10-24 23:42:03 +00:00
|
|
|
|
2023-11-04 01:46:40 +00:00
|
|
|
comp_colors = [white, red, green, blue, black]
|
2023-10-24 23:42:03 +00:00
|
|
|
|
2023-10-24 20:45:22 +00:00
|
|
|
pallete = PalleteSchema('./palletes/printed_pallete.json')
|
2023-10-26 02:43:58 +00:00
|
|
|
|
|
|
|
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)
|
2023-11-04 01:46:40 +00:00
|
|
|
cv2.imshow('pallete', show)
|
|
|
|
cv2.waitKey(0)
|
|
|
|
cv2.destroyAllWindows()
|
2023-10-24 23:42:03 +00:00
|
|
|
|
|
|
|
color_spaces = ['RGB', 'BGR', 'LAB', 'HSV']
|
2023-10-24 21:09:09 +00:00
|
|
|
for space in color_spaces :
|
|
|
|
print(f'Comparing in color space {space}')
|
2023-10-24 23:42:03 +00:00
|
|
|
|
2023-10-24 21:09:09 +00:00
|
|
|
colors = self.get_colors(pallete.colors, space)
|
|
|
|
show = []
|
2023-10-24 23:42:03 +00:00
|
|
|
|
2023-10-24 21:09:09 +00:00
|
|
|
for cc in comp_colors :
|
|
|
|
cccompare = convert_color(cc, 'RGB', space)
|
2023-11-05 12:08:28 +00:00
|
|
|
|
2023-11-04 01:46:40 +00:00
|
|
|
if space == 'RGB' or space == 'BGR' :
|
|
|
|
closest, dist = closest_color_weighted_euclidean(colors, cccompare, space)
|
|
|
|
else :
|
|
|
|
closest, dist = closest_color_euclidean(colors, cccompare)
|
2023-11-05 12:08:28 +00:00
|
|
|
|
2023-11-21 18:28:49 +00:00
|
|
|
colors = remove_from_list(colors, closest)
|
2023-10-24 21:09:09 +00:00
|
|
|
|
|
|
|
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
|
|
|
chosenbgr = convert_color(closest, space, 'BGR')
|
|
|
|
|
2023-11-05 12:08:28 +00:00
|
|
|
dcheck = euclidean_distance(ccbgr[2], ccbgr[1], ccbgr[0], chosenbgr[2], chosenbgr[1], chosenbgr[0])
|
|
|
|
|
2023-10-24 21:09:09 +00:00
|
|
|
original = create_colored_image(100, 100, ccbgr)
|
2023-10-24 23:42:03 +00:00
|
|
|
chosen = create_colored_image(100, 100, chosenbgr)
|
|
|
|
|
2023-11-04 01:46:40 +00:00
|
|
|
print(f'{ccbgr} => {chosenbgr} = {dist}')
|
2023-11-05 12:08:28 +00:00
|
|
|
print(f'dcheck = {dcheck}')
|
2023-10-24 21:09:09 +00:00
|
|
|
|
|
|
|
combined = np.hstack([original, chosen])
|
|
|
|
show.append(combined)
|
2023-10-24 23:42:03 +00:00
|
|
|
|
2023-10-24 21:09:09 +00:00
|
|
|
show = np.vstack(show)
|
2023-11-04 01:46:40 +00:00
|
|
|
cv2.imshow(space, show)
|
2023-10-24 20:45:22 +00:00
|
|
|
cv2.waitKey(0)
|
2023-11-04 01:46:40 +00:00
|
|
|
cv2.destroyAllWindows()
|
2023-10-23 20:09:08 +00:00
|
|
|
|
2023-10-24 21:09:09 +00:00
|
|
|
def get_colors (self, pallete, space) :
|
2023-10-24 20:14:56 +00:00
|
|
|
colors = []
|
|
|
|
for color in pallete :
|
2023-10-24 21:09:09 +00:00
|
|
|
colors.append(convert_color(color['color'], color['space'], space))
|
2023-10-24 20:14:56 +00:00
|
|
|
return colors
|
2023-10-23 20:09:08 +00:00
|
|
|
|
2023-10-23 14:32:35 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
ComparisonComparison()
|