Skip to content

Instantly share code, notes, and snippets.

@ankitshekhawat
Last active April 22, 2019 12:45
Show Gist options
  • Save ankitshekhawat/eaf231cf4a8e554cf4e4743be6c6091b to your computer and use it in GitHub Desktop.
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
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