10 #define DEFAULT_PARAM "-t 2 -c 100" 34 virtual void mousePressEvent( QMouseEvent* );
35 virtual void paintEvent( QPaintEvent* );
42 QPushButton button_change_icon;
43 QPushButton button_run;
44 QPushButton button_clear;
45 QPushButton button_save;
46 QPushButton button_load;
48 QPainter buffer_painter;
55 const QPixmap& choose_icon(
int v)
57 if(v==1)
return icon1;
58 else if(v==2)
return icon2;
64 buffer.fill(Qt::black);
69 const QPixmap& icon = choose_icon(p.value);
70 buffer_painter.drawPixmap((
int)(p.x*
XLEN),(
int)(p.y*
YLEN),icon);
75 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
79 void button_change_icon_clicked()
82 if(current_value > 3) current_value = 1;
83 button_change_icon.setIcon(choose_icon(current_value));
88 if(point_list.empty())
return;
111 const char *p = input_line.text().toLatin1().constData();
114 while (*p && *p !=
'-')
132 param.
gamma = atof(p);
135 param.
coef0 = atof(p);
163 while(*p && !isspace(*p)) ++p;
172 prob.
l = point_list.size();
173 prob.
y =
new double[prob.
l];
186 for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
189 x_space[2 *
i].
value = q->x;
190 x_space[2 * i + 1].
index = -1;
191 prob.
x[
i] = &x_space[2 *
i];
200 int *j =
new int[
XLEN];
202 for (i = 0; i <
XLEN; i++)
204 x[0].
value = (double) i / XLEN;
208 buffer_painter.setPen(
colors[0]);
209 buffer_painter.drawLine(0,0,0,
YLEN-1);
211 int p = (int)(param.
p *
YLEN);
212 for(i = 1; i <
XLEN; i++)
214 buffer_painter.setPen(
colors[0]);
215 buffer_painter.drawLine(i,0,i,
YLEN-1);
217 buffer_painter.setPen(
colors[5]);
218 buffer_painter.drawLine(i-1,j[i-1],i,j[i]);
222 buffer_painter.setPen(
colors[2]);
223 buffer_painter.drawLine(i-1,j[i-1]+p,i,j[i]+p);
225 buffer_painter.setPen(
colors[2]);
226 buffer_painter.drawLine(i-1,j[i-1]-p,i,j[i]-p);
243 for (list <point>::iterator q = point_list.begin(); q != point_list.end(); q++, i++)
246 x_space[3 *
i].
value = q->x;
247 x_space[3 * i + 1].
index = 2;
248 x_space[3 * i + 1].
value = q->y;
249 x_space[3 * i + 2].
index = -1;
250 prob.
x[
i] = &x_space[3 *
i];
251 prob.
y[
i] = q->value;
261 for (i = 0; i <
XLEN; i++)
262 for (j = 0; j <
YLEN ; j++) {
263 x[0].
value = (double) i / XLEN;
264 x[1].
value = (double) j / YLEN;
267 buffer_painter.setPen(
colors[(
int)d]);
268 buffer_painter.drawPoint(i,j);
280 void button_clear_clicked()
284 void button_save_clicked()
286 QString filename = QFileDialog::getSaveFileName();
287 if(!filename.isNull())
289 FILE *fp = fopen(filename.toLatin1().constData(),
"w");
291 const char *p = input_line.text().toLatin1().constData();
292 const char* svm_type_str = strstr(p,
"-s ");
293 int svm_type =
C_SVC;
294 if(svm_type_str != NULL)
295 sscanf(svm_type_str,
"-s %d", &svm_type);
301 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
302 fprintf(fp,
"%f 1:%f\n", p->y, p->x);
306 for(list<point>::iterator p = point_list.begin(); p != point_list.end();p++)
307 fprintf(fp,
"%d 1:%f 2:%f\n", p->value, p->x, p->y);
313 void button_load_clicked()
315 QString filename = QFileDialog::getOpenFileName();
316 if(!filename.isNull())
318 FILE *fp = fopen(filename.toLatin1().constData(),
"r");
323 while(fgets(buf,
sizeof(buf),fp))
327 if(sscanf(buf,
"%d%*d:%lf%*d:%lf",&v,&x,&y)==3)
330 point_list.push_back(p);
332 else if(sscanf(buf,
"%lf%*d:%lf",&y,&x)==2)
334 point p = {
x,
y,current_value};
335 point_list.push_back(p);
348 #include "svm-toy.moc" 351 :button_change_icon(this)
352 ,button_run(
"Run",this)
353 ,button_clear(
"Clear",this)
354 ,button_save(
"Save",this)
355 ,button_load(
"Load",this)
360 buffer.fill(Qt::black);
362 buffer_painter.begin(&buffer);
364 QObject::connect(&button_change_icon, SIGNAL(clicked()),
this,
365 SLOT(button_change_icon_clicked()));
366 QObject::connect(&button_run, SIGNAL(clicked()),
this,
367 SLOT(button_run_clicked()));
368 QObject::connect(&button_clear, SIGNAL(clicked()),
this,
369 SLOT(button_clear_clicked()));
370 QObject::connect(&button_save, SIGNAL(clicked()),
this,
371 SLOT(button_save_clicked()));
372 QObject::connect(&button_load, SIGNAL(clicked()),
this,
373 SLOT(button_load_clicked()));
374 QObject::connect(&input_line, SIGNAL(returnPressed()),
this,
375 SLOT(button_run_clicked()));
378 setAttribute(Qt::WA_NoBackground);
380 icon1 = QPixmap(4,4);
381 icon2 = QPixmap(4,4);
382 icon3 = QPixmap(4,4);
386 painter.begin(&icon1);
387 painter.fillRect(0,0,4,4,QBrush(
colors[4]));
390 painter.begin(&icon2);
391 painter.fillRect(0,0,4,4,QBrush(
colors[5]));
394 painter.begin(&icon3);
395 painter.fillRect(0,0,4,4,QBrush(
colors[6]));
398 button_change_icon.setGeometry( 0,
YLEN, 50, 25 );
399 button_run.setGeometry( 50,
YLEN, 50, 25 );
400 button_clear.setGeometry( 100,
YLEN, 50, 25 );
401 button_save.setGeometry( 150,
YLEN, 50, 25);
402 button_load.setGeometry( 200,
YLEN, 50, 25);
403 input_line.setGeometry( 250,
YLEN, 250, 25);
406 button_change_icon.setIcon(icon1);
411 buffer_painter.end();
416 point p = {(double)event->x()/
XLEN, (double)event->y()/
YLEN, current_value};
417 point_list.push_back(p);
425 p.drawPixmap(0, 0, buffer);
428 int main(
int argc,
char* argv[] )
430 QApplication myapp( argc, argv );
433 mywidget->setGeometry( 100, 100,
XLEN,
YLEN+25 );
void button_run_clicked()
virtual void mousePressEvent(QMouseEvent *)
svm_model * svm_train(const svm_problem *prob, const svm_parameter *param)
virtual void paintEvent(QPaintEvent *)
void draw_point(const point &p)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
int main(int argc, char *argv[])
struct _parameter * param
double svm_predict(const svm_model *model, const svm_node *x)
fprintf(glob_prnt.io, "\)