博客
关于我
图像分割系列5_GMM(高斯混合模型)对图像进行分割
阅读量:686 次
发布时间:2019-03-17

本文共 2311 字,大约阅读时间需要 7 分钟。

实例5:GMM(高斯混合模型)图像分割

#include 
#include
using namespace cv;using namespace cv::ml;using namespace std;int main(int argc, char** argv) { Mat src = imread("toux.jpg"); if (src.empty()) { printf("could not load iamge...\n"); return -1; } namedWindow("input image", CV_WINDOW_AUTOSIZE); imshow("input image", 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);//设置协方差矩阵 //设置停止条件,训练100次结束 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;}

           执行时间:

可见,GMM算法处理时间较长,并不适合工程实时图像处理。

转载地址:http://ezuhz.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
查看>>
Mysql学习总结(61)——MySQL优化之DBA级优化整理汇总
查看>>
Mysql学习总结(62)——MySQL连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link问题
查看>>
Mysql学习总结(63)——Mysql数据库架构方案选择与分析
查看>>
Mysql学习总结(64)——Mysql配置文件my.cnf各项参数解读
查看>>
Mysql学习总结(65)——项目实战中常用SQL实践总结
查看>>
Mysql学习总结(66)——设置MYSQL数据库编码为UTF-8
查看>>
Mysql学习总结(67)——MYSQL慢查询日志
查看>>
Mysql学习总结(68)——MYSQL统计每天、每周、每月、每年数据 SQL 总结
查看>>
Mysql学习总结(69)——Mysql EXPLAIN 命令使用总结
查看>>
Mysql学习总结(6)——MySql之ALTER命令用法详细解读
查看>>
Mysql学习总结(70)——MySQL 优化实施方案
查看>>
Mysql学习总结(71)——MySQL 重复记录查询与删除总结
查看>>
Mysql学习总结(71)——数据库介绍(MySQL安装 体系结构、基本管理)再回顾
查看>>
Mysql学习总结(72)——MySQL 开发者开发,设计规范再总结
查看>>
Mysql学习总结(73)——MySQL 查询A表存在B表不存在的数据SQL总结
查看>>
Mysql学习总结(74)——慢SQL!压垮团队的最后一根稻草!
查看>>
Mysql学习总结(75)——并发量大、数据量大的互联网业务数据库设计军规
查看>>
Mysql学习总结(76)——MySQL执行计划(explain)结果含义总结
查看>>
Mysql学习总结(77)——温故Mysql数据库开发核心原则与规范
查看>>