-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmm.h
More file actions
87 lines (65 loc) · 2.35 KB
/
Copy pathgmm.h
File metadata and controls
87 lines (65 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef GMM_H
#define GMM_H
#include "config.h"
#include <QVector>
#include <QImage>
struct Gaussian
{
double w; // Вес гауссиана
RgbColor mu; // Среднее
double sigma[3][3]; // Матрица ковариаций [i][j]
double det; // Определитель матрицы ковариаций
double sqrtDet; // Квадрат определителя
double inver[3][3]; // Матрица обратная к матрице ковариаций
int pointsCount; // Количество точек
double coeff;
double eigenValues[3];
double eigenVectors[3][3];
};
float gaussN(RgbColor x, const Gaussian& g);
Gaussian* EMclustering(Gaussian* gaussians, const int K, const float delta);
class GMM
{
public:
GMM(int k = 5);
~GMM();
void loadImage(QImage &image_);
void loadPoints(QImage &points);
void finalize();
float p(RgbColor x);
float p(QRgb x);
private:
int K; // Число гауссиан. По крайней мере пока задается
int pointsCount; // Количество точек. Может понадобится в будущем
QImage* image;
Gaussian * g;
QList<RgbColor> pointsList;
inline float p(int i, RgbColor x)
{
return g[i].w * gaussN(x, g[i]);
}
friend class EM;
};
class EM
{
private:
GMM* gmmParent;
float R; // Параметр разбиения вероятности
float deltaMax; // Параметр, по которому выходим из EM
float* computeG(Gaussian);
void fitGaussian(Gaussian *g, QList<RgbColor> &points);
/// EM шаги
// E шаг
QVector<float>* Estep(Gaussian *g, int K, QList<RgbColor> &points);
// M шаг
void Mstep(Gaussian* g, QList<RgbColor> &points, QVector<float> &G);
public:
EM(GMM* gmm = 0, float delta = 0.0005, float R_ = 2) :
gmmParent(gmm), R(R_), deltaMax(delta) {}
// Обычный EM алгоритм
Gaussian* split(int K, QList<RgbColor> &points, Gaussian* g = 0);
// EM с последовательным добавлением элементов
Gaussian* splitContinious(int &K, QList<RgbColor> &points, int minPoints = 0);
Gaussian* splitNotEM(int K, QList<RgbColor> &points);
};
#endif // GMM_H