mirror of
https://github.com/KrisKennaway/ii-pix.git
synced 2025-02-25 05:28:58 +00:00
Use pyclustering for kmedians instead of hand-rolled
Optimize cython code
This commit is contained in:
parent
52af982159
commit
0596aefe0b
80
convert.py
80
convert.py
@ -10,7 +10,8 @@ import random
|
||||
import colour
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
from sklearn.cluster import KMeans
|
||||
from pyclustering.cluster.kmedians import kmedians
|
||||
from pyclustering.cluster.center_initializer import kmeans_plusplus_initializer
|
||||
|
||||
import dither as dither_pyx
|
||||
import dither_pattern
|
||||
@ -26,9 +27,9 @@ import screen as screen_py
|
||||
def cluster_palette(image: Image):
|
||||
line_to_palette = {}
|
||||
|
||||
#shuffle_lines = liprint(st(range(200))
|
||||
#random.shuffle(shuffle_lines)
|
||||
#for idx, line in enumerate(shuffle_lines):
|
||||
# shuffle_lines = liprint(st(range(200))
|
||||
# random.shuffle(shuffle_lines)
|
||||
# for idx, line in enumerate(shuffle_lines):
|
||||
# line_to_palette[line] = idx % 16
|
||||
|
||||
# for line in range(200):
|
||||
@ -54,51 +55,60 @@ def cluster_palette(image: Image):
|
||||
palettes_rgb = {}
|
||||
palettes_cam = {}
|
||||
for palette_idx in range(16):
|
||||
p_lower = max(palette_idx-2, 0)
|
||||
p_upper = min(palette_idx+2, 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, :]
|
||||
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
|
||||
# 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)
|
||||
# palettes_cam[palette_idx] = dither_pyx.k_means_with_fixed_centroids(
|
||||
# 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()
|
||||
palettes_cam[palette_idx] = np.array(
|
||||
kmedians_instance.get_medians()).astype(np.float32)
|
||||
|
||||
# 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 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
|
||||
# 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_
|
||||
# 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(palettes_cam[palette_idx], "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)
|
||||
|
20
dither.pyx
20
dither.pyx
@ -337,10 +337,10 @@ def dither_image(
|
||||
|
||||
import colour
|
||||
|
||||
@cython.boundscheck(True)
|
||||
@cython.boundscheck(False)
|
||||
@cython.wraparound(False)
|
||||
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 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
|
||||
cdef float quant_error
|
||||
@ -362,8 +362,8 @@ def dither_shr(float[:, :, ::1] working_image, object palettes_cam, object palet
|
||||
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)
|
||||
best_palette = best_palette_for_line(line_cam, palettes_cam, <int>(y * 16 / 200), best_palette)
|
||||
# print("-->", best_palette)
|
||||
palette_rgb = palettes_rgb[best_palette]
|
||||
line_to_palette[y] = best_palette
|
||||
|
||||
@ -373,7 +373,8 @@ def dither_shr(float[:, :, ::1] working_image, object palettes_cam, object palet
|
||||
|
||||
best_distance = 1e9
|
||||
best_colour_idx = -1
|
||||
for idx, colour_rgb in enumerate(palette_rgb):
|
||||
for idx in range(16):
|
||||
colour_rgb = palette_rgb[idx, :]
|
||||
colour_cam = convert_rgb_to_cam16ucs(rgb_to_cam16ucs, colour_rgb[0], colour_rgb[1], colour_rgb[2])
|
||||
distance = colour_distance_squared(pixel_cam, colour_cam)
|
||||
if distance < best_distance:
|
||||
@ -383,7 +384,6 @@ def dither_shr(float[:, :, ::1] working_image, object palettes_cam, object palet
|
||||
output_4bit[y, x] = best_colour_idx
|
||||
|
||||
for i in range(3):
|
||||
# output_rgb[y,x,i] = <int>(best_colour_rgb[i] * 255)
|
||||
quant_error = working_image[y, x, i] - best_colour_rgb[i]
|
||||
|
||||
# Floyd-Steinberg dither
|
||||
@ -451,12 +451,12 @@ def dither_shr(float[:, :, ::1] working_image, object palettes_cam, object palet
|
||||
# working_image[y + 2, x + 2, i] + quant_error * (1 / 48),
|
||||
# 0, 1)
|
||||
|
||||
return np.array(output_4bit, dtype=np.uint8), line_to_palette #, np.array(output_rgb, dtype=np.uint8)
|
||||
return np.array(output_4bit, dtype=np.uint8), line_to_palette
|
||||
|
||||
import collections
|
||||
import random
|
||||
|
||||
@cython.boundscheck(True)
|
||||
@cython.boundscheck(False)
|
||||
@cython.wraparound(False)
|
||||
def k_means_with_fixed_centroids(
|
||||
int n_clusters, float[:, ::1] data, float[:, ::1] fixed_centroids = None,
|
||||
@ -509,9 +509,9 @@ 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.boundscheck(False)
|
||||
@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 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user