K-means and EM algorithms
K-means and EM algorithms
如何实现k-means &EM算法不调用openCV函数做图像分割?
我从这段代码开始:
#include opencv2/highgui/highgui.hpp
#include iostream
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("testImage.png", 0);
Mat label_img;
label_img.create(img.cols, img.rows, CV_8UC1);
}
K-means通过迭代e步和m步直到收敛非常简单。
初始化:
将每个像素随机分配给k
集群之一。也就是说,对于label_img
中的每个条目,随机选择一个数字[0..k-1]
。
给定一个分配给集群的像素(label_img
),您可以计算每个集群的中心(简单地说就是分配给该集群的像素值的平均值)。
在此阶段结束时,您将拥有指向k
集群中心的k
向量。
最大化(M-step)
一旦有了k
集群,计算每个像素到k
中心的距离,并将其分配给最接近像素的中心(通过更改label_img
中的相应条目)。
在此阶段结束时,您将有一个新的分配每个像素到集群(label_img
的新值)
您需要重复这两个步骤,直到label_img
不再更改,或者如果您超过了预定义的迭代次数。
include <opencv2/highgui/highgui.hpp>
include <iostream>
using namespace cv;
using namespace std;
void kmeans(const Mat frame, Mat label) {
}
void em(const Mat frame, const Mat label, Mat new_label) {
}
void main()
{
Mat frame;
Mat label_img;
Mat em_label;
frame = imread("testImage.png", 0);
kmeans(frame, label_img);
em(frame, label_img, em_label);
label_img.create(frame.cols, frame.rows, CV_8UC1);
label_img.ptr<float>(25)[30] = 0;
label_img.ptr<float>(25)[31] = 255;
}
相关文章:
- 错误:'EM::EM(...)' 的原型与类 'EM' 中的任何原型都不匹配
- V8 - 节点C++插件 - 在构造函数中引发异常会导致"致命错误:v8::ToLocalChecked Em
- 如何在qt标签中显示em破折号字符
- 具有 SVM、EM 和其他具有"weights per sample"的方法的库,例如
- OPENCV:加快EM算法预测
- EM算法,读取并保存XML文件
- 错误:(functionName)已经在Algorithms.obj中定义
- 交叉编译c++项目,通用ELF中的重定位(EM: 3)
- Boost::string_ref find algorithms
- boost::move and stl algorithms
- 如何列出em++找到的所有符号
- K-means and EM algorithms