Image Colors
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 41 of 51.
A screen printer has a few inks, not millions of colors. k-means picks the best few for any picture:
A color image has shape (rows, columns, 3): red, green and blue per pixel, each from 0 to 1. One reshape turns the pixels into data:
pixels = image.reshape(-1, 3)
km = KMeans(n_clusters=4, n_init=10, random_state=0).fit(pixels)
poster = km.cluster_centers_[km.labels_].reshape(image.shape)
ax.imshow(poster)Each center is a color. Indexing the centers with the labels gives every pixel its center's color; the reshape folds the rows back into a picture.
Challenge
MediumA poster studio screen-prints a mountain lake with k inks. The starter paints the 40 by 60 image, reads k and sets up a titled chart.
- Turn the image into one row per pixel and find
kcolors with k-means. - Repaint every pixel with its center's color and fold the rows back into a picture.
- Draw it on
axand show it.
Like this sunset in 5 inks:
Your program prints nothing: Coddy describes your picture and its inks. Then try other values of k: when does the red boat get its own ink?
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Paints a 40 by 60 mountain lake: a sky, a snowy peak, a forest, the water and a red boat
def paint_lake():
image = np.zeros((40, 60, 3))
for r in range(40):
for c in range(60):
if 30 <= r <= 31 and 10 <= c <= 17 - (r - 30) * 2:
image[r, c] = [0.8, 0.15, 0.1]
elif r >= 27:
t = (r - 27) / 12
image[r, c] = [0.1, 0.45 - 0.2 * t, 0.65 - 0.2 * t]
elif r >= 21:
image[r, c] = [0.1, 0.35 + 0.05 * (c % 3), 0.15]
elif r >= abs(c - 34) + 4 and r < 10:
image[r, c] = [0.95, 0.95, 0.97]
elif r >= abs(c - 34) + 4:
image[r, c] = [0.45, 0.42, 0.45]
else:
image[r, c] = [0.55 + r / 50, 0.75 + r / 120, 0.95]
return np.round(image, 3)
# Don't change the lines below
image = paint_lake()
k = int(input())
fig, ax = plt.subplots()
ax.set_title(f"{k} inks")
ax.axis("off")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler