Improve color conversion and loop through different color spaces when making comparisons
This commit is contained in:
parent
1d23aac7d1
commit
aef8ab8267
12
py/common.py
12
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)
|
cvt = cv2.cvtColor(pixel, b)
|
||||||
|
else :
|
||||||
|
cvt = pixel
|
||||||
|
|
||||||
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 space in color_spaces :
|
||||||
|
print(f'Comparing in color space {space}')
|
||||||
|
colors = self.get_colors(pallete.colors, space)
|
||||||
|
show = []
|
||||||
for cc in comp_colors :
|
for cc in comp_colors :
|
||||||
ccbgr = convert_color(cc, 'RGB', 'HSV')
|
cccompare = convert_color(cc, 'RGB', space)
|
||||||
closest = closest_color(colors, ccbgr)
|
closest = closest_color(colors, cccompare)
|
||||||
|
|
||||||
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
||||||
#print(f'{convert_color(closest,"BGR","RGB")} for {convert_color(ccbgr,"BGR","RGB")}')
|
chosenbgr = convert_color(closest, space, 'BGR')
|
||||||
original = create_colored_image(100, 100, convert_color(closest, 'HSV', 'BGR'))
|
|
||||||
chosen = create_colored_image(100, 100, ccbgr)
|
chosen = create_colored_image(100, 100, chosenbgr)
|
||||||
|
original = create_colored_image(100, 100, ccbgr)
|
||||||
|
|
||||||
combined = np.hstack([original, chosen])
|
combined = np.hstack([original, chosen])
|
||||||
cv2.imshow("image", combined)
|
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