📄 form1.cs
字号:
this.label8.Size = new System.Drawing.Size(184, 23);
this.label8.TabIndex = 6;
this.label8.Text = "Add noise to the patterns";
//
// trackBar3
//
this.trackBar3.Location = new System.Drawing.Point(184, 64);
this.trackBar3.Maximum = 100;
this.trackBar3.Name = "trackBar3";
this.trackBar3.Size = new System.Drawing.Size(176, 45);
this.trackBar3.TabIndex = 5;
this.trackBar3.TickFrequency = 5;
this.trackBar3.TickStyle = System.Windows.Forms.TickStyle.Both;
this.trackBar3.Scroll += new System.EventHandler(this.trackBar3_Scroll);
//
// button5
//
this.button5.Location = new System.Drawing.Point(16, 120);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(120, 24);
this.button5.TabIndex = 4;
this.button5.Text = "Load trained network";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(16, 88);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(120, 24);
this.button4.TabIndex = 3;
this.button4.Text = "Save trained network";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(16, 56);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 24);
this.button3.TabIndex = 2;
this.button3.Text = "Train the network";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// label3
//
this.label3.BackColor = System.Drawing.SystemColors.InactiveCaption;
this.label3.Dock = System.Windows.Forms.DockStyle.Top;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.ForeColor = System.Drawing.SystemColors.Window;
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(384, 32);
this.label3.TabIndex = 1;
this.label3.Text = " Step3: Train the network";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tabPage4
//
this.tabPage4.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label16,
this.label15,
this.label14,
this.label13,
this.label12,
this.label11,
this.label10,
this.trackBar4,
this.label4});
this.tabPage4.Location = new System.Drawing.Point(4, 22);
this.tabPage4.Name = "tabPage4";
this.tabPage4.Size = new System.Drawing.Size(384, 240);
this.tabPage4.TabIndex = 3;
this.tabPage4.Text = "Step 4";
//
// label16
//
this.label16.Dock = System.Windows.Forms.DockStyle.Top;
this.label16.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label16.Location = new System.Drawing.Point(0, 32);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(384, 16);
this.label16.TabIndex = 14;
this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label15
//
this.label15.Font = new System.Drawing.Font("Microsoft Sans Serif", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label15.Location = new System.Drawing.Point(264, 88);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(40, 48);
this.label15.TabIndex = 13;
this.label15.Text = "A";
//
// label14
//
this.label14.BackColor = System.Drawing.SystemColors.Window;
this.label14.Font = new System.Drawing.Font("Microsoft Sans Serif", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label14.Location = new System.Drawing.Point(80, 88);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(40, 48);
this.label14.TabIndex = 12;
this.label14.Text = "A";
//
// label13
//
this.label13.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label13.Location = new System.Drawing.Point(168, 104);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(44, 16);
this.label13.TabIndex = 11;
this.label13.Text = "------>";
//
// label12
//
this.label12.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label12.Location = new System.Drawing.Point(24, 56);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(328, 16);
this.label12.TabIndex = 10;
this.label12.Text = "You\'ve just entered Recognized";
//
// label11
//
this.label11.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label11.Location = new System.Drawing.Point(112, 208);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(168, 16);
this.label11.TabIndex = 9;
this.label11.Text = "Please enter a character";
//
// label10
//
this.label10.Location = new System.Drawing.Point(16, 160);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(128, 16);
this.label10.TabIndex = 8;
this.label10.Text = "Noise level (%)";
this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// trackBar4
//
this.trackBar4.Location = new System.Drawing.Point(144, 152);
this.trackBar4.Maximum = 100;
this.trackBar4.Name = "trackBar4";
this.trackBar4.Size = new System.Drawing.Size(176, 45);
this.trackBar4.TabIndex = 7;
this.trackBar4.TickFrequency = 5;
this.trackBar4.TickStyle = System.Windows.Forms.TickStyle.Both;
this.trackBar4.Scroll += new System.EventHandler(this.trackBar4_Scroll);
//
// label4
//
this.label4.BackColor = System.Drawing.SystemColors.InactiveCaption;
this.label4.Dock = System.Windows.Forms.DockStyle.Top;
this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label4.ForeColor = System.Drawing.SystemColors.Window;
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(384, 32);
this.label4.TabIndex = 1;
this.label4.Text = " Step4: Testing";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// openFileDialog1
//
this.openFileDialog1.DefaultExt = "neuro";
this.openFileDialog1.FileName = "OCRNetwork";
this.openFileDialog1.Filter = "Neural network (*.neuro)|*.neuro";
this.openFileDialog1.Title = "Load neural network";
//
// saveFileDialog1
//
this.saveFileDialog1.DefaultExt = "neuro";
this.saveFileDialog1.FileName = "OCRNetwork";
this.saveFileDialog1.Filter = "Neural network (*.neuro)|*.neuro";
this.saveFileDialog1.Title = "Store the network";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(592, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tabControl1,
this.pictureBox1});
this.Name = "Form1";
this.Text = "XPidea -=Simple OCR Demo (Backprop network RPROP algorithm)=-";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
this.tabPage3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.trackBar3)).EndInit();
this.tabPage4.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.trackBar4)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#endregion
public PatternsCollection CreateTrainingPatterns(Font font)
{
PatternsCollection result = new PatternsCollection(aCharsCount, aMatrixDim * aMatrixDim, aCharsCount);
for (int i= 0; i<aCharsCount; i++)
{
double[] aBitMatrix = CharToDoubleArray(Convert.ToChar(aFirstChar + i), font, aMatrixDim, 0);
for (int j = 0; j<aMatrixDim * aMatrixDim; j++)
result[i].Input[j] = aBitMatrix[j];
result[i].Output[i] = 1;
}
return result;
}
private void ShowNoise(Size sz, Graphics g,int noisePercent)
{
int range = sz.Height*sz.Width*noisePercent/200;
for (int i=0; i<range; i++)
{
int x = (int)OCRNetwork.Random(0,sz.Width);
int y = (int)OCRNetwork.Random(0,sz.Height);
Rectangle r = new Rectangle(x,y,0,0);
r.Inflate(1,1);
Brush b;
if ((OCRNetwork.Random(0,100))>80) //80% is black noise, 20% is white noise
b = new SolidBrush(Color.White);
else
b = new SolidBrush(Color.Black);
g.FillRectangle(b,r);
b.Dispose();
}
}
public double[] CharToDoubleArray(char aChar, Font aFont, int aArrayDim, int aAddNoisePercent)
{
double[] result = new double[aArrayDim*aArrayDim];
Graphics gr = label5.CreateGraphics();
Size size =Size.Round(gr.MeasureString(aChar.ToString(),aFont));
Bitmap aSrc = new Bitmap(size.Width,size.Height);
Graphics bmp = Graphics.FromImage(aSrc);
bmp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
bmp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
bmp.Clear(Color.White);
bmp.DrawString(aChar.ToString(),aFont,new SolidBrush(Color.Black),new Point(0,0),new StringFormat());
ShowNoise(size,bmp,aAddNoisePercent);
pictureBox1.Image = aSrc;
Application.DoEvents();
double xStep = (double)aSrc.Width/(double)aArrayDim;
double yStep = (double)aSrc.Height/(double)aArrayDim;
for (int i=0; i<aSrc.Width; i++)
for (int j=0;j<aSrc.Height;j++)
{
int x = (int)((i/xStep));
int y = (int)(j/yStep);
Color c = aSrc.GetPixel(i,j);
result[y*x+y]+=Math.Sqrt(c.R*c.R+c.B*c.B+c.G*c.G); //Convert to BW, I guess I can use B component of Alpha color space too...
}
return Scale(result);
}
private double MaxOf(double[] src)
{
double res=double.NegativeInfinity;
foreach (double d in src)
if (d>res) res = d;
return res;
}
private double[] Scale(double[] src)
{
double max = MaxOf(src);
if (max!=0)
{
for(int i=0; i<src.Length; i++)
src[i] = src[i]/max;
}
return src;
}
private void button1_Click(object sender, System.EventArgs e)
{
trainingPatterns = CreateTrainingPatterns(label5.Font);
tabControl1.SelectedTab = tabPage2;
}
private void button2_Click(object sender, System.EventArgs e)
{
backpropNetwork = new OCRNetwork(this, new int[3] {aMatrixDim * aMatrixDim, (aMatrixDim * aMatrixDim + aCharsCount)/2, aCharsCount});
tabControl1.SelectedTab = tabPage3;
}
private void button3_Click(object sender, System.EventArgs e)
{
if (backpropNetwork == null)
{
MessageBox.Show("Please create the network first!");
return;
}
if (trainingPatterns == null)
{
MessageBox.Show("Please create training patterns on STEP 1");
return;
}
progressBar1.Maximum = aCharsCount;
label9.Text = "While the network is training you can proceed to the STEP 4 and observe how the recognition quality progress during the training.";
backpropNetwork.Train(trainingPatterns);
MessageBox.Show("Network training successfully complete!");
}
private void tabControl1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (tabControl1.SelectedTab == tabPage4)
{
if (backpropNetwork == null)
{
MessageBox.Show("Please go to STEP 2 and create the network.");
return;
}
label14.Text = e.KeyChar.ToString();
double[] aInput = CharToDoubleArray(e.KeyChar, label5.Font, aMatrixDim, trackBar4.Value);
for (int i = 0; i< backpropNetwork.InputNodesCount;i++)
backpropNetwork.InputNode(i).Value = aInput[i];
backpropNetwork.Run();
label15.Text= Convert.ToChar(aFirstChar + backpropNetwork.BestNodeIndex).ToString();
}
}
private void button4_Click(object sender, System.EventArgs e)
{
if (backpropNetwork == null)
{
MessageBox.Show("Network is not yet created.");
return;
}
if (saveFileDialog1.ShowDialog()== DialogResult.OK)
backpropNetwork.SaveToFile(saveFileDialog1.FileName);
}
private void button5_Click(object sender, System.EventArgs e)
{
if (backpropNetwork == null)
{
MessageBox.Show("Network is not yet created.");
return;
}
if (openFileDialog1.ShowDialog()== DialogResult.OK)
backpropNetwork.LoadFromFile(openFileDialog1.FileName);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Form1.IsTerminated=true;
}
private void trackBar3_Scroll(object sender, System.EventArgs e)
{
this.label8.Text = "Add noise to the patterns ( "+trackBar3.Value.ToString()+"% )";
}
private void trackBar4_Scroll(object sender, System.EventArgs e)
{
this.label10.Text = "Noise level ( "+trackBar4.Value.ToString()+"% )";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -