Improve color conversion and loop through different color spaces when making comparisons
This commit is contained in:
parent
1d23aac7d1
commit
aef8ab8267
14
py/common.py
14
py/common.py
|
@ -35,8 +35,20 @@ def convert_color (color, color_space_a, color_space_b) :
|
||||||
b = cv2.COLOR_BGR2HSV
|
b = cv2.COLOR_BGR2HSV
|
||||||
elif color_space_a == 'HSV' and color_space_b == 'BGR' :
|
elif color_space_a == 'HSV' and color_space_b == 'BGR' :
|
||||||
b = cv2.COLOR_HSV2BGR
|
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]
|
return cvt[0, 0]
|
||||||
|
|
||||||
def closest_color (colors, color):
|
def closest_color (colors, color):
|
||||||
|
|
|
@ -10,22 +10,31 @@ class ComparisonComparison:
|
||||||
blue = [240, 0, 20]
|
blue = [240, 0, 20]
|
||||||
comp_colors = [red, green, blue]
|
comp_colors = [red, green, blue]
|
||||||
pallete = PalleteSchema('./palletes/printed_pallete.json')
|
pallete = PalleteSchema('./palletes/printed_pallete.json')
|
||||||
colors = self.get_colors(pallete.colors)
|
color_spaces = ['RGB', 'LAB', 'HSV']
|
||||||
for cc in comp_colors :
|
for space in color_spaces :
|
||||||
ccbgr = convert_color(cc, 'RGB', 'HSV')
|
print(f'Comparing in color space {space}')
|
||||||
closest = closest_color(colors, ccbgr)
|
colors = self.get_colors(pallete.colors, space)
|
||||||
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
show = []
|
||||||
#print(f'{convert_color(closest,"BGR","RGB")} for {convert_color(ccbgr,"BGR","RGB")}')
|
for cc in comp_colors :
|
||||||
original = create_colored_image(100, 100, convert_color(closest, 'HSV', 'BGR'))
|
cccompare = convert_color(cc, 'RGB', space)
|
||||||
chosen = create_colored_image(100, 100, ccbgr)
|
closest = closest_color(colors, cccompare)
|
||||||
combined = np.hstack([original, chosen])
|
|
||||||
cv2.imshow("image", combined)
|
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)
|
cv2.waitKey(0)
|
||||||
|
|
||||||
def get_colors (self, pallete) :
|
def get_colors (self, pallete, space) :
|
||||||
colors = []
|
colors = []
|
||||||
for color in pallete :
|
for color in pallete :
|
||||||
colors.append(convert_color(color['color'], 'BGR', 'HSV'))
|
colors.append(convert_color(color['color'], color['space'], space))
|
||||||
return colors
|
return colors
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue