一、Kmeans简介
二、opencv-kmeans主色提取
//输入彩色图像 k-means获取主色(颜色中心及比例)
void DCD(Mat& src)
{
if(!src.data)
{
return;
}
//分割通道
vector<Mat> bgr;
split(src, bgr);
int nRows = src.rows;
int nCols = src.cols;
//设定类数
int nClusters = 20;
//输入样本 按行存储
Mat samples(nRows*nCols, 1, CV_32FC3/*, Scalar(0)*/);
for(int i = 0; i < nRows; ++i)
{
for(int j = 0; j < nCols; ++j)
{
for(int iChanel = 0; iChanel < 3; ++iChanel)
{
samples.at<Vec3f>(i*nCols + j)[iChanel] = bgr[iChanel].at<uchar>(i, j);
}
}
}
TermCriteria criteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 1000, FLT_EPSILON);
Mat centers(nClusters,1, CV_32FC3);
Mat label