10 #define DEFAULT_PARAM "-t 2 -c 100" 34 virtual void mousePressEvent( QMouseEvent* );
35 virtual void paintEvent( QPaintEvent* );
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++)
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().toAscii().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++)
188 x_space[2 * i].
index = 1;
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++)
245 x_space[3 * i].
index = 1;
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);
286 QString
filename = QFileDialog::getSaveFileName();
287 if(!filename.isNull())
289 FILE *fp = fopen(filename.toAscii().constData(),
"w");
291 const char *p = input_line.text().toAscii().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);
315 QString
filename = QFileDialog::getOpenFileName();
316 if(!filename.isNull())
318 FILE *fp = fopen(filename.toAscii().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)
366 QObject::connect(&
button_run, SIGNAL(clicked()),
this,
368 QObject::connect(&
button_clear, SIGNAL(clicked()),
this,
370 QObject::connect(&
button_save, SIGNAL(clicked()),
this,
372 QObject::connect(&
button_load, SIGNAL(clicked()),
this,
374 QObject::connect(&
input_line, SIGNAL(returnPressed()),
this,
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]));
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 );
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
TFSIMD_FORCE_INLINE const tfScalar & y() const
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