Compare commits

..

No commits in common. "1d23aac7d1617daa1b2230d655510f11df8fd8fe" and "e22bffbe25fa7811de58d92f45fef4ccb9e40b47" have entirely different histories.

4 changed files with 28 additions and 79 deletions

View File

@ -1,53 +0,0 @@
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

View File

@ -1,32 +1,22 @@
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
class ComparisonComparison: class ComparisonComparison:
def __init__ (self) : def __init__ (self) :
red = [0, 10, 200] c = [0, 0, 255]
green = [5, 250, 5] print(c)
blue = [240, 0, 20] cc = self.convert_color(c, cv2.COLOR_BGR2RGB, cv2.COLOR_RGB2LAB)
comp_colors = [red, green, blue] print(cc)
pallete = PalleteSchema('./palletes/printed_pallete.json') print(self.convert_color(cc, cv2.COLOR_BGR2LAB, cv2.COLOR_LAB2RGB))
colors = self.get_colors(pallete.colors)
for cc in comp_colors :
ccbgr = convert_color(cc, 'RGB', 'HSV')
closest = closest_color(colors, ccbgr)
ccbgr = convert_color(cc, 'RGB', 'BGR')
#print(f'{convert_color(closest,"BGR","RGB")} for {convert_color(ccbgr,"BGR","RGB")}')
original = create_colored_image(100, 100, convert_color(closest, 'HSV', 'BGR'))
chosen = create_colored_image(100, 100, ccbgr)
combined = np.hstack([original, chosen])
cv2.imshow("image", combined)
cv2.waitKey(0)
def get_colors (self, pallete) :
colors = [] def convert_color (self, color, color_space_a, color_space_b) :
for color in pallete : pixel = np.zeros([1, 1, 3], dtype=np.uint8)
colors.append(convert_color(color['color'], 'BGR', 'HSV')) pixel = cv2.cvtColor(pixel, color_space_a)
return colors pixel[:] = color
cvt = cv2.cvtColor(pixel, color_space_b)
return cvt[0, 0]
if __name__ == "__main__": if __name__ == "__main__":
ComparisonComparison() ComparisonComparison()

View File

@ -21,10 +21,8 @@ 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)
elif file is not None and obj is not None :
self.write(file, obj)
else : else :
print('Not sure what you\'re trying to do here') self.write(file, obj)
def parse_file (self, file) : def parse_file (self, file) :
with open(file) as f : with open(file) as f :

View File

@ -2,7 +2,6 @@ from sklearn.cluster import MiniBatchKMeans
import numpy as np import numpy as np
import argparse import argparse
import cv2 import cv2
from common import convert_color, closest_color
class Posterize: class Posterize:
"""Posterize an image and then find nearest colors to use""" """Posterize an image and then find nearest colors to use"""
@ -57,3 +56,18 @@ class Posterize:
def extract_color_mask (self, image, color): def extract_color_mask (self, image, color):
mask = cv2.inRange(image, color, color) mask = cv2.inRange(image, color, color)
return cv2.bitwise_not(mask) return cv2.bitwise_not(mask)
def convert_color (self, color, color_space_a, color_space_b) :
pixel = np.zeros([1, 1, 3], dtype=np.uint8)
pixel = cv2.cvtColor(pixel, color_space_a)
pixel[:] = color
cvt = cv2.cvtColor(pixel, color_space_b)
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