Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#

Sequence Classifiers in C# - Part I: Hidden Markov Models

Rate me:
Please Sign up or sign in to vote.
4.95/5 (86 votes)
3 Dec 2014CPOL22 min read 327.1K   11.1K   155  
Let's understand hidden Markov models before taking a step into hidden conditional random fields.
This article aims to present the reader to the current workings of the Accord.NET Machine Learning Framework; show where the sequences classifiers are located within the framework, describe their source code, how the Markov namespace is organized and the general ideas behind this organization. This will also provide the base to talk about Hidden Conditional Random Fields, which is my main goal in this series.
// Accord.NET Sample Applications
// http://accord.googlecode.com
//
// Copyright © César Souza, 2009-2013
// cesarsouza at gmail.com
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

namespace Gestures
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;

    public partial class Canvas : UserControl
    {
        private bool capturing;
        private List<Point> sequence;



        public Canvas()
        {
            InitializeComponent();

            sequence = new List<Point>();
            this.DoubleBuffered = true;
        }

        public Point[] GetSequence()
        {
            return sequence.ToArray();
        }


        public void Clear()
        {
            sequence.Clear();
            this.Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {

            base.OnPaint(e);

            if (!this.DesignMode)
            {
                if (sequence.Count > 1)
                {
                    for (int i = 1; i < sequence.Count; i++)
                    {
                        int x = (int)sequence[i].X;
                        int y = (int)sequence[i].Y;
                        int p = (int)Accord.Math.Tools.Scale(0, sequence.Count, 0, 255, i);

                        int prevX = (int)sequence[i - 1].X;
                        int prevY = (int)sequence[i - 1].Y;
                        int prevP = (int)Accord.Math.Tools.Scale(0, sequence.Count, 0, 255, i - 1);

                        if (x == prevX && y == prevY)
                            continue;

                        Point start = new Point(prevX, prevY);
                        Point end = new Point(x, y);
                        Color colorStart = Color.FromArgb(255 - p, 0, p);
                        Color colorEnd = Color.FromArgb(255 - prevP, 0, prevP);

                        using (Brush brush = new LinearGradientBrush(start, end, colorStart, colorEnd))
                        using (Pen pen = new Pen(brush, 10))
                        {
                            pen.StartCap = LineCap.Round;
                            pen.EndCap = LineCap.Round;

                            e.Graphics.DrawLine(pen, prevX, prevY, x, y);
                        }
                    }
                }
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            Clear();

            capturing = true;

            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            capturing = false;

            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (capturing)
            {
                if (e.X > 0 && e.Y > 0)
                {
                    sequence.Add(new Point(e.X, e.Y));
                    this.Refresh();
                }
            }

            base.OnMouseMove(e);
        }

    }
}

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
Engineer NAVER LABS Europe
France France
Computer and technology enthusiast, interested in artificial intelligence and image processing. Has a Master's degree on Computer Science specialized on Image and Signal Processing, with expertise on Machine Learning, Computer Vision, Pattern Recognition and Data Mining systems. Author of the Accord.NET Framework for developing scientific computing applications.

If you would like to hire good developers to build your dream application, please check out DaitanGroup, one of the top outsourcing companies in Brazil. This company, located in Brazil's Sillicon Valley but with US-based offices, has huge experience developing telecommunications software for large and small companies worldwide.

Comments and Discussions