Perform comparison in different color spaces. Move common utilty functions to common lib
This commit is contained in:
parent
d579e83762
commit
1d23aac7d1
|
@ -0,0 +1,53 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
|
||||
def convert_color (color, color_space_a, color_space_b) :
|
||||
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
|
||||
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
|
||||
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_BGR2HSV
|
||||
elif color_space_a == 'HSV' and color_space_b == 'BGR' :
|
||||
b = cv2.COLOR_HSV2BGR
|
||||
|
||||
cvt = cv2.cvtColor(pixel, b)
|
||||
return cvt[0, 0]
|
||||
|
||||
def closest_color (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
|
||||
|
||||
def create_colored_image (width, height, bgr_color):
|
||||
image = np.zeros((height, width, 3), np.uint8)
|
||||
image[:] = bgr_color
|
||||
return image
|
Loading…
Reference in New Issue