From 5cab8542698c29562d43ebafd9ec6bd2687f6936 Mon Sep 17 00:00:00 2001 From: kris Date: Thu, 11 Nov 2021 16:10:03 +0000 Subject: [PATCH] Fit palettes from overlapping line ranges, and map line to palette when dithering with two limitations: - cannot choose the same palette as the previous line (this avoids banding) - must be within +/- 1 of the "base" palette for the line number This gives pretty good results! --- convert.py | 75 ++++++++++++++++++++++++++++++++++++------------------ dither.pyx | 52 ++++++++++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 28 deletions(-) diff --git a/convert.py b/convert.py index 12cc424..53f8daa 100644 --- a/convert.py +++ b/convert.py @@ -51,43 +51,68 @@ def cluster_palette(image: Image): with colour.utilities.suppress_warnings(colour_usage_warnings=True): colours_cam = colour.convert(colours_rgb, "RGB", "CAM16UCS").astype(np.float32) - palettes_rgb = {} - palette_colours = collections.defaultdict(list) - for line in range(200): - palette = line_to_palette[line] - palette_colours[palette].extend( - colours_cam[line * 320:(line + 1) * 320]) + palettes_cam = {} + for palette_idx in range(16): + p_lower = max(palette_idx-2, 0) + p_upper = min(palette_idx+2, 16) + palette_pixels = colours_cam[ + int(p_lower * (200/16)) * 320:int(p_upper * ( + 200/16)) * 320, :] + + # kmeans = KMeans(n_clusters=16, max_iter=10000) + # kmeans.fit_predict(palette_pixels) + # palettes_cam[palette_idx] = kmeans.cluster_centers_ + + fixed_centroids = None + # print(np.array(line_colours), fixed_centroids) + palettes_cam[palette_idx] = dither_pyx.k_means_with_fixed_centroids( + 16, palette_pixels, fixed_centroids=fixed_centroids, tolerance=1e-6) + + # palette_colours = collections.defaultdict(list) + # for line in range(200): + # palette = line_to_palette[line] + # palette_colours[palette].extend( + # colours_cam[line * 320:(line + 1) * 320]) # For each line grouping, find big palette entries with minimal total # distance - palette_cam = None - for palette_idx in range(16): - line_colours = palette_colours[palette_idx] - #if palette_idx < 15: - # line_colours += palette_colours[palette_idx + 1] - # if palette_idx < 14: - # line_colours += palette_colours[palette_idx + 2] - # if palette_idx > 0: - # fixed_centroids = palette_cam[:8, :] - # else: - fixed_centroids = None - # print(np.array(line_colours), fixed_centroids) - palette_cam = dither_pyx.k_means_with_fixed_centroids(16, np.array( - line_colours), fixed_centroids=fixed_centroids, tolerance=1e-6) + # palette_cam = None + # for palette_idx in range(16): + # line_colours = palette_colours[palette_idx] + # #if palette_idx < 15: + # # line_colours += palette_colours[palette_idx + 1] + # # if palette_idx < 14: + # # line_colours += palette_colours[palette_idx + 2] + # # if palette_idx > 0: + # # fixed_centroids = palette_cam[:8, :] + # # else: + # fixed_centroids = None + # # print(np.array(line_colours), fixed_centroids) + # palette_cam = dither_pyx.k_means_with_fixed_centroids(16, np.array( + # line_colours), fixed_centroids=fixed_centroids, tolerance=1e-6) #kmeans = KMeans(n_clusters=16, max_iter=10000) #kmeans.fit_predict(line_colours) #palette_cam = kmeans.cluster_centers_ with colour.utilities.suppress_warnings(colour_usage_warnings=True): - palette_rgb = colour.convert(palette_cam, "CAM16UCS", "RGB") + palette_rgb = colour.convert(palettes_cam[palette_idx], "CAM16UCS", "RGB") # SHR colour palette only uses 4-bit values palette_rgb = np.round(palette_rgb * 15) / 15 palettes_rgb[palette_idx] = palette_rgb.astype(np.float32) # print(palettes_rgb) - return palettes_rgb, line_to_palette + + # For each line, pick the palette with lowest total distance + # best_palette = 15 + # for line in range(200): + # line_pixels = colours_cam[line*320:(line+1)*320] + # best_palette = dither_pyx.best_palette_for_line( + # 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 def main(): @@ -150,15 +175,15 @@ def main(): gamma=args.gamma_correct, srgb_output=True)).astype( np.float32) / 255 - palettes_rgb, line_to_palette = cluster_palette(rgb) + palettes_cam, palettes_rgb, line_to_palette = cluster_palette(rgb) # print(palette_rgb) # screen.set_palette(0, (image_py.linear_to_srgb_array(palette_rgb) * # 15).astype(np.uint8)) for i, p in palettes_rgb.items(): screen.set_palette(i, (np.round(p * 15)).astype(np.uint8)) - output_4bit = dither_pyx.dither_shr(rgb, palettes_rgb, rgb_to_cam16, - line_to_palette) + 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): diff --git a/dither.pyx b/dither.pyx index ff165cf..31277df 100644 --- a/dither.pyx +++ b/dither.pyx @@ -339,7 +339,7 @@ import colour @cython.boundscheck(True) @cython.wraparound(False) -def dither_shr(float[:, :, ::1] working_image, object palettes_rgb, float[:,::1] rgb_to_cam16ucs, object line_to_palette): +def dither_shr(float[:, :, ::1] working_image, object palettes_cam, object palettes_rgb, float[:,::1] rgb_to_cam16ucs): cdef int y, x, idx, best_colour_idx cdef float best_distance, distance cdef float[::1] best_colour_rgb, pixel_cam, colour_rgb, colour_cam @@ -349,9 +349,24 @@ def dither_shr(float[:, :, ::1] working_image, object palettes_rgb, float[:,::1] cdef (unsigned char)[:, ::1] output_4bit = np.zeros((200, 320), dtype=np.uint8) # cdef (unsigned char)[:, :, ::1] output_rgb = np.zeros((200, 320, 3), dtype=np.uint8) + cdef float[:, ::1] line_cam = np.zeros((320, 3), dtype=np.float32) + + line_to_palette = {} + best_palette = 15 for y in range(200): print(y) - palette_rgb = palettes_rgb[line_to_palette[y]] + # palette_rgb = palettes_rgb[line_to_palette[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, y * 16 / 200, best_palette) + print("-->", best_palette) + palette_rgb = palettes_rgb[best_palette] + line_to_palette[y] = best_palette + for x in range(320): pixel_cam = convert_rgb_to_cam16ucs( rgb_to_cam16ucs, working_image[y, x, 0], working_image[y, x, 1], working_image[y, x, 2]) @@ -436,11 +451,13 @@ def dither_shr(float[:, :, ::1] working_image, object palettes_rgb, float[:,::1] # working_image[y + 2, x + 2, i] + quant_error * (1 / 48), # 0, 1) - return np.array(output_4bit, dtype=np.uint8) #, np.array(output_rgb, dtype=np.uint8) + return np.array(output_4bit, dtype=np.uint8), line_to_palette #, np.array(output_rgb, dtype=np.uint8) import collections import random +@cython.boundscheck(True) +@cython.wraparound(False) def k_means_with_fixed_centroids( int n_clusters, float[:, ::1] data, float[:, ::1] fixed_centroids = None, int iterations = 10000, float tolerance = 1e-3): @@ -492,3 +509,32 @@ def k_means_with_fixed_centroids( print(weighted_centroids) return np.array([c for w, c in sorted(weighted_centroids, reverse=True)], dtype=np.float32) +@cython.boundscheck(True) +@cython.wraparound(False) +def best_palette_for_line(float [:, ::1] line_cam, object palettes_cam, int base_palette_idx, int last_palette_idx): + cdef int palette_idx, best_palette_idx + cdef float best_total_dist, total_dist, best_pixel_dist, pixel_dist + cdef float[:, ::1] palette_cam + cdef float[::1] pixel_cam, palette_entry + + best_total_dist = 1e9 + best_palette_idx = -1 + for palette_idx, palette_cam in palettes_cam.items(): + if palette_idx < (base_palette_idx - 1) or palette_idx > (base_palette_idx + 1): + continue + if palette_idx == last_palette_idx: + continue + total_dist = 0 + best_pixel_dist = 1e9 + for pixel_cam in line_cam: + for palette_entry in palette_cam: + pixel_dist = colour_distance_squared(pixel_cam, palette_entry) + if pixel_dist < best_pixel_dist: + best_pixel_dist = pixel_dist + total_dist += best_pixel_dist + # print(total_dist) + if total_dist < best_total_dist: + best_total_dist = total_dist + best_palette_idx = palette_idx + return best_palette_idx +