You are here: Start » AVL.NET » Adaptive Vision Library .NET Usage » Basic Template Matching Example

Basic Template Matching Example

Following example creates a reusable EdgeModel object with the CreateEdgeModel function that is used to locate some objects in the loaded image. Locating objects is performed with LocateMultipleObjects_Edges function and the found bounding rectangles used to calculate bounding circles, which are drawn into the loaded image with DrawCircles_Palette. If no object is found, appropriate message is drawn with DrawString function.

To have the example work, a single form with PictureBox control is needed.


//...
using System.Linq;

using AvlNet;
    //...

    public partial class Form1 : Form
    {
        EdgeModel model = null;
        DrawingStyle style = null;

        public Form1()
        {
            InitializeComponent();

            style = new DrawingStyle() { Thickness = 2.0f, Opacity = 1.0f };
            CreateModel();
            MatchModel();
        }

        
        private void CreateModel()
        {
            Image modelImage;
            AVL.LoadImage("mount.jpg", false, out modelImage);
            AVL.CreateEdgeModel(modelImage, 0, 0.0f, 35.0f, 15.0f, 0.0f, 360.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, out model);
            modelImage.Dispose();
        }

        private void MatchModel()
        {
            Image inspectingImage;
            Object2D[] matches;

            AVL.LoadImage("mounts.jpg", false, out inspectingImage);

            AVL.LocateMultipleObjects_Edges(inspectingImage, model, 0, null, 10.0f, false, false, 0.8f, 20.0f, out matches);
            if (matches.Length > 0)
            {
                //extract rectangles from the found matches
                Rectangle2D[] rectangles = matches.Select<Object2D, Rectangle2D>(obj => obj.Match).ToArray();

                Circle2D?[] boundingCircles = new Circle2D?[rectangles.Length];
                
                //get bounding circles for the matched rectangles
                for (int i = 0; i < rectangles.Length; i++)
                {
                    Path rectPath;
                    AVL.CreateRectanglePath(rectangles[i], out rectPath);
                    Circle2D boundingCircle;
                    AVL.PathBoundingCircle(rectPath, out boundingCircle);
                    boundingCircles[i] = boundingCircle;
                }

                for (int i = 0; i < boundingCircles.Length; i++)
                {
                    if (boundingCircles[i].HasValue)
                        AVL.DrawCircle(
                            ref inspectingImage,
                            boundingCircles[i].Value,
                            new Pixel(0.0f, 255.0f, 0.0f, 255.0f), style);
                }
            }
            else
            {
                AVL.DrawString(
                    ref inspectingImage,
                    "No object found",
                    new Location(inspectingImage.Width / 2, inspectingImage.Height / 2),
					Anchor2D.MiddleCenter, 
                    new Pixel(255.0f, 0.0f, 0.0f, 255.0f),
                    style, 40.0f, 0.0f);
            }

            pictureBox1.Image = inspectingImage.CreateBitmap();

            inspectingImage.Dispose();
        }

        //...


Previous: Basic Image Thresholding Next: Blob Analysis