Checkpoint

- switch to pyclustering for kmedians
- allow choosing the same palette as previous line, with a multiplicative penalty to distance in case it's much better
- iterate kmedians multiple times and choose the best, since it's only a local optimum
This commit is contained in:
kris 2021-11-15 09:19:44 +00:00
parent 643e50349e
commit b363d60754
2 changed files with 66 additions and 48 deletions

View File

@ -11,6 +11,8 @@ import colour
from PIL import Image
import numpy as np
from pyclustering.cluster.kmedians import kmedians
from pyclustering.cluster.kmeans import kmeans
from pyclustering.utils.metric import distance_metric, type_metric
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
import dither as dither_pyx
@ -25,7 +27,7 @@ import screen as screen_py
# - support HGR
def cluster_palette(image: Image):
line_to_palette = {}
# line_to_palette = {}
# shuffle_lines = liprint(st(range(200))
# random.shuffle(shuffle_lines)
@ -40,13 +42,13 @@ def cluster_palette(image: Image):
# else:
# line_to_palette[line] = np.clip(int(line / (200 / 16)) + 2, 0, 15)
for line in range(200):
if line % 3 == 0:
line_to_palette[line] = int(line / (200 / 16))
elif line % 3 == 1:
line_to_palette[line] = np.clip(int(line / (200 / 16)) + 1, 0, 15)
else:
line_to_palette[line] = np.clip(int(line / (200 / 16)) + 2, 0, 15)
# for line in range(200):
# if line % 3 == 0:
# line_to_palette[line] = int(line / (200 / 16))
# elif line % 3 == 1:
# line_to_palette[line] = np.clip(int(line / (200 / 16)) + 1, 0, 15)
# else:
# line_to_palette[line] = np.clip(int(line / (200 / 16)) + 2, 0, 15)
colours_rgb = np.asarray(image).reshape((-1, 3))
with colour.utilities.suppress_warnings(colour_usage_warnings=True):
@ -55,6 +57,7 @@ def cluster_palette(image: Image):
palettes_rgb = np.empty((16, 16, 3), dtype=np.float32)
palettes_cam = np.empty((16, 16, 3), dtype=np.float32)
for palette_idx in range(16):
print("Fitting palette %d" % palette_idx)
p_lower = max(palette_idx - 2, 0)
p_upper = min(palette_idx + 2, 16)
palette_pixels = colours_cam[
@ -71,12 +74,23 @@ def cluster_palette(image: Image):
# 16, palette_pixels, fixed_centroids=fixed_centroids,
# tolerance=1e-6)
initial_centers = kmeans_plusplus_initializer(
palette_pixels, 16).initialize()
kmedians_instance = kmedians(palette_pixels, initial_centers)
kmedians_instance.process()
best_wce = 1e9
best_medians = None
for i in range(500):
# print(i)
initial_centers = kmeans_plusplus_initializer(
palette_pixels, 16).initialize()
kmedians_instance = kmedians(
palette_pixels, initial_centers, tolerance=0.1, itermax=100,
metric=distance_metric(type_metric.MANHATTAN))
kmedians_instance.process()
if kmedians_instance.get_total_wce() < best_wce:
best_wce = kmedians_instance.get_total_wce()
print(i, best_wce)
best_medians = kmedians_instance
print("Best %f" % best_wce)
palettes_cam[palette_idx, :, :] = np.array(
kmedians_instance.get_medians()).astype(np.float32)
best_medians.get_medians()).astype(np.float32)
# palette_colours = collections.defaultdict(list)
# for line in range(200):
@ -122,7 +136,7 @@ def cluster_palette(image: Image):
# line_pixels, palettes_cam, best_palette)
# line_to_palette[line] = best_palette
# print(line, line_to_palette[line])
return palettes_cam, palettes_rgb, line_to_palette
return palettes_cam, palettes_rgb
def main():
@ -185,7 +199,7 @@ def main():
gamma=args.gamma_correct, srgb_output=True)).astype(
np.float32) / 255
palettes_cam, palettes_rgb, line_to_palette = cluster_palette(rgb)
palettes_cam, palettes_rgb = cluster_palette(rgb)
# print(palette_rgb)
# screen.set_palette(0, (image_py.linear_to_srgb_array(palette_rgb) *
# 15).astype(np.uint8))
@ -193,36 +207,37 @@ def main():
screen.set_palette(i, (np.round(palettes_rgb[i, :, :] * 15)).astype(
np.uint8))
output_4bit, line_to_palette = dither_pyx.dither_shr(
rgb, palettes_cam, palettes_rgb, rgb_to_cam16)
screen.set_pixels(output_4bit)
output_rgb = np.zeros((200, 320, 3), dtype=np.uint8)
for i in range(200):
screen.line_palette[i] = line_to_palette[i]
output_rgb[i, :, :] = (
palettes_rgb[line_to_palette[i]][
output_4bit[i, :]] * 255).astype(np.uint8)
output_srgb = image_py.linear_to_srgb(output_rgb).astype(np.uint8)
for penalty in [1,2,3,4,5,6,7,8,9,10,1e9]:
output_4bit, line_to_palette = dither_pyx.dither_shr(
rgb, palettes_cam, palettes_rgb, rgb_to_cam16, float(penalty))
screen.set_pixels(output_4bit)
output_rgb = np.zeros((200, 320, 3), dtype=np.uint8)
for i in range(200):
screen.line_palette[i] = line_to_palette[i]
output_rgb[i, :, :] = (
palettes_rgb[line_to_palette[i]][
output_4bit[i, :]] * 255).astype(np.uint8)
output_srgb = image_py.linear_to_srgb(output_rgb).astype(np.uint8)
# dither = dither_pattern.PATTERNS[args.dither]()
# bitmap = dither_pyx.dither_image(
# screen, rgb, dither, args.lookahead, args.verbose, rgb_to_cam16)
# dither = dither_pattern.PATTERNS[args.dither]()
# bitmap = dither_pyx.dither_image(
# screen, rgb, dither, args.lookahead, args.verbose, rgb_to_cam16)
# Show output image by rendering in target palette
# output_palette_name = args.show_palette or args.palette
# output_palette = palette_py.PALETTES[output_palette_name]()
# output_screen = screen_py.DHGRScreen(output_palette)
# if output_palette_name == "ntsc":
# output_srgb = output_screen.bitmap_to_image_ntsc(bitmap)
# else:
# output_srgb = image_py.linear_to_srgb(
# output_screen.bitmap_to_image_rgb(bitmap)).astype(np.uint8)
out_image = image_py.resize(
Image.fromarray(output_srgb), screen.X_RES, screen.Y_RES,
srgb_output=False) # XXX true
# Show output image by rendering in target palette
# output_palette_name = args.show_palette or args.palette
# output_palette = palette_py.PALETTES[output_palette_name]()
# output_screen = screen_py.DHGRScreen(output_palette)
# if output_palette_name == "ntsc":
# output_srgb = output_screen.bitmap_to_image_ntsc(bitmap)
# else:
# output_srgb = image_py.linear_to_srgb(
# output_screen.bitmap_to_image_rgb(bitmap)).astype(np.uint8)
out_image = image_py.resize(
Image.fromarray(output_srgb), screen.X_RES, screen.Y_RES,
srgb_output=False) # XXX true
if args.show_output:
out_image.show()
if args.show_output:
out_image.show()
# Save Double hi-res image
outfile = os.path.join(os.path.splitext(args.output)[0] + "-preview.png")

