⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 水平垂直投影 form1.cs

📁 支持向量机程序,非常有用,可以供大家实验使用,改进.希望能多大家工作有帮助.
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using DataBaseDeal;
using libsvm;

namespace MyOcr
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.PictureBox pic;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		

		svm_model model;

		bool IsDraw=false;
		int drwx=0,drwy=0;
		private System.Windows.Forms.Button button5;


		
		class ltWord 
		{
			public ltWord()
			{				
				Czty=new double[20];	
				value=0;
			}
			public double[] Czty ;
			public byte value;

		}

		double GetIsWordFromRange(Bitmap bitmap,int left,int top ,int right,int down)
		{
			double jsCount=0;
			for(int i=left;i< right;i++)
			{
				for(int j=top;j<down;j++)
				{
					if(bitmap.GetPixel(i,j).G!=255)
					{
						//MessageBox.Show(bitmap.GetPixel(i,j).ToString());
						jsCount++;
					}
					
					
				}				
			}
			double dddd=jsCount/(16*16);
			if(jsCount>40) 
				dddd=1;
		   else
			   dddd=0;

			//MessageBox.Show(dddd.ToString());
			return dddd;
		}

		ltWord GetWordFromBitmap(Bitmap bitmap)
		{
			ltWord tempWord=new ltWord();			
			for(int i=0;i<10;i++)
			{
				double ltCZTYV=0;
				double ltCZTYH=0;
				for(int j=0;j<10;j++)
				{
					ltCZTYV+=GetIsWordFromRange(bitmap,i*16,j*16,(i+1)*16,(j+1)*16);
					ltCZTYH+=GetIsWordFromRange(bitmap,j*16,i*16,(j+1)*16,(i+1)*16);
					
				}
				tempWord.Czty[i]=ltCZTYV;
				tempWord.Czty[i+10]=ltCZTYH;
				//MessageBox.Show(tempWord.Czty[i].ToString());
			}
			//bitmap.Save("c:\\aa.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
			return tempWord;

		}



		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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.pic = new System.Windows.Forms.PictureBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// pic
			// 
			this.pic.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.pic.Image = ((System.Drawing.Image)(resources.GetObject("pic.Image")));
			this.pic.Location = new System.Drawing.Point(24, 24);
			this.pic.Name = "pic";
			this.pic.Size = new System.Drawing.Size(160, 160);
			this.pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pic.TabIndex = 0;
			this.pic.TabStop = false;
			this.pic.Click += new System.EventHandler(this.pic_Click);
			this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
			this.pic.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pic_MouseMove);
			this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(24, 232);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(272, 56);
			this.button1.Name = "button1";
			this.button1.TabIndex = 2;
			this.button1.Text = "Clear";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(272, 104);
			this.button2.Name = "button2";
			this.button2.TabIndex = 3;
			this.button2.Text = "识别";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(152, 232);
			this.button3.Name = "button3";
			this.button3.TabIndex = 4;
			this.button3.Text = "训练";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(248, 232);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(88, 23);
			this.button4.TabIndex = 5;
			this.button4.Text = "清除所有训练";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(272, 144);
			this.button5.Name = "button5";
			this.button5.TabIndex = 6;
			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(376, 317);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.pic);
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "Form1";
			this.TopMost = true;
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{		
			//Bitmap bitmap=new Bitmap(pic.Image);
			//MessageBox.Show(bitmap.GetPixel(e.X,e.Y).ToString());
			IsDraw=(e.Button==MouseButtons.Left);
			if(IsDraw==true)
			{
				drwx=e.X;
				drwy=e.Y;
			}
		}

		private void pic_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(IsDraw==true)
			{
				Pen myPen = new Pen(Color.Red);	
				// Bitmap ddd =new Bitmap(pic.Image);
				IntPtr hwnd = pic.Handle; 
				Graphics g = Graphics.FromImage(pic.Image);				
				myPen.Width =10;
				g.DrawLine(myPen,new Point(drwx,drwy),new Point(e.X,e.Y));
				drwx=e.X;
				drwy=e.Y;			
				g=Graphics.FromHwnd(hwnd);
				g.DrawImageUnscaled(pic.Image,new Point(0,0));
				g.Dispose();
			}
		}

		private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			IsDraw=false;			
			drwx=0;
			drwy=0;
			
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{			
			//model = svm.svm_load_model("c:\\aa.txt");
			DataBase.StrConn="Provider=sqloledb;Data Source=.;Initial Catalog=Test;User Id=sa;Password=830701";
		}

		private void pic_Click(object sender, System.EventArgs e)
		{
		
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			ltWord tttt=GetWordFromBitmap(new Bitmap(pic.Image));
			svm_node[] x = new svm_node[20];
			for(int i=0;i<20;i++)
			{
				x[i] = new svm_node();
				x[i].index=i+1;
				x[i].value_Renamed=tttt.Czty[i];
				
			}
			double d = svm.svm_predict(model, x);
			MessageBox.Show(d.ToString());
			
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//pic.Image.Save("c:\\123.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
			IntPtr hwnd = pic.Handle; 
			Graphics g = Graphics.FromHwnd(hwnd); 		
			g.Clear(Color.White);
			g = Graphics.FromImage(pic.Image);	
			g.Clear(Color.White);
			g.Dispose();
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			try
			{
				int num=int.Parse(textBox1.Text);
				ltWord tttt=GetWordFromBitmap(new Bitmap(pic.Image));
				DataBase.ExeSqlNoQue("insert svm_v(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,num) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
					tttt.Czty[0],tttt.Czty[1],tttt.Czty[2],tttt.Czty[3],tttt.Czty[4],tttt.Czty[5],tttt.Czty[6],tttt.Czty[7],tttt.Czty[8],tttt.Czty[9],tttt.Czty[10],tttt.Czty[11],tttt.Czty[12],tttt.Czty[13],tttt.Czty[14],tttt.Czty[15],tttt.Czty[16],tttt.Czty[17],tttt.Czty[18],tttt.Czty[19],num);
				MessageBox.Show("训练成功");				
			}
			catch
			{
				MessageBox.Show("训练失败!");
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			//new Bitmap(
		
		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			
			DataTable MySvm=DataBase.ExeSqlToTable("select * from svm_v");
			if(MySvm.Rows.Count>0)
			{

				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 =500;
				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];
				

				// build problem
				svm_problem prob = new svm_problem();
				prob.l = MySvm.Rows.Count;
				prob.y = new double[prob.l];
			
				
				if(param.gamma == 0) param.gamma = 0.5;
				prob.x = new svm_node [prob.l][];
				MessageBox.Show(prob.l.ToString());
				for(int i=0;i<prob.l;i++)
				{
					
					prob.x[i] = new svm_node[20];
					for(int j=1;j<=20;j++)
					{
						double V_v=double.Parse(MySvm.Rows[i]["v"+(j).ToString()].ToString());
						prob.x[i][j-1] = new svm_node();
						prob.x[i][j-1].index = j+1;
						prob.x[i][j-1].value_Renamed =V_v;						
				
					}					
					prob.y[i] =double.Parse(MySvm.Rows[i]["num"].ToString());
				}			
				model = svm.svm_train(prob, param);				
				svm.svm_save_model("c:\\aa.txt",model);
			}
			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -