marker_separation/py/comparison_comparison.py

85 lines
2.2 KiB
Python
Raw Normal View History

2023-10-23 14:32:35 +00:00
import cv2
import numpy as np
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-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]
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-11-04 01:46:40 +00:00
comp_colors = [white, red, green, blue, black]
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()
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)
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)
colors = remove_from_list(colors, closest)
ccbgr = convert_color(cc, 'RGB', 'BGR')
chosenbgr = convert_color(closest, space, 'BGR')
original = create_colored_image(100, 100, ccbgr)
chosen = create_colored_image(100, 100, chosenbgr)
2023-11-04 01:46:40 +00:00
print(f'{ccbgr} => {chosenbgr} = {dist}')
combined = np.hstack([original, chosen])
show.append(combined)
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
def get_colors (self, pallete, space) :
colors = []
for color in pallete :
colors.append(convert_color(color['color'], color['space'], space))
return colors
2023-10-23 20:09:08 +00:00
2023-10-23 14:32:35 +00:00
if __name__ == "__main__":
ComparisonComparison()