View File

@ -339,7 +339,7 @@ import colour
@cython.boundscheck(False)
@cython.wraparound(False)
def dither_shr(float[:, :, ::1] working_image, float[:, :, ::1] palettes_cam, float[:, :, ::1] palettes_rgb, float[:,::1] rgb_to_cam16ucs):
def dither_shr(float[:, :, ::1] working_image, float[:, :, ::1] palettes_cam, float[:, :, ::1] palettes_rgb, float[:,::1] rgb_to_cam16ucs, float penalty):
cdef int y, x, idx, best_colour_idx, best_palette
cdef float best_distance, distance
cdef float[::1] best_colour_rgb, pixel_cam, colour_rgb, colour_cam
@ -354,13 +354,13 @@ def dither_shr(float[:, :, ::1] working_image, float[:, :, ::1] palettes_cam, fl
cdef int[::1] line_to_palette = np.zeros(200, dtype=np.int32)
best_palette = 15
for y in range(200):
print(y)
# print(y)
for x in range(320):
colour_cam = convert_rgb_to_cam16ucs(
rgb_to_cam16ucs, working_image[y,x,0], working_image[y,x,1], working_image[y,x,2])
line_cam[x, :] = colour_cam
best_palette = best_palette_for_line(line_cam, palettes_cam, <int>(y * 16 / 200), best_palette)
best_palette = best_palette_for_line(line_cam, palettes_cam, <int>(y * 16 / 200), best_palette, penalty)
# print("-->", best_palette)
palette_rgb = palettes_rgb[best_palette, :, :]
line_to_palette[y] = best_palette
@ -509,7 +509,7 @@ def k_means_with_fixed_centroids(
@cython.boundscheck(False)
@cython.wraparound(False)
cdef int best_palette_for_line(float [:, ::1] line_cam, float[:, :, ::1] palettes_cam, int base_palette_idx, int last_palette_idx) nogil:
cdef int best_palette_for_line(float [:, ::1] line_cam, float[:, :, ::1] palettes_cam, int base_palette_idx, int last_palette_idx, float last_penalty) nogil:
cdef int palette_idx, best_palette_idx, palette_entry_idx, pixel_idx
cdef float best_total_dist, total_dist, best_pixel_dist, pixel_dist
cdef float[:, ::1] palette_cam
@ -517,20 +517,23 @@ cdef int best_palette_for_line(float [:, ::1] line_cam, float[:, :, ::1] palette
best_total_dist = 1e9
best_palette_idx = -1
cdef float penalty
cdef int line_size = line_cam.shape[0]
for palette_idx in range(16):
palette_cam = palettes_cam[palette_idx, :, :]
if palette_idx < (base_palette_idx - 1) or palette_idx > (base_palette_idx + 1):
continue
if palette_idx == last_palette_idx:
continue
penalty = last_penalty
else:
penalty = 1.0
total_dist = 0
best_pixel_dist = 1e9
for pixel_idx in range(line_size):
pixel_cam = line_cam[pixel_idx]
for palette_entry_idx in range(16):
palette_entry = palette_cam[palette_entry_idx, :]
pixel_dist = colour_distance_squared(pixel_cam, palette_entry)
pixel_dist = colour_distance_squared(pixel_cam, palette_entry) * penalty
if pixel_dist < best_pixel_dist:
best_pixel_dist = pixel_dist
total_dist += best_pixel_dist