Created
March 2, 2018 17:21
-
-
Save nbortolotti/9ee4aa75e5d1419eceaf96a2d8bf0f6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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