For some reason RGB and BGR comparison seem wrong.
This commit is contained in:
parent
aef8ab8267
commit
4d94666449
20
py/common.py
20
py/common.py
|
@ -3,14 +3,17 @@ import numpy as np
|
||||||
|
|
||||||
def convert_color (color, color_space_a, color_space_b) :
|
def convert_color (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)
|
||||||
|
|
||||||
if color_space_a == 'RGB' :
|
if color_space_a == 'RGB' :
|
||||||
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2RGB)
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2RGB)
|
||||||
elif color_space_a == 'LAB' :
|
elif color_space_a == 'LAB' :
|
||||||
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2LAB)
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2LAB)
|
||||||
elif color_space_a == 'HSV' :
|
elif color_space_a == 'HSV' :
|
||||||
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2HSV)
|
pixel = cv2.cvtColor(pixel, cv2.COLOR_BGR2HSV)
|
||||||
|
|
||||||
#default is BGR
|
#default is BGR
|
||||||
pixel[:] = color
|
pixel[:] = color
|
||||||
|
|
||||||
if color_space_a == 'RGB' and color_space_b == 'BGR' :
|
if color_space_a == 'RGB' and color_space_b == 'BGR' :
|
||||||
b = cv2.COLOR_RGB2BGR
|
b = cv2.COLOR_RGB2BGR
|
||||||
elif color_space_a == 'BGR' and color_space_b == 'RGB' :
|
elif color_space_a == 'BGR' and color_space_b == 'RGB' :
|
||||||
|
@ -41,7 +44,7 @@ def convert_color (color, color_space_a, color_space_b) :
|
||||||
b = None
|
b = None
|
||||||
elif color_space_a == 'LAB' and color_space_b == 'LAB' :
|
elif color_space_a == 'LAB' and color_space_b == 'LAB' :
|
||||||
b = None
|
b = None
|
||||||
elif color_space_a == 'HSV' and color_space_b == 'HSB' :
|
elif color_space_a == 'HSV' and color_space_b == 'HSV' :
|
||||||
b = None
|
b = None
|
||||||
|
|
||||||
if b is not None :
|
if b is not None :
|
||||||
|
@ -57,9 +60,22 @@ def closest_color (colors, color):
|
||||||
distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
|
distances = np.sqrt(np.sum((colors - color) ** 2, axis=1))
|
||||||
index_of_smallest = np.where(distances == np.amin(distances))
|
index_of_smallest = np.where(distances == np.amin(distances))
|
||||||
smallest_distance = colors[index_of_smallest]
|
smallest_distance = colors[index_of_smallest]
|
||||||
return smallest_distance
|
return smallest_distance[0]
|
||||||
|
|
||||||
def create_colored_image (width, height, bgr_color):
|
def create_colored_image (width, height, bgr_color):
|
||||||
image = np.zeros((height, width, 3), np.uint8)
|
image = np.zeros((height, width, 3), np.uint8)
|
||||||
image[:] = bgr_color
|
image[:] = bgr_color
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
def remove_from_list (l, item) :
|
||||||
|
new_array = []
|
||||||
|
for i in l :
|
||||||
|
if not list_match(i, item) :
|
||||||
|
new_array.append(i)
|
||||||
|
return new_array
|
||||||
|
|
||||||
|
def list_match (a, b) :
|
||||||
|
for i in range(len(a)) :
|
||||||
|
if a[i] != b[i] :
|
||||||
|
return False
|
||||||
|
return True
|
|
@ -1,32 +1,44 @@
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pallete_schema import PalleteSchema
|
from pallete_schema import PalleteSchema
|
||||||
from common import convert_color, closest_color, create_colored_image
|
from common import convert_color, closest_color, create_colored_image, remove_from_list
|
||||||
|
|
||||||
class ComparisonComparison:
|
class ComparisonComparison:
|
||||||
def __init__ (self) :
|
def __init__ (self) :
|
||||||
|
|
||||||
red = [0, 10, 200]
|
red = [0, 10, 200]
|
||||||
green = [5, 250, 5]
|
green = [5, 250, 5]
|
||||||
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')
|
||||||
color_spaces = ['RGB', 'LAB', 'HSV']
|
|
||||||
|
color_spaces = ['RGB', 'BGR', 'LAB', 'HSV']
|
||||||
for space in color_spaces :
|
for space in color_spaces :
|
||||||
print(f'Comparing in color space {space}')
|
print(f'Comparing in color space {space}')
|
||||||
|
|
||||||
colors = self.get_colors(pallete.colors, space)
|
colors = self.get_colors(pallete.colors, space)
|
||||||
show = []
|
show = []
|
||||||
|
|
||||||
for cc in comp_colors :
|
for cc in comp_colors :
|
||||||
cccompare = convert_color(cc, 'RGB', space)
|
cccompare = convert_color(cc, 'RGB', space)
|
||||||
|
print(cccompare)
|
||||||
closest = closest_color(colors, cccompare)
|
closest = closest_color(colors, cccompare)
|
||||||
|
print(closest)
|
||||||
|
colors = remove_from_list(colors, closest)
|
||||||
|
|
||||||
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
||||||
chosenbgr = convert_color(closest, space, 'BGR')
|
chosenbgr = convert_color(closest, space, 'BGR')
|
||||||
|
|
||||||
chosen = create_colored_image(100, 100, chosenbgr)
|
|
||||||
original = create_colored_image(100, 100, ccbgr)
|
original = create_colored_image(100, 100, ccbgr)
|
||||||
|
chosen = create_colored_image(100, 100, chosenbgr)
|
||||||
|
|
||||||
|
print(f'{ccbgr} => {chosenbgr}')
|
||||||
|
|
||||||
combined = np.hstack([original, chosen])
|
combined = np.hstack([original, chosen])
|
||||||
show.append(combined)
|
show.append(combined)
|
||||||
|
|
||||||
show = np.vstack(show)
|
show = np.vstack(show)
|
||||||
cv2.imshow("image", show)
|
cv2.imshow("image", show)
|
||||||
cv2.waitKey(0)
|
cv2.waitKey(0)
|
||||||
|
|
Loading…
Reference in New Issue