Skip to content

Instantly share code, notes, and snippets.

@nbortolotti
Created March 2, 2018 17:21
Show Gist options
  • Select an option

  • Save nbortolotti/9ee4aa75e5d1419eceaf96a2d8bf0f6c to your computer and use it in GitHub Desktop.

Select an option

Save nbortolotti/9ee4aa75e5d1419eceaf96a2d8bf0f6c to your computer and use it in GitHub Desktop.
def dominant_color(np_object):
pixs = np_object.reshape((-1, 3))
n_colors = 5
flags = cv2.KMEANS_RANDOM_CENTERS
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
_, labels, centroids = cv2.kmeans(np.float32(pixs), n_colors, None, criteria, 10, flags)
palette = np.uint8(centroids)
dominant_object_color = palette[np.argmax(itemfreq(labels)[:, -1])]
return dominant_object_color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment