Spaces:
Running
on
A10G
Running
on
A10G
Commit
·
b65a245
1
Parent(s):
8350a42
Update sketch_helper.py
Browse files- sketch_helper.py +1 -15
sketch_helper.py
CHANGED
|
@@ -5,20 +5,6 @@ from skimage.color import rgb2lab
|
|
| 5 |
from skimage.color import lab2rgb
|
| 6 |
from sklearn.cluster import KMeans
|
| 7 |
|
| 8 |
-
def color_quantization(image, n_colors):
|
| 9 |
-
# Reshape the image into a 2D array of pixels
|
| 10 |
-
pixels = image.reshape(-1, 3)
|
| 11 |
-
|
| 12 |
-
# Fit k-means clustering algorithm to the pixel data
|
| 13 |
-
kmeans = KMeans(n_clusters=n_colors, random_state=0).fit(pixels)
|
| 14 |
-
|
| 15 |
-
# Replace each pixel with its nearest cluster center
|
| 16 |
-
labels = kmeans.predict(pixels)
|
| 17 |
-
colors = kmeans.cluster_centers_.astype(np.uint8)
|
| 18 |
-
quantized_image = colors[labels].reshape(image.shape)
|
| 19 |
-
|
| 20 |
-
return quantized_image
|
| 21 |
-
|
| 22 |
|
| 23 |
def get_high_freq_colors(image):
|
| 24 |
im = image.getcolors(maxcolors=1024*1024)
|
|
@@ -30,7 +16,7 @@ def get_high_freq_colors(image):
|
|
| 30 |
high_freq_colors = [c for c in sorted_colors if c[0] > max(2, mean_freq)] # Ignore colors that occur very few times (less than 2) or less than half the average frequency
|
| 31 |
return high_freq_colors
|
| 32 |
|
| 33 |
-
def
|
| 34 |
# Get color histogram
|
| 35 |
hist, _ = np.histogramdd(image.reshape(-1, 3), bins=(256, 256, 256), range=((0, 256), (0, 256), (0, 256)))
|
| 36 |
# Get most frequent colors
|
|
|
|
| 5 |
from skimage.color import lab2rgb
|
| 6 |
from sklearn.cluster import KMeans
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def get_high_freq_colors(image):
|
| 10 |
im = image.getcolors(maxcolors=1024*1024)
|
|
|
|
| 16 |
high_freq_colors = [c for c in sorted_colors if c[0] > max(2, mean_freq)] # Ignore colors that occur very few times (less than 2) or less than half the average frequency
|
| 17 |
return high_freq_colors
|
| 18 |
|
| 19 |
+
def color_quantization(image, n_colors):
|
| 20 |
# Get color histogram
|
| 21 |
hist, _ = np.histogramdd(image.reshape(-1, 3), bins=(256, 256, 256), range=((0, 256), (0, 256), (0, 256)))
|
| 22 |
# Get most frequent colors
|