You are here: Start » Program Examples » Dynamic Template Matching
Dynamic Template Matching
Aim
The aim of this program is to create a dynamic template for matching purposes.
Input
Image from Web Camera.
Output
Rectangle-shaped match to the object in the image. In this example we used an image from an industrial camera, however the example is meant to be for online tests. It depends on user's intention what exactly should be detected.
Hints
To create such an application we need filters to grab a frame from Web Camera and Template Matching filters.
Solution (AVS)
- Create a MainLoop macrofilter working in a loop, get in and right-click on the macrofilter and add register. Name it Model of type EdgeModel.
- Add WebCamera_GrabImage filter.
- Add ResizeImage filter and connect outFrame from previous filter with inImage input.
- Set inNewWidth to 640 and inNewHeight to 480 in Properties window of ResizeImage filter.
- Add new variant step and name it CreateModel.
- Create HMI by clicking on HMI button on upper toolbar.
- Add to HMI selectingVideoBox and an impulseButton beneath.
- Name the button Create Model (changes are made in Properties window in the lower left corner of AVS)
- Make your HMI looks like on this image:
- Perform these connections between HMI and program:
- In step CreateModel for variant "true" add CreateBoxRegion filter.
- Right-click on inImage macrofilter input, find Property Outputs and select Width and Height.
- Connect them to inFrameWidth and inFrameHeight respectively.
- Connect inSelection? from macrofilter's input with inBox of CreateBoxRegion filter's input.
- Add CreateEdgeModel filter.
- Connect image to inImage.
- Connect outRegion to inTemplateRegion.
- Connect diagEdges and outEdgeModel to macrofilter outputs.
- Set these new values: inEdgeThreshold to 60, inEdgeHysteresis to 40, inMinAngle to -45 and finally inMaxAngle to 45.
- Skip to variant "False" and just connect inModel with outEdgeModel.
- Go back to MainLoop and add LastNotNil filter after CreateModel macrofilter. In a new window of choosing generic filter data type select "Choose later". Connect diagEdges to it. Click OK.
- Add LocateSingleObject_Edges filter.
- Click on "Show Ports" on this filter and choose inEdgeModel.
- Connect outEdgeModel output from CreateModel and outImage from ResizeImage to the current filter.
- In Properties window change default value of LocateSingleObjects_Edges to auto and set inMinScore to 0.6.
- Add DrawRectangles_SingleColor and connect outObject.Match to inRectangles and connect image from ResizeImage filter to inImage. Connect outImage to inImage on HMI input of selectingVideoBox.
- Connect outEdgeModel from CreateModel to the macrofilter's output nextModel.
Macrofilter Main
Macrofilter MainLoop
Used Filters
Icon | Name | Description |
---|---|---|
CreateBoxRegion | Creates a rectangular region corresponding to a given box. | |
LocateSingleObject_Edges | Detection of an object whose outlines are sharp and rigid. Often one of the first filters in a program. | |
CreateEdgeModel | Dynamic creation of models in the runtime environment (normally they are created interactively in Studio). | |
DrawRectangles_SingleColor | Draws rectangles on an image with a single color. | |
LastNotNil | Returns the last value passed that existed. | |
ResizeImage | Enlarges or shrinks an image to new dimensions. | |
WebCamera_GrabImage | Not recommended for industrial operation. |
Further Readings
- Template Matching - Most detailed description of the Template Matching technique.