Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#

Neural Network OCR

Rate me:
Please Sign up or sign in to vote.
4.91/5 (155 votes)
11 Aug 2005GPL310 min read 1.1M   46.3K   388  
Some ideas about optical character recognition using neural networks.
namespace NeuroOCR
{
	using System;
	using System.ComponentModel;
	
	using SourceGrid2.Cells.Virtual;

	/// <summary>
	/// Summary description for GridIntArray.
	/// </summary>
	public class GridArray : SourceGrid2.GridVirtual
	{
		private CellVirtual	columnHeader;
		private CellVirtual	rowHeader;
		private CellVirtual	cellHeader;
		private CellVirtual	dataCell;
		private Array		array;
		private bool		readOnly = false;

		// Readonly property
		[DefaultValue(false)]
		public bool ReadOnly
		{
			get { return readOnly; }
			set
			{
				readOnly = value;
				RefreshCellsStyle();
			}
		}

		// Constructor
		public GridArray()
		{
		}

		// Load data
		public void LoadData(Array array)
		{
			//
			this.array = array;

			// set column and row headers
			FixedRows = 1;
			FixedColumns = 1;

			// Redim the grid
			Redim(array.GetLength(0) + FixedRows, array.GetLength(1) + FixedColumns);

			// Col Header Cell Template
			columnHeader = new CellColumnHeaderTemplate();
			columnHeader.BindToGrid(this);

			// Row Header Cell Template
			rowHeader = new CellRowHeaderTemplate();
			rowHeader.BindToGrid(this);

			// Header Cell Template (0,0 cell)
			cellHeader = new CellHeaderTemplate();
			cellHeader.BindToGrid(this);

			// Data Cell Template
			dataCell = new CellArrayTemplate(array);;
			dataCell.BindToGrid(this);

			RefreshCellsStyle();
		}

		// Return the Cell at the specified Row and Col position
		public override SourceGrid2.Cells.ICellVirtual GetCell(int row, int col)
		{
			try
			{
				if (array != null)
				{
					if ((row < FixedRows) && (col < FixedColumns))
						return cellHeader;
					else if (row < FixedRows)
						return columnHeader;
					else if (col < FixedColumns)
						return rowHeader;
					else
						return dataCell;
				}
				else
					return null;
			}
			catch(Exception)
			{
				return null;
			}		
		}

		// Set the specified cell int he specified position
		public override void SetCell(int row, int col, SourceGrid2.Cells.ICellVirtual cell)
		{
			throw new ApplicationException("Cannot set cell for this kind of grid");
		}

		// Refresh cells style
		private void RefreshCellsStyle()
		{
			if (dataCell != null)
			{
				dataCell.DataModel.EnableEdit = !readOnly;
			}
		}


		// Column header template
		private class CellColumnHeaderTemplate : SourceGrid2.Cells.Virtual.ColumnHeader
		{
			// Get the value of the cell at the specified position 
			public override object GetValue(SourceGrid2.Position position)
			{
				return position.Column - Grid.FixedColumns;
			}
			// Set the value of the cell at the specified position
			public override void SetValue(SourceGrid2.Position position, object val)
			{
				throw new ApplicationException("Cannot change this kind of cell");
			}
			// Get sort status
			public override SourceGrid2.SortStatus GetSortStatus(SourceGrid2.Position position)
			{
				return new SourceGrid2.SortStatus (SourceGrid2.GridSortMode.None, false);
			}
			// Set sort status
			public override void SetSortMode(SourceGrid2.Position position, SourceGrid2.GridSortMode mode)
			{
			}		
		}

		// Row header template
		private class CellRowHeaderTemplate : SourceGrid2.Cells.Virtual.RowHeader
		{	
			// Get the value of the cell at the specified position
			public override object GetValue(SourceGrid2.Position position)
			{
				return position.Row - Grid.FixedRows;
			}
			// Set the value of the cell at the specified position
			public override void SetValue(SourceGrid2.Position position, object val)
			{
				throw new ApplicationException("Cannot change this kind of cell");
			}		
		}

		// Cell header template
		private class CellHeaderTemplate : SourceGrid2.Cells.Virtual.Header
		{
			// Get the value of the cell at the specified position
			public override object GetValue(SourceGrid2.Position position)
			{
				return null;
			}
			// Set the value of the cell at the specified position
			public override void SetValue(SourceGrid2.Position position, object val)
			{
				throw new ApplicationException("Cannot change this kind of cell");
			}		
		}

		// Cell temlate
		public class CellArrayTemplate : SourceGrid2.Cells.Virtual.CellVirtual
		{
			private Array array;

			// Constructor
			public CellArrayTemplate(Array array)
			{
				this.array = array;
				DataModel = SourceGrid2.Utility.CreateDataModel(array.GetType().GetElementType());
			}
			// Get the value of the cell at the specified position
			public override object GetValue(SourceGrid2.Position position)
			{
				return array.GetValue(position.Row - Grid.FixedRows, position.Column - Grid.FixedColumns);
			}
			// Set the value of the cell at the specified position
			public override void SetValue(SourceGrid2.Position position, object val)
			{
				array.SetValue(val, position.Row - Grid.FixedRows, position.Column-Grid.FixedColumns);
				OnValueChanged(new SourceGrid2.PositionEventArgs(position, this));
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET, Computer Vision Sandbox, cam2web, ANNT, etc.

Comments and Discussions