GMM在数据聚类和图像分类中有很重要的应用。
概念理解:
(1)条件概率:
(2)先验概率:在有一定量数据的前提下,我们对参数进行概率估计,事件发生前的预判概率。
(3)后验概率:在最合适的那个参数的前提下,观测数据出现的最大概率。
(4)极大似然估计:找到一组参数使得我们观测到的数据出现的概率最大。
(5)高斯分布:,概率密度函数。其中N的两个参数第一个代表均值,第二个代表协方差矩阵。
(6)参数估计:已知概率密度函数的形式,而要估计其中的参数的过程。
GMM高斯混合模型(Gaussian Mixed Model)指的是多个高斯分布函数的线性组合。
下边对其原理做一个了解。
高斯混合模型(GMM)
设有随机变量X ,则混合高斯模型可以用下式表示:
GMM聚类时分为两步,第一步是随机地在这K 个分量中选一个,每个分量被选中的概率即为混合系数为πk, 可以设定π1=π2=0.5,表示每个分量被选中的概率是0.5,即从中抽出一个点,这个点属于第一类的概率和第二类的概率各占一半。实际应用中事先指定πk 的值是很笨的做法,当问题一般化后,会出现一个问题:当从集合随机选取一个点,并不能确定这个点来自哪里?换言之怎么根据数据自动确定π1 和π2 的值?这就是GMM参数估计的问题。要解决这个问题,可以使用EM算法。通过EM算法,我们可以迭代计算出GMM中的参数:
参数估计过程:
GMM的应用之一:数据聚类
#include
#include
using namespace cv;
using namespace cv::ml;
using namespace std;
int main(int argc, char** argv) {
Mat img = Mat::zeros(500, 500, CV_8UC3);
RNG rng(12345);
Scalar colorTab[] = {
Scalar(0, 0, 255),
Scalar(0, 255, 0),
Scalar(255, 0, 0),
Scalar(0, 255, 255),
Scalar(255, 0, 255)
};
int numCluster = rng.uniform(2, 5);
printf("number of clusters : %d\n", numCluster);
int sampleCount = rng.uniform(5, 1000);
Mat points(sampleCount, 2, CV_32FC1);
Mat labels;
// 生成随机数
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 = points.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(points, 1, &rng);
//training
Ptr em_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());
// 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]);
Vec2d predict = em_model->predict2(sample, noArray()); // 预言
int response = cvRound(predict[1]); // response 就是给出的当前的分类
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(points.at(i, 0)), cvRound(points.at(i, 1)));
circle(img, p, 1, colorTab[labels.at(i)], -1);
}
imshow("GMM-EM Demo", img);
waitKey(0);
return 0;
}
GMM的应用二:图像分割
#include
#include
using namespace cv;
using namespace cv::ml;
using namespace std;
int main(int argc, char** argv) {
Mat src = imread("D:/picture/opencv/images/toux.jpg");
if (src.empty()) {
printf("could not load iamge...\n");
return -1;
}
const char* inputWinTitle = "input image";
namedWindow(inputWinTitle, CV_WINDOW_AUTOSIZE);
imshow(inputWinTitle, src);
// 初始化
int numCluster = 3;
const Scalar colors[] = {
Scalar(255, 0, 0),
Scalar(0, 255, 0),
Scalar(0, 0, 255),
Scalar(255, 255, 0)
};
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 = 0;
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
Ptr em_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);
double 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;
/*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];*/
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];
}
}
printf("execution time(ms) : %.2f\n", (getTickCount() - time) / getTickFrequency() * 1000);
imshow("EM-Segmentation", result);
waitKey(0);
return 0;
}