Group an array of pixels using k-means clustering as efficiently as possible.