In [1]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
from sklearn.cluster import MiniBatchKMeans
import numpy as np
import cv2
In [2]:
image = cv2.imread("./data/IMG_3654_1024.jpg")
(h, w) = image.shape[:2]
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
image = image.reshape((image.shape[0] * image.shape[1], 3))
clt = MiniBatchKMeans(n_clusters = 5)
labels = clt.fit_predict(image)
quant = clt.cluster_centers_.astype("uint8")[labels]
quant = quant.reshape((h, w, 3))
image = image.reshape((h, w, 3))
# convert from L*a*b* to RGB
quant = cv2.cvtColor(quant, cv2.COLOR_LAB2BGR)
image = cv2.cvtColor(image, cv2.COLOR_LAB2BGR)
# display the images and wait for a keypress
plt.figure(figsize=(12,8))
plt.subplot(111), plt.imshow(np.hstack([image, quant]))
Out[2]:
In [3]:
image = cv2.imread("./data/IMG_3654_1024.jpg")
(h, w) = image.shape[:2]
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
image = image.reshape((image.shape[0] * image.shape[1], 3))
clt = MiniBatchKMeans(n_clusters = 5)
labels = clt.fit_predict(image)
quant = clt.cluster_centers_.astype("uint8")[labels]
quant = quant.reshape((h, w, 3))
image = image.reshape((h, w, 3))
# convert from L*a*b* to RGB
quant = cv2.cvtColor(quant, cv2.COLOR_LAB2RGB)
image = cv2.cvtColor(image, cv2.COLOR_LAB2RGB)
# display the images and wait for a keypress
plt.figure(figsize=(12,8))
plt.subplot(111), plt.imshow(np.hstack([image, quant]))
Out[3]: