?? form1.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using libsvm;
namespace Demo
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
const String DEFAULT_PARAM="-t 2 -c 100";
static Color[] colors =
{
Color.Red,
Color.Yellow,
Color.Green,
Color.Violet,
Color.YellowGreen,
Color.Tomato,
Color.Teal,
Color.Tan
};
int XLEN=0;
int YLEN=0;
byte current_value = 1;
private System.Windows.Forms.Panel pictureBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.OpenFileDialog openFileDia;
private System.Windows.Forms.SaveFileDialog saveFileDia;
ArrayList point_list = new ArrayList();
class point
{
public point(double x, double y, byte bValue)
{
this.x = x;
this.y = y;
this.Value = bValue;
}
public double x, y;
public byte Value;
}
private double atof(String s)
{
return Double.Parse(s);
}
private int atoi(String s)
{
return int.Parse(s);
}
void draw_all_points()
{
int n = point_list.Count;
for(int i=0;i<n;i++)
draw_point((point)point_list[i]);
}
void draw_point(point p)
{
Color c = colors[p.Value];
Graphics g=pictureBox1.CreateGraphics();
Pen myPen = new Pen(c);
myPen.Width =1;
//myPen.
g.DrawRectangle(myPen,(float)p.x*XLEN,(float)p.y*YLEN,3,3);
//string strTemp=String.Format("X:{0},y:{1}。水平X:{2},水平Y:{3}",x,y,g.DpiX,g.DpiY);
//MessageBox.Show(strTemp);
}
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.openFileDia = new System.Windows.Forms.OpenFileDialog();
this.saveFileDia = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(424, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Change";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(424, 104);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "run";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Black;
this.pictureBox1.Location = new System.Drawing.Point(16, 24);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(384, 280);
this.pictureBox1.TabIndex = 4;
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
//
// button3
//
this.button3.Location = new System.Drawing.Point(424, 152);
this.button3.Name = "button3";
this.button3.TabIndex = 5;
this.button3.Text = "Clear";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(424, 192);
this.button4.Name = "button4";
this.button4.TabIndex = 6;
this.button4.Text = "保存文件";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(424, 240);
this.button5.Name = "button5";
this.button5.TabIndex = 7;
this.button5.Text = "載入文件";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(552, 325);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
XLEN=pictureBox1.Width;
YLEN=pictureBox1.Height;
}
private void button1_Click(object sender, System.EventArgs e)
{
++current_value;
if(current_value >3) current_value = 1;
}
private void button2_Click(object sender, System.EventArgs e)
{
//String args=DEFAULT_PARAM;
if(point_list.Count==0) return;
svm_parameter param = new svm_parameter();
// default values
param.svm_type = svm_parameter.C_SVC;
param.kernel_type = svm_parameter.RBF;
param.degree = 3;
param.gamma = 0;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 40;
param.C = 1000;
param.eps = 1e-3;
param.p = 0.1;
param.shrinking = 1;
param.nr_weight = 0;
param.weight_label = new int[0];
param.weight = new double[0];
/*
SupportClass.Tokenizer st = new SupportClass.Tokenizer(args);
System.String[] argv = new System.String[st.Count];
for (int i = 0; i < argv.Length; i++)
argv[i] = st.NextToken();
for (int i = 0; i < argv.Length; i++)
{
if (argv[i][0] != '-')
break;
++i;
switch (argv[i - 1][1])
{
case 's':
param.svm_type = atoi(argv[i]);
break;
case 't':
param.kernel_type = atoi(argv[i]);
break;
case 'd':
param.degree = atof(argv[i]);
break;
case 'g':
param.gamma = atof(argv[i]);
break;
case 'r':
param.coef0 = atof(argv[i]);
break;
case 'n':
param.nu = atof(argv[i]);
break;
case 'm':
param.cache_size = atof(argv[i]);
break;
case 'c':
param.C = atof(argv[i]);
break;
case 'e':
param.eps = atof(argv[i]);
break;
case 'p':
param.p = atof(argv[i]);
break;
case 'h':
param.shrinking = atoi(argv[i]);
break;
case 'w':
++param.nr_weight;
{
int[] old = param.weight_label;
param.weight_label = new int[param.nr_weight];
param.weight_label=old;
//System.Console.arraycopy(old, 0, param.weight_label, 0, param.nr_weight - 1);
}
{
double[] old = param.weight;
param.weight = new double[param.nr_weight];
param.weight=old;
//System.Console.arraycopy(old, 0, param.weight, 0, param.nr_weight - 1);
}
param.weight_label[param.nr_weight - 1] = atoi(argv[i - 1].Substring(2));
param.weight[param.nr_weight - 1] = atof(argv[i]);
break;
default:
System.Console.Error.Write("unknown option\n");
break;
}
}
*/
// build problem
svm_problem prob = new svm_problem();
prob.l = point_list.Count;
prob.y = new double[prob.l];
if(param.svm_type == svm_parameter.EPSILON_SVR ||
param.svm_type == svm_parameter.NU_SVR)
{
if(param.gamma == 0) param.gamma = 1;
prob.x = new svm_node[prob.l][];
for(int i=0;i<prob.l;i++)
{
point p = (point)point_list[i];
prob.x[i][0] = new svm_node();
prob.x[i][0].index = 1;
prob.x[i][0].value_Renamed = p.x;
prob.y[i] = p.y;
}
svm_model model = svm.svm_train(prob, param);
svm_node[] x = new svm_node[1];
x[0] = new svm_node();
x[0].index = 1;
int[] j = new int[XLEN];
for (int i = 0; i < XLEN; i++)
{
x[0].value_Renamed = (double) i / XLEN;
j[i] = (int)(YLEN*svm.svm_predict(model, x));
}
}
else
{
if(param.gamma == 0) param.gamma = 0.5;
prob.x = new svm_node [prob.l][];
for(int i=0;i<prob.l;i++)
{
point p = (point)point_list[i];
prob.x[i] = new svm_node[2];
prob.x[i][0] = new svm_node();
prob.x[i][0].index = 1;
prob.x[i][0].Value = p.x;
prob.x[i][1] = new svm_node();
prob.x[i][1].index = 2;
prob.x[i][1].Value = p.y;
prob.y[i] = p.Value;
}
svm_model model = svm.svm_train(prob, param);
svm_node[] x = new svm_node[2];
x[0] = new svm_node();
x[1] = new svm_node();
x[0].index = 1;
x[1].index = 2;
Graphics g=pictureBox1.CreateGraphics();
for (int i = 0; i < XLEN; i++)
for (int j = 0; j < YLEN ; j++)
{
x[0].Value = (double) i /XLEN;
x[1].Value = (double) j / YLEN;
double d = svm.svm_predict(model, x);
Color c = colors[(int)d];
Pen myPen = new Pen(c);
myPen.Width = 1;
g.DrawLine(myPen,i,j,i+1,j+1);
}
draw_all_points();
svm.svm_save_model("c:\\a.txt",model);
}
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
point p=new point((double)e.X/XLEN,(double)e.Y/YLEN,current_value);
point_list.Add(p);
draw_point(p);
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
}
private void button3_Click(object sender, System.EventArgs e)
{
this.point_list.Clear();
Graphics g=pictureBox1.CreateGraphics();
g.Clear(Color.Black);
}
private void button4_Click(object sender, System.EventArgs e)
{
}
private void button5_Click(object sender, System.EventArgs e)
{
svm_model model = libsvm.svm.svm_load_model("c:\\a.txt");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -