multimodalart HF staff commited on
Commit
5fef8b2
1 Parent(s): b65a245

Update sketch_helper.py

Browse files
Files changed (1) hide show
  1. sketch_helper.py +34 -19
sketch_helper.py CHANGED
@@ -6,27 +6,42 @@ 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)
11
- sorted_colors = sorted(im, key=lambda x: x[0], reverse=True)
12
-
13
- freqs = [c[0] for c in sorted_colors]
14
- mean_freq = sum(freqs) / len(freqs)
15
 
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
23
- colors = np.argwhere(hist > 0)
24
- colors = colors[np.argsort(hist[colors[:, 0], colors[:, 1], colors[:, 2]])[::-1]]
25
- colors = colors[:n_colors]
26
- # Replace each pixel with the closest color
27
- dists = np.sum((image.reshape(-1, 1, 3) - colors.reshape(1, -1, 3))**2, axis=2)
28
- labels = np.argmin(dists, axis=1)
29
- return colors[labels].reshape((image.shape[0], image.shape[1], 3)).astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  def create_binary_matrix(img_arr, target_color):
32
  # Create mask of pixels with target color
 
6
  from sklearn.cluster import KMeans
7
 
8
 
9
+ def count_high_freq_colors(image):
10
+ im = image.getcolors(maxcolors=1024*1024)
11
+ sorted_colors = sorted(im, key=lambda x: x[0], reverse=True)
 
 
 
12
 
13
+ freqs = [c[0] for c in sorted_colors]
14
+ mean_freq = sum(freqs) / len(freqs)
15
 
16
+ high_freq_colors = [c for c in sorted_colors if c[0] > max(2, mean_freq*1.25)]
17
+ return high_freq_colors
18
+
19
+ def get_high_freq_colors(image, similarity_threshold=30):
20
+ image_copy = image.copy()
21
+ high_freq_colors = count_high_freq_colors(image)
22
+ # Check for similar colors and replace the lower frequency color with the higher frequency color in the image
23
+ for i, (freq1, color1) in enumerate(high_freq_colors):
24
+ for j, (freq2, color2) in enumerate(high_freq_colors):
25
+ if (color_distance(color1, color2) < similarity_threshold) or (color_distance(color1, opaque_color_on_white(color2, 0.5)) < 5):
26
+ if(freq2 > freq1):
27
+ replace_color(image_copy, color1, color2)
28
+
29
+ high_freq_colors = count_high_freq_colors(image_copy)
30
+ print(high_freq_colors)
31
+ return [high_freq_colors, image_copy]
32
+
33
+ def color_quantization(image, color_frequency_list):
34
+ # Extract the color values from the frequency list
35
+ color_values = [color for _, color in color_frequency_list]
36
+
37
+ # Replace the colors that are not in the frequency list with white
38
+ mask = np.ones(image.shape[:2], dtype=bool)
39
+ for color in color_values:
40
+ color_mask = np.all(image == color, axis=2)
41
+ mask = np.logical_and(mask, np.logical_not(color_mask))
42
+ image[mask] = (255, 255, 255)
43
+
44
+ return image
45
 
46
  def create_binary_matrix(img_arr, target_color):
47
  # Create mask of pixels with target color