Last active
April 22, 2019 12:45
-
-
Save ankitshekhawat/eaf231cf4a8e554cf4e4743be6c6091b to your computer and use it in GitHub Desktop.
extract colors from 2d array; GPU seeded with kmc2; sorted according to number of samples
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
import kmc2 | |
from sklearn.cluster import MiniBatchKMeans | |
from skimge.color import lab2rgb, rgb2lab | |
def get_colors(array, clusters=3): | |
labs = rgb2lab([array]).squeeze() | |
seeding = kmc2.kmc2(labs, clusters) | |
kmeans = MiniBatchKMeans(n_clusters = clusters, init=seeding) | |
kmeans.fit(np.squeeze(labs)) | |
LABELS = kmeans.labels_ | |
colors = kmeans.cluster_centers_ | |
colors = colors[np.argsort(np.unique(LABELS, return_counts=True)[1])[::-1]] | |
return lab2rgb([colors]).squeeze()*255 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment