-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Machine Learning for OpenCV
By :

One exciting application of k-means is the compression of image color spaces. For example, a typical true-color image comes with a 24-bit color depth, allowing for a total of 16,777,216 color variations. However, in most images, a large number of the colors will be unused, and many of the pixels in the image will have similar or identical colors.
Alternatively, we can use k-means to reduce the color palette to, for example, 16 color variations. The trick here is to think of the cluster centers as the reduced color palette. Then k-means will automatically organize the millions of colors in the original image into the appropriate number of colors!
Let's have a look at a particular image:
In [1]: import cv2 ... import numpy as np ... lena = cv2.imread('data/lena.jpg', cv2.IMREAD_COLOR)
By now, we know how to start matplotlib in our sleep:
In [2]: import matplotlib.pyplot as plt ... %matplotlib inline ... plt...