Compare commits
2 Commits
aef8ab8267
...
654726840d
Author | SHA1 | Date |
---|---|---|
Matt McWilliams | 654726840d | |
Matt McWilliams | 4d94666449 |
|
@ -1,2 +1,3 @@
|
|||
env
|
||||
__pycache__
|
||||
*.png
|
||||
|
|
22
py/common.py
22
py/common.py
|
@ -3,14 +3,17 @@ 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' :
|
||||
|
@ -41,7 +44,7 @@ def convert_color (color, color_space_a, color_space_b) :
|
|||
b = None
|
||||
elif color_space_a == 'LAB' and color_space_b == 'LAB' :
|
||||
b = None
|
||||
elif color_space_a == 'HSV' and color_space_b == 'HSB' :
|
||||
elif color_space_a == 'HSV' and color_space_b == 'HSV' :
|
||||
b = 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))
|
||||
index_of_smallest = np.where(distances == np.amin(distances))
|
||||
smallest_distance = colors[index_of_smallest]
|
||||
return smallest_distance
|
||||
return smallest_distance[0]
|
||||
|
||||
def create_colored_image (width, height, bgr_color):
|
||||
image = np.zeros((height, width, 3), np.uint8)
|
||||
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 numpy as np
|
||||
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:
|
||||
def __init__ (self) :
|
||||
|
||||
red = [0, 10, 200]
|
||||
green = [5, 250, 5]
|
||||
blue = [240, 0, 20]
|
||||
|
||||
comp_colors = [red, green, blue]
|
||||
|
||||
pallete = PalleteSchema('./palletes/printed_pallete.json')
|
||||
color_spaces = ['RGB', 'LAB', 'HSV']
|
||||
|
||||
color_spaces = ['RGB', 'BGR', '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 :
|
||||
cccompare = convert_color(cc, 'RGB', space)
|
||||
print(cccompare)
|
||||
closest = closest_color(colors, cccompare)
|
||||
print(closest)
|
||||
colors = remove_from_list(colors, closest)
|
||||
|
||||
ccbgr = convert_color(cc, 'RGB', 'BGR')
|
||||
chosenbgr = convert_color(closest, space, 'BGR')
|
||||
|
||||
chosen = create_colored_image(100, 100, chosenbgr)
|
||||
original = create_colored_image(100, 100, ccbgr)
|
||||
chosen = create_colored_image(100, 100, chosenbgr)
|
||||
|
||||
print(f'{ccbgr} => {chosenbgr}')
|
||||
|
||||
combined = np.hstack([original, chosen])
|
||||
show.append(combined)
|
||||
|
||||
show = np.vstack(show)
|
||||
cv2.imshow("image", show)
|
||||
cv2.waitKey(0)
|
||||
|
|
|
@ -2,25 +2,37 @@ from sklearn.cluster import MiniBatchKMeans
|
|||
import numpy as np
|
||||
import argparse
|
||||
import cv2
|
||||
from common import convert_color, closest_color
|
||||
from common import convert_color, closest_color, create_colored_image, remove_from_list, list_match
|
||||
|
||||
class Posterize:
|
||||
"""Posterize an image and then find nearest colors to use"""
|
||||
colors = []
|
||||
colors_dict = {}
|
||||
original_colors = []
|
||||
image = None
|
||||
|
||||
pallete = None
|
||||
pallete_space = 'BGR'
|
||||
|
||||
comparison_space = 'BGR'
|
||||
|
||||
image = None
|
||||
|
||||
h = 0
|
||||
w = 0
|
||||
n_colors = 3
|
||||
|
||||
white = [255, 255, 255]
|
||||
|
||||
def __init__ (self, image, pallete, n_colors) :
|
||||
output = ''
|
||||
|
||||
def __init__ (self, image, pallete, n_colors, output) :
|
||||
self.image = cv2.imread(image)
|
||||
(self.h, self.w) = self.image.shape[:2]
|
||||
self.pallete = pallete
|
||||
self.n_colors = n_colors + 1
|
||||
self.output = output
|
||||
|
||||
self.flatten_pallete()
|
||||
self.posterize()
|
||||
self.determine_colors()
|
||||
|
||||
|
@ -42,18 +54,41 @@ class Posterize:
|
|||
self.image = bgrquant
|
||||
|
||||
|
||||
cv2.imshow("image", bgrquant)
|
||||
cv2.imshow('image', bgrquant)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
def determine_colors (self):
|
||||
reshaped = self.image.reshape(-1, self.image.shape[2])
|
||||
self.original_colors = np.unique(reshaped, axis=0)
|
||||
#print(self.original_colors)
|
||||
white = closest_color(self.original_colors, [255, 255, 255])
|
||||
|
||||
blank = create_colored_image(self.w, self.h, [255, 255, 255])
|
||||
|
||||
for i in range(self.n_colors) :
|
||||
if list_match(self.original_colors[i], white) :
|
||||
continue
|
||||
mask = self.extract_color_mask(self.image, self.original_colors[i])
|
||||
cv2.imwrite(f'{i}.png', mask)
|
||||
closest = closest_color(self.colors, self.original_colors[i])
|
||||
self.colors = remove_from_list(self.colors, closest)
|
||||
name = self.match_color_name(closest)
|
||||
cv2.imwrite(f'{name}.png', mask)
|
||||
color_mat = create_colored_image(self.w, self.h, closest)
|
||||
blank = cv2.bitwise_and(color_mat, color_mat, mask = (255-mask))
|
||||
|
||||
cv2.imshow('image', blank)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
def extract_color_mask (self, image, color):
|
||||
mask = cv2.inRange(image, color, color)
|
||||
return cv2.bitwise_not(mask)
|
||||
|
||||
def flatten_pallete (self) :
|
||||
for color in self.pallete.colors:
|
||||
self.colors.append(color['color'])
|
||||
self.colors_dict[f'{color["color"][0]},{color["color"][1]},{color["color"][2]}'] = color['name']
|
||||
self.pallete_space = color['space']
|
||||
|
||||
def match_color_name (self, key) :
|
||||
return self.colors_dict[f'{key[0]},{key[1]},{key[2]}']
|
||||
|
|
|
@ -21,12 +21,14 @@ class Separator :
|
|||
else :
|
||||
print(f'File {args.input} does not exist')
|
||||
exit(1)
|
||||
|
||||
if isfile(args.pallete) :
|
||||
self.pallete = PalleteSchema(args.pallete)
|
||||
else :
|
||||
print(f'File {args.pallete} does not exist')
|
||||
exit(2)
|
||||
Posterize(self.input, self.pallete, args.colors)
|
||||
|
||||
Posterize(self.input, self.pallete, args.colors, args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__" :
|
||||
|
|
Loading…
Reference in New Issue