opencv图像分割 - 高斯混合模型(GMM)方法

1、方法概述

什么是GMM

GMM的数学模型

高斯混合模型 (GMM)

高斯分布与概率密度分布 - PDF

跟K-Means相比较,属于软分类

实现方法-期望最大化(E-M)

停止条件-收敛

2、代码演示

样本数据训练与预言 :

using namespace cv;
using namespace std;
using namespace cv::ml;
Mat src, dst;
int main(int argc, char** argv) {
	Mat img(500, 500, CV_8UC3);
	RNG rng(12345);
	Scalar colorTab[] = {
		Scalar(0,0,255),
		Scalar(0,255,255),
		Scalar(255,0,0),
		Scalar(255,255,0),
		Scalar(0,255,0)
	};
	int numCluster = rng.uniform(2, 5);
	printf("cluster num :%d\n", numCluster);
	int sampleCount = rng.uniform(2, 1000);
	Mat point(sampleCount, 2, CV_32FC1);
	Mat labels;
	Mat centers;
	//生成随机数
	for (int k = 0; k < numCluster; k++) {
		Point center;
		center.x = rng.uniform(0, img.cols);
		center.y = rng.uniform(0, img.rows);
		Mat pointChunk = point.rowRange(k * sampleCount / numCluster, k == numCluster - 1 ? sampleCount : (k + 1) * sampleCount / numCluster);
		rng.fill(pointChunk, RNG::NORMAL, Scalar(center.x, center.y), Scalar(img.cols * 0.05, img.rows * 0.05));
	}
	randShuffle(point, 1, &rng);
	Ptrem_model = EM::create();
	em_model->setClustersNumber(numCluster);
	em_model->setCovarianceMatrixType(EM::COV_MAT_SPHERICAL);
	em_model->setTermCriteria(TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 100, 0.1));
	em_model->trainEM(point, noArray(), labels, noArray());
	//classify every image pixels
	Mat sample(1, 2, CV_32FC1);
	for (int row = 0; row < img.rows; row++) {
		for (int col = 0; col < img.cols; col++) {
			sample.at(0) = (float)col;
			sample.at(1) = (float)row;
			//预言
			int response = cvRound(em_model->predict2(sample, noArray())[1]);
			Scalar c = colorTab[response];
			circle(img, Point(col, row), 1, c * 0.75, -1);
		}
	}
	//draw the clusters
	for (int i = 0; i < sampleCount; i++) {
		Point p(cvRound(point.at(i, 0)), point.at(i, 1));
		circle(img, p, 1, colorTab[labels.at(i)], -1);
	}
	imshow("GMM resultimg", img);

结果:

opencv图像分割 - 高斯混合模型(GMM)方法_第1张图片

 图像分割 :

//初始化
	int numCluster = 3;
	const Scalar colors[] = {
		Scalar(255,0,0),
		Scalar(0,0,255),
		Scalar(0,255,0),
		Scalar(0,255,255)
	};
	int width = src.cols;
	int height = src.rows;
	int dims = src.channels();
	int nsamples = width * height;
	Mat points(nsamples, dims, CV_64FC1);
	Mat labels;
	Mat result = Mat::zeros(src.size(), CV_8UC3);
	//图像RGB像素 数据转换为样本数据
	int index;
	for (int row = 0; row < height; row++) {
		for (int col = 0; col < width; col++) {
			index = row * width + col;
			Vec3b rgb = src.at(row, col);
			points.at(index, 0) = static_cast(rgb[0]);
			points.at(index, 1) = static_cast(rgb[1]);
			points.at(index, 2) = static_cast(rgb[2]);
		}
	}
	//EM Cluster Train
	Ptrem_model = EM::create();
	em_model->setClustersNumber(numCluster);
	em_model->setCovarianceMatrixType(EM::COV_MAT_SPHERICAL);
	em_model->setTermCriteria(TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 100, 0.1));
	em_model->trainEM(points, noArray(), labels, noArray());
	//对每个像素标记演示与显示
	/*Mat sample(dims, 1, CV_64FC1);
	int time = getTickCount();
	for (int row = 0; row < height; row++) {
		for (int col = 0; col < width; col++) {
			index = row * width + col;
			int label = labels.at(index, 0);
			Scalar c = colors[label];
			result.at(row, col)[0] = c[0];
			result.at(row, col)[1] = c[1];
			result.at(row, col)[2] = c[2];
		}
	}
	printf("time :%d", (getTickCount() - time) *getTickFrequency());
	*/
	//预言
	Mat sample(dims, 1, CV_64FC1);
	int time = getTickCount();
	int r = 0, g = 0, b = 0;
	for (int row = 0; row < height; row++) {
		for (int col = 0; col < width; col++) {
			index = row * width + col;
			b = src.at(row, col)[0];
			g = src.at(row, col)[1];
			r = src.at(row, col)[2];
			sample.at(0) = b;
			sample.at(1) = g;
			sample.at(2) = r;
			int response = cvRound(em_model->predict2(sample, noArray())[1]);
			Scalar c = colors[response];
			result.at(row, col)[0] = c[0];
			result.at(row, col)[1] = c[1];
			result.at(row, col)[2] = c[2];
		}
	}
	imshow("DMMresult", result);
}

结果:

你可能感兴趣的:(opencv学习)