Optical Character Recognition

Introduction:

This program will allow to train the artificial neuron so that to make them able to recognize different images of characters. It is done by using Back Propagation Algorithm a technique of Artificial intelligence.

Back Propagation Algorithm:

There are three layers of artificial neurons. They are input layer, hidden layer and output layer.

Propagates inputs forward in the usual way, i.e.

  • All outputs are computed using sigmoid thresholding of the inner product of the corresponding weight and input vectors.
  • All outputs at stage n are connected to all the inputs at stage n+1
  • Propagates the errors backwards by apportioning them to each unit according to the amount of this error the unit is responsible for.

Capture

Overall Process:

Load Image: 

In this process image is loaded from the folder containing images and after that getting its pixels and get input neurons and output neurons

After loading the image, training process is started in which number of iterations works and error become closer to minimum error. After reaching to minimum level, iteration process stops.

Recognition:

This process image is loaded from the folder containing images and matched with the image which is being trained.

asa

Working:

For example, we have number of characters in a folder and by clicking on load button we load a number of characters placing in a folder, loading characters will show in first picture box. After loading function training of neurons will star t .It will take some iterations and some time to train. In some number of iteration and   in a fewer time training will be done. Training will be stop when the error  will be less than  the error defined. Now by clicking on recognize button you may select any character image which you want to recognize from the program and by selecting you will see that the same character image will be displayed in the first picture box in response to the placed image in the second picture box.

aksnjdkand

 Code:

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.Collections.Specialized;
 using System.Configuration;
 using System.IO;
 using System.Threading;
 //using NeuronDotNet.Controls;
namespace CharacterRecogAlgo
 {
  public partial class Form1 : Form
  {
  int ImageX;
  int ImageY;
  string[] images;
  public Dictionary<string, double[]> dict;
  CharacterRecogAlgo.backpropagation back;
  OpenFileDialog openfile = new OpenFileDialog();
public Form1()
  {
  InitializeComponent();
  }
public void findInputNeurons()
  {
  NameValueCollection AppSettings = ConfigurationManager.AppSettings;
  openfile.InitialDirectory = Path.GetFullPath("PATTERNS");
  openfile.ShowDialog();
try
  {
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
  images = Directory.GetFiles(openfile.InitialDirectory.ToString(), "*.bmp");
  foreach (string st in images)
  {
  Bitmap img = new Bitmap(st);
  pictureBox1.Image = img;
  ImageX += img.Width;
  ImageY += img.Height;
  Application.DoEvents();
  Thread.Sleep(150);
  img.Dispose();
  }
  ImageX = ImageX / images.Length;
  ImageY = ImageY / images.Length;
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message.ToString());
  }
}
private void load_image_Click(object sender, EventArgs e)
  {
  findInputNeurons();
  dict = new Dictionary<string,double[]>();
  foreach(string st in images)
  { 
  Bitmap imgbit = new Bitmap(st);
  double[] tempArr = new double[ImageX * ImageY];
  tempArr = Tobinary(imgbit);
  dict.Add(Path.GetFileNameWithoutExtension(st), tempArr);
  imgbit.Dispose();
  }
  textBox1.Text = (ImageX * ImageY).ToString();
  output.Text = dict.Count.ToString();
}
  public double[] Tobinary(Bitmap imgbit)
  {
  double heightY = (double) ImageY / imgbit.Height;
  double widthX = (double) ImageX / imgbit.Width;
  double[] linearArr = new double[ImageX * ImageY];
  for (int i = 0; i < ImageY; i++)
  {
  for (int j = 0; j < ImageX; j++)
  {
  Color cr = imgbit.GetPixel((int)(j / widthX), (int)(i / heightY));
  linearArr[i * ImageX + j] = 1 - (double)((cr.R + cr.G + cr.B) / 3) / 255;
  }
  }
  return linearArr;
  }
private void train_image_Click(object sender, EventArgs e)
  {
  back = new CharacterRecogAlgo.backpropagation(ImageX * ImageY, int.Parse(hidden.Text), dict.Count, dict, double.Parse(rate.Text), int.Parse(iteration.Text), double.Parse(min.Text));
  back.Learning(dict, geterr, getitr);
  }
private void recog_image_Click(object sender, EventArgs e)
  {
  try
  {
NameValueCollection AppSettings = ConfigurationManager.AppSettings;
  openfile.InitialDirectory = Path.GetFullPath("INPUT PATTERNS");
  openfile.ShowDialog();
  Bitmap imp = new Bitmap(openfile.FileName);
  pictureBox1.Image = imp;
  pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
int max = back.testalgo(Tobinary(imp));
  string maxIndex = images[max];
  Bitmap match = new Bitmap(maxIndex);
  pictureBox2.Image = match;
  pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
  }
  }
 }