Skip to content

Instantly share code, notes, and snippets.

@andrisgauracs
Created January 10, 2019 22:56
Show Gist options
  • Select an option

  • Save andrisgauracs/d876674a486f6c02123389e763f57dc3 to your computer and use it in GitHub Desktop.

Select an option

Save andrisgauracs/d876674a486f6c02123389e763f57dc3 to your computer and use it in GitHub Desktop.
# Courtesy of https://www.pyimagesearch.com/2014/05/26/opencv-python-k-means-color-clustering/
def centroid_histogram(clt):
# grab the number of different clusters and create a histogram
# based on the number of pixels assigned to each cluster
numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins=numLabels)
# normalize the histogram, such that it sums to one
hist = hist.astype("float")
hist /= hist.sum()
# return the histogram
return hist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment