Click here to Skip to main content
15,891,951 members
Articles / Artificial Intelligence / Neural Networks

Multiple convolution neural networks approach for online handwriting recognition

Rate me:
Please Sign up or sign in to vote.
4.95/5 (37 votes)
9 Apr 2013CPOL8 min read 76.6K   25.1K   74  
The research focuses on the presentation of word recognition technique for an online handwriting recognition system which uses multiple component neural networks (MCNN) as the exchangeable parts of the classifier.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace UPImage
{
    public class InputPattern:IDisposable
    {
        Bitmap originalBmp;
        Rectangle originalRect;
        List<Rectangle> patternRects;
        List<Color> colorList;
        int background;
        public Bitmap OriginalBmp
        {
            get
            {
                return originalBmp;
            }
            set
            {
                if (originalBmp == value)
                    return;
                originalBmp = value;
            }
        }
        public Rectangle OriginalRectangle
        {
            get
            {
                return originalRect;
            }
            set
            {
                if (originalRect == value)
                    return;
                originalRect = value;
            }
        }
        public List<Rectangle> PatternRects
        {
            get
            {
                return patternRects;
            }
            set
            {
                if (patternRects == value)
                    return;
                patternRects = value;
            }
        }
        public int Background
        {
            get
            {
                return background;
            }
            set
            {
                if (background == value)
                    return;
                background = value;
            }
        }
        public InputPattern(Bitmap original, int backgroundColor,Rectangle rect)
        {
            originalBmp = original;
            background = backgroundColor;
            originalRect = rect;
            patternRects = null;
            colorList = null;
            InitColorList();
        }
        public void Dispose()
        {
            if (patternRects != null)
            {
                patternRects.Clear();
                patternRects = null;
            }
            if (colorList != null)
            {
                colorList.Clear();
                colorList = null;
            }
            originalBmp.Dispose();
            
        }
        public void GetPatternBoundaries(int hStep, int vStep, bool bTopStart, int widthMin, int heightMin)
        {
            Bitmap bmpTemp;

            if (ImageProcessing.IsGrayscale(originalBmp))
            {
                bmpTemp =(Bitmap) originalBmp.Clone();
            }
            else
            {
                //change to indexed grayscale bitmap
                bmpTemp = ImageProcessing.ColorToIndexedGrayscale(originalBmp);
            }
            if (patternRects != null)
            {
                patternRects.Clear();
                patternRects = null;
            }
            
            List<Rectangle> rectangles = new List<Rectangle>();
            while (true)
            {
                Rectangle newrectange = ImageProcessing.PatternRectangeBoundary(bmpTemp, background, hStep, vStep, bTopStart);

                //fill rectange with index color
                bmpTemp = ImageProcessing.FillColor(bmpTemp, newrectange, background);
                if ((newrectange.Width >= widthMin) && (newrectange.Height >= heightMin))
                {

                    rectangles.Add(newrectange);
                }
                if(newrectange.Width== 0 || newrectange.Height==0)
                {
                    break;
                }
            }
            bmpTemp.Dispose();
            patternRects= rectangles;
        }
        public Bitmap DrawPatternBoundary(Bitmap orginal,Color color)
        {
            Bitmap bmp=null;
            if (ImageProcessing.IsGrayscale(orginal))
            {
                bmp = ImageProcessing.CreateNoneIndexedImage(orginal);
            }
            else
            {
                bmp = (Bitmap)orginal.Clone();
            }
            if (originalRect != null)
            {
                Graphics graphicGraph = Graphics.FromImage(bmp);
                SolidBrush brush = new SolidBrush(color);
                Pen colorPen = new Pen(brush, 1);
                graphicGraph.DrawRectangle(colorPen, originalRect);
                
            }
            return bmp;
        }
        /// <summary>
        /// Draw Pattern Rectangle boundary to orginal bitmap
        /// </summary>
        /// <param name="orginal"></param>
        /// <returns></returns>
        public Bitmap DrawChildPatternBoundaries(Bitmap orginal)
        {
            Bitmap bmpTemp;
            if (orginal == null)
            {
                bmpTemp = originalBmp;
            }
            else
            {
                bmpTemp = orginal;
            }
            Bitmap bmp = null;
            if (ImageProcessing.IsGrayscale(bmpTemp))
            {
                bmp = ImageProcessing.CreateNoneIndexedImage(bmpTemp);
            }
            else
            {
                bmp = (Bitmap)bmpTemp.Clone();
            }
            if (patternRects != null)
            {
                int iColor = 0;

                foreach (var rect in patternRects)
                {
                    Graphics graphicGraph = Graphics.FromImage(bmp);
                    SolidBrush brush = new SolidBrush(colorList[iColor]);
                    Pen colorPen = new Pen(brush, 1);
                    int x = originalRect.X+rect.X;
                    int y = originalRect.Y+rect.Y;
                    Rectangle newRect = new Rectangle(x, y, rect.Width, rect.Height);
                    graphicGraph.DrawRectangle(colorPen, newRect);
                    iColor++;
                    if (iColor >= colorList.Count)
                    {
                        iColor = 0;
                    }
                }
            }
            
            return bmp;
        }
        private void InitColorList()
        {
            colorList = new List<Color>();
            foreach (string colorName in Enum.GetNames(typeof(System.Drawing.KnownColor)))
            {
                //Check if color is dark 
                if (colorName.StartsWith("D") == true)
                {
                    colorList.Add(System.Drawing.Color.FromName(colorName));
                }
            }
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Vietnam Maritime University
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions