Color classification with k-tools in OpenCV

I want to group many images using the K-Means algorithm. I want to configure clusters so that each cluster represents the dominant color or hue of the image. I read something about this in the article Clustering Color Images Using K-Means

Anyone have an idea to do this in OpenCV?

Maybe I can compare the histograms of each image. But if I have a lot of photos, it takes a lot of time.

+4
source share
1 answer

You can vectorize your image so that each line is a set of RGB, rather than using cv::kmeans for a cluster, for example:

  std::vector<cv::Mat> imgRGB; cv::split(img,imgRGB); int k=5; int n = img.rows *img.cols; cv::Mat img3xN(n,3,CV_8U); for(int i=0;i!=3;++i) imgRGB[i].reshape(1,n).copyTo(img3xN.col(i)); img3xN.convertTo(img3xN,CV_32F); cv::Mat bestLables; cv::kmeans(img3xN,k,bestLables,cv::TermCriteria(),10,cv::KMEANS_RANDOM_CENTERS ); bestLables= bestLables.reshape(0,img.rows); cv::convertScaleAbs(bestLables,bestLables,int(255/k)); cv::imshow("result",bestLables); cv::waitKey(); 
+10
source

All Articles