Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / C#

Image Recognition with Neural Networks

Rate me:
Please Sign up or sign in to vote.
4.82/5 (89 votes)
30 Oct 2007CPOL4 min read 943.8K   46.2K   286  
This article contains a brief description of BackPropagation Artificial Neural Network and its implementation for Image Recognition
#region Copyright (c), Some Rights Reserved
/*##########################################################################
 * 
 * DrawingPanel.cs
 * -------------------------------------------------------------------------
 * By
 * Murat FIRAT, September 2007
 * 
 * -------------------------------------------------------------------------
 * Description:
 * Inherited From Panel, The Control Provides Drawing Characters On It.. 
 * 
 * -------------------------------------------------------------------------
 ###########################################################################*/
#endregion

using System;
using System.Windows.Forms;
using System.Drawing;

namespace BPSimplified
{
    //A Custom Panel To Draw Characters
    class DrawingPanel : Panel
    {
        private Bitmap BufferImage = null;
        private bool MouseMoving = false;

        private Graphics g_Image = null;
        private Graphics g_Panel = null;

        private int p_Size = 10;//Default Point Size

        public DrawingPanel()
        {
            InitializeDrawingPanel();
        }

        private void InitializeDrawingPanel()
        {
            CreateNewImage();
            base.MouseDown += new MouseEventHandler(DrawingPanel_MouseDown);
            base.MouseUp += new MouseEventHandler(DrawingPanel_MouseUp);
            base.MouseMove += new MouseEventHandler(DrawingPanel_MouseMove);
            base.Paint += new PaintEventHandler(DrawingPanel_Paint);
            base.Resize += new EventHandler(DrawingPanel_Resize);
            base.BackColor = Color.White;
        }

        void DrawingPanel_Resize(object sender, EventArgs e)
        {
            CreateNewImage();
        }

        void DrawingPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(BufferImage, 0, 0);
            g.Dispose();
        }

        void DrawingPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseMoving &&
                (e.X < (base.Width - p_Size * .85)) && (e.Y < (base.Height - p_Size * .85)) &&
                (e.X > -p_Size * .15) && (e.Y > -p_Size * .15))//To Prevent Drawing Over The Panel 
            {
                //Draw The Point to The Image
                g_Image = Graphics.FromImage(BufferImage);
                g_Image.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, p_Size, p_Size);

                //Draw The Image to The Panel
                g_Panel = base.CreateGraphics();
                g_Panel.DrawImage(BufferImage, 0, 0);
            }
        }

        void DrawingPanel_MouseUp(object sender, MouseEventArgs e)
        {
            MouseMoving = false;
        }

        void DrawingPanel_MouseDown(object sender, MouseEventArgs e)
        {
            MouseMoving = true;
        }

        public void Clear()
        {
            CreateNewImage();        
            base.Invalidate();
        }

        private void CreateNewImage()
        {
            BufferImage = new Bitmap(base.Width, base.Height);

            for (int i = 0; i < BufferImage.Height; i++)
                for (int j = 0; j < BufferImage.Width; j++)
                    BufferImage.SetPixel(j, i, Color.White);
        }

        protected override void Dispose(bool disposing)
        {
            //Release Resources
            if (g_Image != null) g_Image.Dispose();
            if (g_Panel != null) g_Panel.Dispose();
            if (BufferImage != null) BufferImage.Dispose();

            base.Dispose(disposing);
        }

        public Bitmap ImageOnPanel
        {
            get
            {                
                return BufferImage;
            }
            set
            {
                BufferImage = value;
                base.Invalidate();
            }
        }

        public int PointSize
        {
            get { return p_Size; }
            set { p_Size = value; }
        }
    }
}

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
Software Developer (Senior)
Turkey Turkey
Has BS degree on computer science, working as software engineer in istanbul.

Comments and Discussions