10 #define DEFAULT_PARAM "-t 2 -c 100" 34 virtual void mousePressEvent(QMouseEvent*);
35 virtual void paintEvent(QPaintEvent*);
58 if (v == 1)
return icon1;
59 else if (v == 2)
return icon2;
65 buffer.fill(Qt::black);
70 const QPixmap& icon = choose_icon(p.
value);
71 buffer_painter.drawPixmap((
int)(p.
x *
XLEN), (
int)(p.
y *
YLEN), icon);
76 for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
83 if (current_value > 3) current_value = 1;
84 button_change_icon.setIcon(choose_icon(current_value));
89 if (point_list.empty())
return;
112 const char *p = input_line.text().toAscii().constData();
116 while (*p && *p !=
'-')
135 param.
gamma = atof(p);
138 param.
coef0 = atof(p);
166 while (*p && !isspace(*p)) ++p;
175 prob.
l = point_list.size();
176 prob.
y =
new double[prob.
l];
189 for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
191 x_space[2 * i].
index = 1;
192 x_space[2 * i].
value = q->x;
193 x_space[2 * i + 1].
index = -1;
194 prob.
x[i] = &x_space[2 * i];
203 int *j =
new int[
XLEN];
205 for (i = 0; i <
XLEN; i++)
207 x[0].
value = (double) i / XLEN;
211 buffer_painter.setPen(
colors[0]);
212 buffer_painter.drawLine(0, 0, 0,
YLEN - 1);
214 int p = (int)(param.
p *
YLEN);
215 for (i = 1; i <
XLEN; i++)
217 buffer_painter.setPen(
colors[0]);
218 buffer_painter.drawLine(i, 0, i,
YLEN - 1);
220 buffer_painter.setPen(
colors[5]);
221 buffer_painter.drawLine(i - 1, j[i - 1], i, j[i]);
225 buffer_painter.setPen(
colors[2]);
226 buffer_painter.drawLine(i - 1, j[i - 1] + p, i, j[i] + p);
228 buffer_painter.setPen(
colors[2]);
229 buffer_painter.drawLine(i - 1, j[i - 1] - p, i, j[i] - p);
246 for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
248 x_space[3 * i].
index = 1;
249 x_space[3 * i].
value = q->x;
250 x_space[3 * i + 1].
index = 2;
251 x_space[3 * i + 1].
value = q->y;
252 x_space[3 * i + 2].
index = -1;
253 prob.
x[i] = &x_space[3 * i];
254 prob.
y[i] = q->value;
264 for (i = 0; i <
XLEN; i++)
265 for (j = 0; j <
YLEN ; j++)
267 x[0].
value = (double) i / XLEN;
268 x[1].
value = (double) j / YLEN;
271 buffer_painter.setPen(
colors[(
int)d]);
272 buffer_painter.drawPoint(i, j);
290 QString filename = QFileDialog::getSaveFileName();
291 if (!filename.isNull())
293 FILE *fp = fopen(filename.toAscii().constData(),
"w");
295 const char *p = input_line.text().toAscii().constData();
296 const char* svm_type_str = strstr(p,
"-s ");
297 int svm_type =
C_SVC;
298 if (svm_type_str != NULL)
299 sscanf(svm_type_str,
"-s %d", &svm_type);
305 for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
306 fprintf(fp,
"%f 1:%f\n", p->y, p->x);
310 for (list<point>::iterator p = point_list.begin(); p != point_list.end(); p++)
311 fprintf(fp,
"%d 1:%f 2:%f\n", p->value, p->x, p->y);
319 QString filename = QFileDialog::getOpenFileName();
320 if (!filename.isNull())
322 FILE *fp = fopen(filename.toAscii().constData(),
"r");
327 while (fgets(buf,
sizeof(buf), fp))
331 if (sscanf(buf,
"%d%*d:%lf%*d:%lf", &v, &x, &y) == 3)
334 point_list.push_back(p);
336 else if (sscanf(buf,
"%lf%*d:%lf", &y, &x) == 2)
338 point p = {
x, y, current_value};
339 point_list.push_back(p);
352 #include "svm-toy.moc" 355 : button_change_icon(this)
356 , button_run(
"Run", this)
357 , button_clear(
"Clear", this)
358 , button_save(
"Save", this)
359 , button_load(
"Load", this)
370 QObject::connect(&
button_run, SIGNAL(clicked()),
this,
372 QObject::connect(&
button_clear, SIGNAL(clicked()),
this,
374 QObject::connect(&
button_save, SIGNAL(clicked()),
this,
376 QObject::connect(&
button_load, SIGNAL(clicked()),
this,
378 QObject::connect(&
input_line, SIGNAL(returnPressed()),
this,
382 setAttribute(Qt::WA_NoBackground);
384 icon1 = QPixmap(4, 4);
385 icon2 = QPixmap(4, 4);
386 icon3 = QPixmap(4, 4);
390 painter.begin(&
icon1);
391 painter.fillRect(0, 0, 4, 4, QBrush(
colors[4]));
394 painter.begin(&
icon2);
395 painter.fillRect(0, 0, 4, 4, QBrush(
colors[5]));
398 painter.begin(&
icon3);
399 painter.fillRect(0, 0, 4, 4, QBrush(
colors[6]));
429 p.drawPixmap(0, 0,
buffer);
432 int main(
int argc,
char* argv[])
434 QApplication myapp(argc, argv);
437 mywidget->setGeometry(100, 100,
XLEN,
YLEN + 25);
const QPixmap & choose_icon(int v)
virtual void mousePressEvent(QMouseEvent *)
struct svm_parameter param
def svm_train(arg1, arg2=None, arg3=None)
void button_save_clicked()
virtual void paintEvent(QPaintEvent *)
QPushButton button_change_icon
int main(int argc, char *argv[])
void button_change_icon_clicked()
void button_run_clicked()
def svm_predict(y, x, m, options="")
void draw_point(const point &p)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
void draw_point(const point &p)
void button_clear_clicked()
void button_load_clicked()
struct svm_node * x_space