opencv kmeans (C++)

kmeans

函数原型

double cv::kmeans(
	InputArray 	data,
	int 	K,
	InputOutputArray 	bestLabels,
	TermCriteria 	criteria,
	int 	attempts,
	int 	flags,
	OutputArray 	centers = noArray()
)

参数说明

  • Parameters

    data 待聚类的数据集,数据集的每一个样本是一个N维的点,点坐标都是float型的,例如:有m个样本,每个样本有n个维度,那data的格式就为cv::Mat dataSet(m,n,CV_32F)
    K 聚类数,即要把数据集聚成k类.
    bestLabels 存储data中每一个样本的标签,数据类型为int型
    criteria opencv中迭代算法的终止条件,例如迭代的次数限制,或者迭代的精度达到要求时,算法迭代终止
    attempts 使用不同的初始聚类中心执行算法的次数
    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.

示例

读取一张图片,把图片中每一个像素点的RGB值作为特征进行聚类(颜色量化),聚类数目根据需要进行调整。

#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;
}
  • 效果

你可能感兴趣的:(opencv,计算机视觉,opencv,kmeans,c++)