2023-10-21 12:26:58 +00:00
|
|
|
from sklearn.cluster import MiniBatchKMeans
|
|
|
|
import numpy as np
|
|
|
|
import argparse
|
|
|
|
import cv2
|
2023-10-25 00:44:09 +00:00
|
|
|
from common import convert_color, closest_color, create_colored_image, remove_from_list, list_match
|
2023-10-21 12:26:58 +00:00
|
|
|
|
|
|
|
class Posterize:
|
|
|
|
"""Posterize an image and then find nearest colors to use"""
|
|
|
|
colors = []
|
2023-10-25 00:44:09 +00:00
|
|
|
colors_dict = {}
|
2023-10-22 00:27:30 +00:00
|
|
|
original_colors = []
|
2023-10-25 00:44:09 +00:00
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
pallete = None
|
2023-10-25 00:44:09 +00:00
|
|
|
pallete_space = 'BGR'
|
|
|
|
|
|
|
|
comparison_space = 'BGR'
|
|
|
|
|
|
|
|
image = None
|
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
h = 0
|
|
|
|
w = 0
|
|
|
|
n_colors = 3
|
2023-10-25 00:44:09 +00:00
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
white = [255, 255, 255]
|
2023-10-21 17:51:38 +00:00
|
|
|
|
2023-10-25 00:44:09 +00:00
|
|
|
output = ''
|
|
|
|
|
|
|
|
def __init__ (self, image, pallete, n_colors, output) :
|
2023-10-21 17:51:38 +00:00
|
|
|
self.image = cv2.imread(image)
|
|
|
|
(self.h, self.w) = self.image.shape[:2]
|
|
|
|
self.pallete = pallete
|
2023-10-22 00:27:30 +00:00
|
|
|
self.n_colors = n_colors + 1
|
2023-10-25 00:44:09 +00:00
|
|
|
self.output = output
|
2023-10-22 00:27:30 +00:00
|
|
|
|
2023-10-25 00:44:09 +00:00
|
|
|
self.flatten_pallete()
|
2023-10-22 00:27:30 +00:00
|
|
|
self.posterize()
|
|
|
|
self.determine_colors()
|
2023-10-21 17:51:38 +00:00
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
def posterize (self):
|
2023-10-21 17:51:38 +00:00
|
|
|
lab = cv2.cvtColor(self.image, cv2.COLOR_BGR2LAB)
|
|
|
|
feature = lab.reshape((self.h * self.w, 3))
|
|
|
|
|
|
|
|
clusters = MiniBatchKMeans(n_clusters = self.n_colors, n_init = 'auto')
|
|
|
|
|
|
|
|
labels = clusters.fit_predict(feature)
|
|
|
|
quant = clusters.cluster_centers_.astype('uint8')[labels]
|
2023-10-21 12:26:58 +00:00
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
rquant = quant.reshape((self.h, self.w, 3))
|
|
|
|
rfeature = feature.reshape((self.h, self.w, 3))
|
|
|
|
|
|
|
|
bgrquant = cv2.cvtColor(rquant, cv2.COLOR_LAB2BGR)
|
2023-10-22 00:27:30 +00:00
|
|
|
#bgrfeature = cv2.cvtColor(rfeature, cv2.COLOR_LAB2BGR)
|
|
|
|
|
|
|
|
self.image = bgrquant
|
|
|
|
|
2023-10-21 17:51:38 +00:00
|
|
|
|
2023-10-25 00:44:09 +00:00
|
|
|
cv2.imshow('image', bgrquant)
|
2023-10-21 17:51:38 +00:00
|
|
|
cv2.waitKey(0)
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
def determine_colors (self):
|
|
|
|
reshaped = self.image.reshape(-1, self.image.shape[2])
|
|
|
|
self.original_colors = np.unique(reshaped, axis=0)
|
2023-10-25 00:44:09 +00:00
|
|
|
white = closest_color(self.original_colors, [255, 255, 255])
|
|
|
|
|
|
|
|
blank = create_colored_image(self.w, self.h, [255, 255, 255])
|
|
|
|
|
2023-10-22 00:27:30 +00:00
|
|
|
for i in range(self.n_colors) :
|
2023-10-25 00:44:09 +00:00
|
|
|
if list_match(self.original_colors[i], white) :
|
|
|
|
continue
|
2023-10-24 02:05:58 +00:00
|
|
|
mask = self.extract_color_mask(self.image, self.original_colors[i])
|
2023-10-25 00:44:09 +00:00
|
|
|
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()
|
2023-10-22 00:27:30 +00:00
|
|
|
|
2023-10-24 02:05:58 +00:00
|
|
|
def extract_color_mask (self, image, color):
|
2023-10-22 00:27:30 +00:00
|
|
|
mask = cv2.inRange(image, color, color)
|
|
|
|
return cv2.bitwise_not(mask)
|
2023-10-25 00:44:09 +00:00
|
|
|
|
|
|
|
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]}']
|