Improve color conversion and loop through different color spaces when making comparisons

This commit is contained in:
mmcwilliams 2023-10-24 17:09:09 -04:00
parent 1d23aac7d1
commit aef8ab8267
2 changed files with 34 additions and 13 deletions

View File

@ -35,8 +35,20 @@ def convert_color (color, color_space_a, color_space_b) :
b = cv2.COLOR_BGR2HSV
elif color_space_a == 'HSV' and color_space_b == 'BGR' :
b = cv2.COLOR_HSV2BGR
elif color_space_a == 'RGB' and color_space_b == 'RGB' :
b = None
elif color_space_a == 'BGR' and color_space_b == 'BGR' :
b = None
elif color_space_a == 'LAB' and color_space_b == 'LAB' :
b = None
elif color_space_a == 'HSV' and color_space_b == 'HSB' :
b = None
if b is not None :
cvt = cv2.cvtColor(pixel, b)
else :
cvt = pixel
cvt = cv2.cvtColor(pixel, b)
return cvt[0, 0]
def closest_color (colors, color):

View File

@ -10,22 +10,31 @@ class ComparisonComparison:
blue = [240, 0, 20]
comp_colors = [red, green, blue]
pallete = PalleteSchema('./palletes/printed_pallete.json')
colors = self.get_colors(pallete.colors)
for cc in comp_colors :
ccbgr = convert_color(cc, 'RGB', 'HSV')
closest = closest_color(colors, ccbgr)
ccbgr = convert_color(cc, 'RGB', 'BGR')
#print(f'{convert_color(closest,"BGR","RGB")} for {convert_color(ccbgr,"BGR","RGB")}')
original = create_colored_image(100, 100, convert_color(closest, 'HSV', 'BGR'))
chosen = create_colored_image(100, 100, ccbgr)
combined = np.hstack([original, chosen])
cv2.imshow("image", combined)
color_spaces = ['RGB', '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)
closest = closest_color(colors, cccompare)
ccbgr = convert_color(cc, 'RGB', 'BGR')
chosenbgr = convert_color(closest, space, 'BGR')
chosen = create_colored_image(100, 100, chosenbgr)
original = create_colored_image(100, 100, ccbgr)
combined = np.hstack([original, chosen])
show.append(combined)
show = np.vstack(show)
cv2.imshow("image", show)
cv2.waitKey(0)
def get_colors (self, pallete) :
def get_colors (self, pallete, space) :
colors = []
for color in pallete :
colors.append(convert_color(color['color'], 'BGR', 'HSV'))
colors.append(convert_color(color['color'], color['space'], space))
return colors
if __name__ == "__main__":