opencv对灰度图像做kmeans

		Mat  source;
Mat points(image_width*image_height, 1, CV_32F, Scalar(10));
		int ind = 0;
		for (int row = 0; row < image_height; row++) {
			for (int col = 0; col < image_width; col++) {
				ind = row * image_width + col;//			
				points.at<float>(ind, 0) = source.at<float>(row, col);
			}
		}
		//运行kmeans
		int numCluster = 2;
		Mat labels;
		Mat centers;
		TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 0.1);
		kmeans(points, numCluster, labels, criteria, 3, KMEANS_PP_CENTERS, centers);

		// 显示图像分割后的结果,一维转多维
		Mat result = Mat::zeros(Filter_diff.size(), Filter_diff.type());
		for (int row = 0; row < image_height; row++)
		{
			for (int col = 0; col < image_width; col++)
			{
				int index = row * image_width + col;
				int label = labels.at<int>(index, 0);
				result.at<float>(row, col) = label;
			}
		}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值