GMM随机数分类:
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
using namespace cv::ml;
int main()
{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); //分类个数cout << "分类个数:" << numCluster << endl;int sampleCount = rng.uniform(2, 1000); //需要分类的点数Mat points(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 = 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); //将随机数据块打乱//使用GMMPtr<EM> 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 pixelsMat sample(1, 2, CV_32FC1);for (int row = 0; row < img.rows; row++){for (int col = 0; col < img.cols; col++){sample.at<float>(0) = (float)col;sample.at<float>(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 clustersfor (int i = 0; i < sampleCount; i++){Point p(cvRound(points.at<float>(i, 0)), cvRound(points.at<float>(i, 1)));circle(img, p, 1, colorTab[labels.at<int>(i)], -1);}imshow("GMM_Demo", img);waitKey(0);return 0; //返回值为0表示成功执行此函数
}
运行结果:
GMM 图像分割案例:
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::ml;
实时性很差/
int main(int argc, char **argv)
{Mat src = imread("E:\\vs2015\\opencvstudy\\2kmeans.jpg", 1);if (src.empty()){cout << "could not load the image!" << endl;return -1; //返回-1代表函数执行失败}imshow("input", src);int width = src.cols;int height = src.rows;int dims = src.channels();初始化定义int nsamples = width*height;const Scalar color[] = {Scalar(0,0,255),Scalar(0,255,0),Scalar(0,0,255),Scalar(255,255,0)};int numcluster = 4;Mat points(nsamples, dims, CV_64FC1);Mat labels;Mat result = Mat::zeros(src.size(), src.type());RGB数据转换到样本数据int index = 0;for (int row = 0; row < height; row++){for (int col = 0; col < width; col++){index = row*width + col;Vec3b bgr = src.at<Vec3b>(row, col);points.at<double>(index, 0) = static_cast<int>(bgr[0]);points.at<double>(index, 1) = static_cast<int>(bgr[1]);points.at<double>(index, 2) = static_cast<int>(bgr[2]);}}EM Cluster TrainPtr <EM> 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<int>(index,0);Scalar c = color[label];result.at<Vec3b>(row, col)[0] = c[0];result.at<Vec3b>(row, col)[1] = c[1];result.at<Vec3b>(row, col)[2] = c[2];*/b = src.at<Vec3b>(row,col)[0];g = src.at<Vec3b>(row,col)[1];r = src.at<Vec3b>(row,col)[2];sample.at<double>(0) = b;sample.at<double>(1) = g;sample.at<double>(2) = r;int response = cvRound(em_model->predict2(sample, noArray())[1]);Scalar c = color[response];result.at<Vec3b>(row, col)[0] = c[0];result.at<Vec3b>(row, col)[1] = c[1];result.at<Vec3b>(row, col)[2] = c[2];}}cout << "time consume:" << (getTickCount()-time) / getTickFrequency() * 1000 << endl;imshow("EM_Result", result);imwrite("EM_Result.jpg",result);//for (int i = 0; i < centers.rows; i++)//{// int x = centers.at<float>(i, 0);// int y = centers.at<float>(i, 1);// cout << "第" << i << "个:" << "c.x" << x << "c.y" << y << endl;//}//imshow("KMeans_Result", result);waitKey(0);return 0;
}
运行结果: