# Revision history [back]

### Kmeans segmentation

I have done a kmeans on an image for segmentation and now I want to restore the colors of the centers in the segmented image (for better visualization):

cv::Mat image = cv::imread(argv[1]);
cv::Mat fImage;
image.convertTo(fImage, CV_32F);
fImage = fImage.reshape(3, image.cols * image.rows);
cv::Mat labels;
cv::Mat centers;
cv::kmeans(fImage, 10, labels, cv::TermCriteria(), 3, cv::KMEANS_RANDOM_CENTERS, centers);
cv::Mat segmented = labels.reshape(1, image.rows);
segmented.convertTo(segmented, CV_8UC3);


but segmented contains just the indexes in the centers. Is there a fast way to replace the values of segmented by the values of centers?

I know I can do the for loop in for loop to replace the values, but I thought of something like LUT; unfortunately, this runs only on 256 lut Mat, but centers is not a 256 one channel Mat. Is there another way to do it?