Improve color conversion method. Allow for comparison_comparison to test all color spaces
This commit is contained in:
parent
e22bffbe25
commit
f0de99a148
|
@ -4,19 +4,68 @@ from pallete_schema import PalleteSchema
|
||||||
|
|
||||||
class ComparisonComparison:
|
class ComparisonComparison:
|
||||||
def __init__ (self) :
|
def __init__ (self) :
|
||||||
c = [0, 0, 255]
|
red = [0, 10, 200]
|
||||||
print(c)
|
green = [5, 250, 5]
|
||||||
cc = self.convert_color(c, cv2.COLOR_BGR2RGB, cv2.COLOR_RGB2LAB)
|
blue = [240, 0, 20]
|
||||||
print(cc)
|
comp_colors = [red, green, blue]
|
||||||
print(self.convert_color(cc, cv2.COLOR_BGR2LAB, cv2.COLOR_LAB2RGB))
|
pallete = PalleteSchema('./palletes/test_pallete.json')
|
||||||
|
colors = self.get_colors(pallete.colors)
|
||||||
|
for cc in comp_colors :
|
||||||
|
ccbgr = self.convert_color(cc, 'RGB', 'BGR')
|
||||||
|
closest = self.closest(colors, ccbgr)
|
||||||
|
print(f'{closest} for {ccbgr}')
|
||||||
|
|
||||||
|
def get_colors (self, pallete) :
|
||||||
|
colors = []
|
||||||
|
for color in pallete :
|
||||||
|
colors.append(color['color'])
|
||||||
|
return colors
|
||||||
|
|
||||||
def convert_color (self, color, color_space_a, color_space_b) :
|
def convert_color (self, color, color_space_a, color_space_b) :
|
||||||
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
|
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
|
||||||
pixel = cv2.cvtColor(pixel, color_space_a)
|
if color_space_a == 'RGB' :
|
||||||
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2RGB)
|
||||||
|
elif color_space_a == 'LAB' :
|
||||||
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2LAB)
|
||||||
|
elif color_space_a == 'HSV' :
|
||||||
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2HSV)
|
||||||
|
#default is BGR
|
||||||
pixel[:] = color
|
pixel[:] = color
|
||||||
cvt = cv2.cvtColor(pixel, color_space_b)
|
if color_space_a == 'RGB' and color_space_b == 'BGR' :
|
||||||
|
b = cv2.COLOR_RGB2BGR
|
||||||
|
elif color_space_a == 'BGR' and color_space_b == 'RGB' :
|
||||||
|
b = cv2.COLOR_BGR2RGB
|
||||||
|
elif color_space_a == 'RGB' and color_space_b == 'LAB' :
|
||||||
|
b = cv2.COLOR_RGB2LAB
|
||||||
|
elif color_space_a == 'LAB' and color_space_b == 'RGB' :
|
||||||
|
b = cv2.COLOR_LAB2RGB
|
||||||
|
elif color_space_a == 'BGR' and color_space_b == 'LAB' :
|
||||||
|
b = cv2.COLOR_BGR2LAB
|
||||||
|
elif color_space_a == 'LAB' and color_space_b == 'BGR' :
|
||||||
|
b = cv2.COLOR_LAB2BGR
|
||||||
|
elif color_space_a == 'HSV' and color_space_b == 'LAB' :
|
||||||
|
b = cv2.COLOR_HSV2LAB
|
||||||
|
elif color_space_a == 'LAB' and color_space_b == 'HSV' :
|
||||||
|
b = cv2.COLOR_LAB2HSV
|
||||||
|
elif color_space_a == 'RGB' and color_space_b == 'HSV' :
|
||||||
|
b = cv2.COLOR_RGB2HSV
|
||||||
|
elif color_space_a == 'HSV' and color_space_b == 'RGB' :
|
||||||
|
b = cv2.COLOR_HSV2RGB
|
||||||
|
elif color_space_a == 'BGR' and color_space_b == 'HSV' :
|
||||||
|
b = cv2.COLOR_BGRHSV
|
||||||
|
elif color_space_a == 'HSV' and color_space_b == 'BGR' :
|
||||||
|
b = cv2.COLOR_HSV2BGR
|
||||||
|
|
||||||
|
cvt = cv2.cvtColor(pixel, b)
|
||||||
return cvt[0, 0]
|
return cvt[0, 0]
|
||||||
|
|
||||||
|
def closest(self, colors, color):
|
||||||
|
colors = np.array(colors)
|
||||||
|
color = np.array(color)
|
||||||
|
distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
|
||||||
|
index_of_smallest = np.where(distances == np.amin(distances))
|
||||||
|
smallest_distance = colors[index_of_smallest]
|
||||||
|
return smallest_distance
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ComparisonComparison()
|
ComparisonComparison()
|
|
@ -21,8 +21,10 @@ class PalleteSchema :
|
||||||
def __init__ (self, file = None, obj = None):
|
def __init__ (self, file = None, obj = None):
|
||||||
if file is not None and obj is None:
|
if file is not None and obj is None:
|
||||||
self.parse_file(file)
|
self.parse_file(file)
|
||||||
else :
|
elif file is not None and obj is not None :
|
||||||
self.write(file, obj)
|
self.write(file, obj)
|
||||||
|
else :
|
||||||
|
print('Not sure what you\'re trying to do here')
|
||||||
|
|
||||||
def parse_file (self, file) :
|
def parse_file (self, file) :
|
||||||
with open(file) as f :
|
with open(file) as f :
|
||||||
|
|
|
@ -59,9 +59,40 @@ class Posterize:
|
||||||
|
|
||||||
def convert_color (self, color, color_space_a, color_space_b) :
|
def convert_color (self, color, color_space_a, color_space_b) :
|
||||||
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
|
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
|
||||||
pixel = cv2.cvtColor(pixel, color_space_a)
|
if color_space_a == 'RGB' :
|
||||||
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2RGB)
|
||||||
|
elif color_space_a == 'LAB' :
|
||||||
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2LAB)
|
||||||
|
elif color_space_a == 'HSV' :
|
||||||
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2HSV)
|
||||||
|
#default is BGR
|
||||||
pixel[:] = color
|
pixel[:] = color
|
||||||
cvt = cv2.cvtColor(pixel, color_space_b)
|
if color_space_a == 'RGB' and color_space_b == 'BGR' :
|
||||||
|
b = cv2.COLOR_RGB2BGR
|
||||||
|
elif color_space_a == 'BGR' and color_space_b == 'RGB' :
|
||||||
|
b = cv2.COLOR_BGR2RGB
|
||||||
|
elif color_space_a == 'RGB' and color_space_b == 'LAB' :
|
||||||
|
b = cv2.COLOR_RGB2LAB
|
||||||
|
elif color_space_a == 'LAB' and color_space_b == 'RGB' :
|
||||||
|
b = cv2.COLOR_LAB2RGB
|
||||||
|
elif color_space_a == 'BGR' and color_space_b == 'LAB' :
|
||||||
|
b = cv2.COLOR_BGR2LAB
|
||||||
|
elif color_space_a == 'LAB' and color_space_b == 'BGR' :
|
||||||
|
b = cv2.COLOR_LAB2BGR
|
||||||
|
elif color_space_a == 'HSV' and color_space_b == 'LAB' :
|
||||||
|
b = cv2.COLOR_HSV2LAB
|
||||||
|
elif color_space_a == 'LAB' and color_space_b == 'HSV' :
|
||||||
|
b = cv2.COLOR_LAB2HSV
|
||||||
|
elif color_space_a == 'RGB' and color_space_b == 'HSV' :
|
||||||
|
b = cv2.COLOR_RGB2HSV
|
||||||
|
elif color_space_a == 'HSV' and color_space_b == 'RGB' :
|
||||||
|
b = cv2.COLOR_HSV2RGB
|
||||||
|
elif color_space_a == 'BGR' and color_space_b == 'HSV' :
|
||||||
|
b = cv2.COLOR_BGRHSV
|
||||||
|
elif color_space_a == 'HSV' and color_space_b == 'BGR' :
|
||||||
|
b = cv2.COLOR_HSV2BGR
|
||||||
|
|
||||||
|
cvt = cv2.cvtColor(pixel, b)
|
||||||
return cvt[0, 0]
|
return cvt[0, 0]
|
||||||
|
|
||||||
def closest(self, colors, color):
|
def closest(self, colors, color):
|
||||||
|
|
Loading…
Reference in New Issue