Skip to content
This repository
Browse code

64 colors + random codebook comparison.

  • Loading branch information...
commit 975027d5e57bd51bcd23418ac5c81c64358659d3 1 parent cd88137
Mathieu Blondel authored
39  examples/cluster/plot_color_quantization.py
@@ -6,23 +6,29 @@
6 6
 
7 7
 Performs a pixel-wise Vector Quantization (VQ) of an image of the summer palace
8 8
 (China), reducing the number of colors required to show the image from 96,615
9  
-unique colors to 256, while preserving the overall appearance quality.
  9
+unique colors to 64, while preserving the overall appearance quality.
10 10
 
11 11
 In this example, pixels are represented in a 3D-space and K-means is used to
12  
-find 256 clusters. In the image processing literature, the codebook obtained
13  
-from K-means (the cluster centers) is called the color palette. Using a
14  
-256-color palette, pixels can be encoded with a single byte (the index of the
15  
-closest color in the palette) whereas an RGB encoding requires 3 bytes per
16  
-pixel. The GIF file format, for example, uses such a palette.
  12
+find 64 color clusters. In the image processing literature, the codebook
  13
+obtained from K-means (the cluster centers) is called the color palette. Using a
  14
+a single byte, up to 256 colors can be addressed, whereas an RGB encoding
  15
+requires 3 bytes per pixel. The GIF file format, for example, uses such a
  16
+palette.
  17
+
  18
+For comparison, a quantized image using a random codebook (colors picked up
  19
+randomly) is also shown.
17 20
 """
18 21
 print __doc__
19 22
 import numpy as np
20 23
 import pylab as pl
21 24
 from scikits.learn.cluster import KMeans
  25
+from scikits.learn.metrics import euclidean_distances
22 26
 from scikits.learn.datasets import load_sample_image
23 27
 from scikits.learn.utils import shuffle
24 28
 from time import time
25 29
 
  30
+n_colors = 64
  31
+
26 32
 # Load the Summer Palace photo
27 33
 china = load_sample_image("china.jpg")
28 34
 
@@ -39,16 +45,24 @@
39 45
 print "Fitting estimator on a small sub-sample of the data"
40 46
 t0 = time()
41 47
 image_array_sample = shuffle(image_array, random_state=0)[:1000]
42  
-kmeans = KMeans(k=256, random_state=0).fit(image_array_sample)
  48
+kmeans = KMeans(k=n_colors, random_state=0).fit(image_array_sample)
43 49
 print "done in %0.3fs." % (time() - t0)
44 50
 
45 51
 # Get labels for all points
46  
-print "Predicting labels on the full image"
  52
+print "Predicting color indices on the full image (k-means)"
47 53
 t0 = time()
48 54
 labels = kmeans.predict(image_array)
49 55
 print "done in %0.3fs." % (time() - t0)
50 56
 
51 57
 
  58
+codebook_random = shuffle(image_array, random_state=0)[:n_colors+1]
  59
+print "Predicting color indices on the full image (random)"
  60
+t0 = time()
  61
+dist = euclidean_distances(codebook_random, image_array, squared=True)
  62
+labels_random = dist.argmin(axis=0)
  63
+print "done in %0.3fs." % (time() - t0)
  64
+
  65
+
52 66
 def recreate_image(codebook, labels, w, h):
53 67
     """Recreate the (compressed) image from the code book & labels"""
54 68
     d = codebook.shape[1]
@@ -72,6 +86,13 @@ def recreate_image(codebook, labels, w, h):
72 86
 pl.clf()
73 87
 ax = pl.axes([0, 0, 1, 1])
74 88
 pl.axis('off')
75  
-pl.title('Quantized image (256 colors)')
  89
+pl.title('Quantized image (64 colors, K-Means)')
76 90
 pl.imshow(recreate_image(kmeans.cluster_centers_, labels, w, h))
  91
+
  92
+pl.figure(3)
  93
+pl.clf()
  94
+ax = pl.axes([0, 0, 1, 1])
  95
+pl.axis('off')
  96
+pl.title('Quantized image (64 colors, Random)')
  97
+pl.imshow(recreate_image(codebook_random, labels_random, w, h))
77 98
 pl.show()

0 notes on commit 975027d

Please sign in to comment.
Something went wrong with that request. Please try again.