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 DrawCircle. 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 AvlNet;
using System.Linq;
    //...

    public partial class Form1 : Form
    {
        SafeNullableRef<EdgeModel> model = Nullable.CreateSafe<EdgeModel>();
        DrawingStyle style;

        public Form1()
        {
            InitializeComponent();

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

        private void CreateModel()
        {
            using (var modelImage = new Image())
            {
                AVL.LoadImage("mount.jpg", false, 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, model);
            }
        }

        private void MatchModel()
        {
            using (var inspectingImage = new Image())
            {
                List<Object2D> matches = new List<Object2D>();

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

                if (model.HasValue)
                {
                    AVL.LocateMultipleObjects_Edges(
                        inspectingImage,
                        model.Value,
                        0, 10.0f,
                        EdgePolarityMode.MatchStrictly,
                        EdgeNoiseLevel.High,
                        false, 0.8f, 20.0f,
                        matches);
                }
                if (matches.Any())
                {
                    foreach (var match in matches)
                    {
                        AVL.DrawCircle(
                            inspectingImage,
                            new Circle2D(match.Point, (match.Match.Width + match.Match.Height) / 2.5f),
                            Pixel.Green,
                            style);
                    }
                }
                else
                {
                    AVL.DrawString(
                        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);
                }

                if (pictureBox1.Image != null)
                    pictureBox1.Image.Dispose();

                pictureBox1.Image = inspectingImage.CreateBitmap();
            }
        }
        //...        
    }
Previous: Basic Image Thresholding Next: Blob Analysis