You are here: Start » FIL.NET » FabImage Library .NET Usage » Basic Template Matching Example
Basic Template Matching Example
Following example creates a reusable EdgeModel object with the CreateEdgeModel1 function that is used to locate some objects in the loaded image. Locating objects is performed with LocateMultipleObjects_Edges1 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 Fil; //... public partial class Form1 : Form { readonly Ftl.Conditional<EdgeModel> model; readonly DrawingStyle style; readonly Disposables disposables; public Form1() { InitializeComponent(); disposables = new(); model = new(); style = new DrawingStyle() { Thickness = 2.0f, Opacity = 1.0f }; model.DisposeWith(disposables); style.DisposeWith(disposables); CreateModel(); MatchModel(); } private void CreateModel() { using var modelImage = new Image(); Fil.Invoke.LoadImage("Images/mount.jpg", false, modelImage); Fil.Invoke.CreateEdgeModel1( inImage: modelImage, inTemplateRegion: null, inReferenceFrame: null, inMinPyramidLevel: 0, inMaxPyramidLevel: null, inSmoothingStdDev: 0.0f, inEdgeThreshold: 35.0f, inEdgeHysteresis: 15.0f, inMinAngle: 0.0f, inMaxAngle: 360.0f, inAnglePrecision: 1.0f, inMinScale: 1.0f, inMaxScale: 1.0f, inScalePrecision: 1.0f, inEdgeCompleteness: 1.0f, outEdgeModel: model); } private void MatchModel() { using var inspectingImage = new Image(); List<Object2D> matches = []; Fil.Invoke.LoadImage("Images/mounts.jpg", false, inspectingImage); if (model.HasValue) { Fil.Invoke.LocateMultipleObjects_Edges1( inImage: inspectingImage, inSearchRegion: null, inSearchRegionAlignment: null, inEdgeModel: model.Value, inMinPyramidLevel: 0, inMaxPyramidLevel: null, inEdgeThreshold: 10.0f, inEdgePolarityMode: EdgePolarityMode.MatchStrictly, inEdgeNoiseLevel: EdgeNoiseLevel.High, inIgnoreBoundaryObjects: false, inMinScore: 0.8f, inMinDistance: 20.0f, outObjects: matches); } if (matches.Count > 0) { foreach (var match in matches) { Fil.Invoke.DrawCircle( ioImage: inspectingImage, inCircle: new Circle2D(match.Point, (match.Match.Width + match.Match.Height) / 2.5f), inCircleAlignment: null, inColor: Pixel.Green, inDrawingStyle: style); } } else { Fil.Invoke.DrawString( ioImage: inspectingImage, inString: "No object found", inLocation: new Location(inspectingImage.Width / 2, inspectingImage.Height / 2), inLocationAlignment: null, inLocationAnchor: Anchor2D.MiddleCenter, inColor: Pixel.Red, inDrawingStyle: style, inSize: 40.0f, inOrientationAngle: 0.0f, inBackgroundColor: null); } pictureBox1.Image?.Dispose(); pictureBox1.Image = inspectingImage.ToBitmap(); } //... }
Previous: Basic Image Thresholding | Next: Blob Analysis |