






from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2


def centroid_histogram(clt):
    
    
    numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
    (hist, _) = np.histogram(clt.labels_, bins = numLabels)

    
    hist = hist.astype("float")
    hist /= hist.sum()

    
    return hist


def plot_colors(hist, centroids):
    
    
    bar = np.zeros((50, 300, 3), dtype = "uint8")
    startX = 0

    
    
    for (percent, color) in zip(hist, centroids):
        
        endX = startX + (percent * 300)
        cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
                      color.astype("uint8").tolist(), -1)
        startX = endX

    
    return bar


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
ap.add_argument("-c", "--clusters", required=True, type=int,
                help="# of clusters")
args = vars(ap.parse_args())



image = cv2.imread(args["image"])
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


plt.figure()
plt.axis("off")
plt.imshow(image)


image = image.reshape((image.shape[0]*image.shape[1], 3))


clt = KMeans(n_clusters=args["clusters"])
clt.fit(image)



hist = centroid_histogram(clt)
bar = plot_colors(hist, clt.cluster_centers_)


plt.figure()
plt.axis("off")
plt.imshow(bar)
plt.show()