kmeans
函數(shù)原型
double cv::kmeans(
InputArray data,
int K,
InputOutputArray bestLabels,
TermCriteria criteria,
int attempts,
int flags,
OutputArray centers = noArray()
)
參數(shù)說明
-
Parameters
data 待聚類的數(shù)據(jù)集,數(shù)據(jù)集的每一個(gè)樣本是一個(gè)N維的點(diǎn),點(diǎn)坐標(biāo)都是float型的,例如:有m個(gè)樣本,每個(gè)樣本有n個(gè)維度,那data的格式就為cv::Mat dataSet(m,n,CV_32F) K 聚類數(shù),即要把數(shù)據(jù)集聚成k類. bestLabels 存儲data中每一個(gè)樣本的標(biāo)簽,數(shù)據(jù)類型為int型 criteria opencv中迭代算法的終止條件,例如迭代的次數(shù)限制,或者迭代的精度達(dá)到要求時(shí),算法迭代終止 attempts 使用不同的初始聚類中心執(zhí)行算法的次數(shù) flags cv::KmeansFlags見下表,選擇聚類中心的初始化方式 centers Output matrix of the cluster centers, one row per each cluster center. cv::KmeansFlags
| KMEANS_RANDOM_CENTERS Python: cv.KMEANS_RANDOM_CENTERS | Select random initial centers in each attempt. |
|---|---|
| KMEANS_PP_CENTERS Python: cv.KMEANS_PP_CENTERS | Use kmeans++ center initialization by Arthur and Vassilvitskii [Arthur2007]. |
| KMEANS_USE_INITIAL_LABELS Python: cv.KMEANS_USE_INITIAL_LABELS | During the first (and possibly the only) attempt, use the user-supplied labels instead of computing them from the initial centers. For the second and further attempts, use the random or semi-random centers. Use one of KMEANS_*_CENTERS flag to specify the exact method. |
示例
讀取一張圖片,把圖片中每一個(gè)像素點(diǎn)的RGB值作為特征進(jìn)行聚類(顏色量化),聚類數(shù)目根據(jù)需要進(jìn)行調(diào)整。
#include "opencv.hpp"
int kmeansDemo(cv::Mat &srcImage, cv::Mat &dst, int clusterCount)
{
if (srcImage.empty())
return -1;
if (clusterCount <= 0)
return -1;
//cv::GaussianBlur(srcImage, srcImage, cv::Size(0, 0), 2);
int width = srcImage.cols;
int height = srcImage.rows;
//init
int sampleCount = width * height;
cv::Mat labels;//Input/output integer array that stores the cluster indices for every sample
cv::Mat centers;//Output matrix of the cluster centers, one row per each cluster center.
// convert image to kmeans data
cv::Mat sampleData = srcImage.reshape(3, sampleCount);//every pixel is a sample
cv::Mat data;
sampleData.convertTo(data, CV_32F);
//K-Means
cv::TermCriteria criteria = cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 5, 0.1);
cv::kmeans(data, clusterCount, labels, criteria, clusterCount, cv::KMEANS_PP_CENTERS, centers);
//create a color map
std::vector<cv::Scalar> colorMaps;
uchar b, g, r;;
//clusterCount is equal to centers.rows
for (int i = 0; i < centers.rows; i++)
{
b = (uchar)centers.at<float>(i, 0);
g = (uchar)centers.at<float>(i, 1);
r = (uchar)centers.at<float>(i, 2);
colorMaps.push_back(cv::Scalar(b, g, r));
}
// Show result
int index = 0;
dst = cv::Mat::zeros(srcImage.size(), srcImage.type());
uchar *ptr=NULL;
int *label = NULL;
for (int row = 0; row < height; row++) {
ptr = dst.ptr<uchar>(row);
for (int col = 0; col < width; col++) {
index = row * width + col;
label = labels.ptr<int>(index);
*(ptr + col * 3) = colorMaps[*label][0];
*(ptr + col * 3 + 1) = colorMaps[*label][1];
*(ptr + col * 3 + 2) = colorMaps[*label][2];
}
}
return 0;
}
int main()
{
int clusterCount = 8;//the number of clusters
std::string path = "K:\\deepImage\\fruit.jpg";
cv::Mat srcImage = cv::imread(path);
cv::imshow("srcImage", srcImage);
cv::Mat dst;
kmeansDemo(srcImage,dst,clusterCount);
std::string txt = "clusters:" + std::to_string(clusterCount);
cv::putText(dst, txt, cv::Point(5, 35), 0, 1, cv::Scalar(0, 255, 250), 2);
cv::imshow("result", dst);
cv::waitKey(0);
return 0;
}
-
效果
顏色聚類數(shù)為8的效果
顏色聚類數(shù)為6
顏色聚類數(shù)為16


