Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / Windows Forms

Contour Analysis for Image Recognition in C#

Rate me:
Please Sign up or sign in to vote.
4.97/5 (421 votes)
8 Jun 2014GPL325 min read 1.4M   112.3K   708  
The theory of contour analysis and its practical application to image recognition and OCR
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 Emgu.CV;
using ContourAnalysisNS;
using System.IO;

namespace ContourAnalysisDemo
{
    public partial class ShowContoursForm : Form
    {
        Templates templates;
        Templates samples;
        public Template selectedTemplate;
        Bitmap bmp;

        public ShowContoursForm(Templates templates, Templates samples,  IImage image)
        {
            if (image == null)
                return;
            InitializeComponent();
            this.templates = templates;
            this.samples = samples;
            
            this.samples = new Templates();
            foreach (var sample in samples)
                this.samples.Add(sample);

            dgvContours.RowCount = samples.Count;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);

            //some magic
            string fileName = Path.GetTempPath() + "\\temp.bmp";
            image.Save(fileName);
            bmp = (Bitmap)Image.FromFile(fileName);
        }

        private void dgvContours_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
            e.Handled = true;

            if (e.RowIndex < 0) return;

            Template template = samples[e.RowIndex];

            if (e.ColumnIndex == -1)
            {
                e.Graphics.DrawString(e.RowIndex.ToString(), Font, Brushes.Black, e.CellBounds.Location);
                return;
            }

            if (e.ColumnIndex == 0)
            {
                var rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, (e.CellBounds.Width - 24)/2, e.CellBounds.Height);
                rect.Inflate(-20, -20);
                Rectangle boundRect = template.contour.SourceBoundingRect;
                float k1 = 1f *  rect.Width / boundRect.Width;
                float k2 = 1f * rect.Height / boundRect.Height;
                float k = Math.Min(k1, k2);

                e.Graphics.DrawImage(bmp,
                    new Rectangle(rect.X, rect.Y, (int)(boundRect.Width * k), (int)(boundRect.Height * k)),
                    boundRect, GraphicsUnit.Pixel);
            }

            if (e.ColumnIndex == 0)
            {
                template.Draw(e.Graphics, e.CellBounds);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbTemplateName.Text == "<template name>")
                MessageBox.Show("Enter template name");
            else
            try
            {
                int i = dgvContours.SelectedCells[0].RowIndex;
                samples[i].name = tbTemplateName.Text;
                templates.Add(samples[i]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbTemplateName_Enter(object sender, EventArgs e)
        {
            tbTemplateName.ForeColor = Color.Black;
            if (tbTemplateName.Text == "<template name>")
                tbTemplateName.Text = "";
        }
    }
}

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 Freelancer
Ukraine Ukraine
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.

Comments and Discussions