- Introduced SVG export, which has some issues with dynamical systems (too many paths...
[mldemos:mldemos.git] / _AlgorithmsPlugins / OpenCV / interfaceKMCluster.cpp
1 /*********************************************************************\r
2 MLDemos: A User-Friendly visualization toolkit for machine learning\r
3 Copyright (C) 2010  Basilio Noris\r
4 Contact: mldemos@b4silio.com\r
5 \r
6 This library is free software; you can redistribute it and/or\r
7 modify it under the terms of the GNU Lesser General Public License,\r
8 version 3 as published by the Free Software Foundation.\r
9 \r
10 This library is distributed in the hope that it will be useful, but\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
13 Lesser General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU Lesser General Public\r
16 License along with this library; if not, write to the Free\r
17 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
18 *********************************************************************/\r
19 #include "interfaceKMCluster.h"\r
20 #include "basicOpenCV.h"\r
21 #include <QPixmap>\r
22 #include <QBitmap>\r
23 #include <QPainter>\r
24 \r
25 using namespace std;\r
26 \r
27 ClustKM::ClustKM()\r
28 {\r
29         params = new Ui::ParametersKM();\r
30         params->setupUi(widget = new QWidget());\r
31 }\r
32 \r
33 void ClustKM::SetParams(Clusterer *clusterer)\r
34 {\r
35         if(!clusterer) return;\r
36         int clusters = params->kmeansClusterSpin->value();\r
37         int power = params->kmeansNormSpin->value();\r
38         int metrictype = params->kmeansNormCombo->currentIndex();\r
39         float beta = params->kmeansBetaSpin->value();\r
40         int method = params->kmeansMethodCombo->currentIndex();\r
41         if (metrictype < 3) power = metrictype;\r
42         ((ClustererKM *)clusterer)->SetParams(clusters, method, beta, power);\r
43 }\r
44 \r
45 Clusterer *ClustKM::GetClusterer()\r
46 {\r
47         ClustererKM *clusterer = new ClustererKM();\r
48         SetParams(clusterer);\r
49         return clusterer;\r
50 }\r
51 \r
52 void ClustKM::DrawInfo(Canvas *canvas, QPainter &painter, Clusterer *clusterer)\r
53 {\r
54         if(!canvas || !clusterer) return;\r
55         painter.setRenderHint(QPainter::Antialiasing);\r
56         ClustererKM * _kmeans = (ClustererKM*)clusterer;\r
57         KMeansCluster *kmeans = _kmeans->kmeans;\r
58         painter.setBrush(Qt::NoBrush);\r
59         FOR(i, kmeans->GetClusters())\r
60         {\r
61                 fvec mean = kmeans->GetMean(i);\r
62                 QPointF point = canvas->toCanvasCoords(mean);\r
63                 CvScalar color = CV::color[(i+1)%CV::colorCnt];\r
64                 painter.setPen(QPen(Qt::black, 12));\r
65                 painter.drawEllipse(point, 8, 8);\r
66                 painter.setPen(QPen(QColor(color.val[0],color.val[1],color.val[2]),4));\r
67                 painter.drawEllipse(point, 8, 8);\r
68                 //painter.setPen(QPen(Qt::white, 2));\r
69                 //painter.drawEllipse(point, 8, 8);\r
70         }\r
71 }\r
72 \r
73 void ClustKM::DrawModel(Canvas *canvas, QPainter &painter, Clusterer *clusterer)\r
74 {\r
75         if(!canvas || !clusterer) return;\r
76         painter.setRenderHint(QPainter::Antialiasing);\r
77 \r
78         FOR(i, canvas->data->GetSamples().size())\r
79         {\r
80                 fvec sample = canvas->data->GetSample(i);\r
81                 QPointF point = canvas->toCanvasCoords(sample);\r
82                 fvec res = clusterer->Test(sample);\r
83                 float r=0,g=0,b=0;\r
84                 if(res.size() > 1)\r
85                 {\r
86                         FOR(j, res.size())\r
87                         {\r
88                                 r += CVColor[(j+1)%CVColorCnt].red()*res[j];\r
89                                 g += CVColor[(j+1)%CVColorCnt].green()*res[j];\r
90                                 b += CVColor[(j+1)%CVColorCnt].blue()*res[j];\r
91                         }\r
92                 }\r
93                 else if(res.size())\r
94                 {\r
95                         r = (1-res[0])*255 + res[0]* 255;\r
96                         g = (1-res[0])*255;\r
97                         b = (1-res[0])*255;\r
98                 }\r
99                 painter.setBrush(QColor(r,g,b));\r
100                 painter.setPen(Qt::black);\r
101                 painter.drawEllipse(point,5,5);\r
102         }\r
103 }\r
104 \r
105 void ClustKM::SaveOptions(QSettings &settings)\r
106 {\r
107         settings.setValue("kmeansBeta", params->kmeansBetaSpin->value());\r
108         settings.setValue("kmeansCluster", params->kmeansClusterSpin->value());\r
109         settings.setValue("kmeansMethod", params->kmeansMethodCombo->currentIndex());\r
110         settings.setValue("kmeansPower", params->kmeansNormSpin->value());\r
111         settings.setValue("kmeansNormCombo", params->kmeansNormCombo->currentIndex());\r
112 }\r
113 \r
114 bool ClustKM::LoadOptions(QSettings &settings)\r
115 {\r
116         if(settings.contains("kmeansBeta")) params->kmeansBetaSpin->setValue(settings.value("kmeansBeta").toFloat());\r
117         if(settings.contains("kmeansCluster")) params->kmeansClusterSpin->setValue(settings.value("kmeansCluster").toFloat());\r
118         if(settings.contains("kmeansMethod")) params->kmeansMethodCombo->setCurrentIndex(settings.value("kmeansMethod").toInt());\r
119         if(settings.contains("kmeansPower")) params->kmeansNormSpin->setValue(settings.value("kmeansPower").toFloat());\r
120         if(settings.contains("kmeansNormCombo")) params->kmeansNormCombo->setCurrentIndex(settings.value("kmeansNormCombo").toInt());\r
121         return true;\r
122 }\r
123 \r
124 void ClustKM::SaveParams(std::ofstream &file)\r
125 {\r
126         file << "clusterOptions" << ":" << "kmeansBeta" << " " << params->kmeansBetaSpin->value() << std::endl;\r
127         file << "clusterOptions" << ":" << "kmeansCluster" << " " << params->kmeansClusterSpin->value() << std::endl;\r
128         file << "clusterOptions" << ":" << "kmeansMethod" << " " << params->kmeansMethodCombo->currentIndex() << std::endl;\r
129         file << "clusterOptions" << ":" << "kmeansPower" << " " << params->kmeansNormSpin->value() << std::endl;\r
130         file << "clusterOptions" << ":" << "kmeansNormCombo" << " " << params->kmeansNormCombo->currentIndex() << std::endl;\r
131 }\r
132 \r
133 bool ClustKM::LoadParams(char *line, float value)\r
134 {\r
135         if(endsWith(line,"kmeansBeta")) params->kmeansBetaSpin->setValue(value);\r
136         if(endsWith(line,"kmeansCluster")) params->kmeansClusterSpin->setValue((int)value);\r
137         if(endsWith(line,"kmeansMethod")) params->kmeansMethodCombo->setCurrentIndex((int)value);\r
138         if(endsWith(line,"kmeansPower")) params->kmeansNormSpin->setValue((int)value);\r
139         if(endsWith(line,"kmeansNormCombo")) params->kmeansNormCombo->setCurrentIndex((int)value);\r
140         return true;\r
141 }\r