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.
Labeling connections is explained in this article.
Solution (FIS)
- Add WebCamera_GrabImage filter to the ACQUIRE section.
- Add ResizeImage filter to the ACQUIRE section and connect outFrame from previous filter with inImage input.
- Set inNewWidth to 640 and inNewHeight to 480 in Properties window of ResizeImage filter.
- Add a new Variant macrofilter to the PROCESS section 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 FIS)
- 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 CreateEdgeModel1 filter.
- Connect image to inImage.
- Connect outRegion to inTemplateRegion.
- Connect outEdges 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.
- Add LastNotNil filter after CreateModel macrofilter. In a new window of choosing generic filter data type select "Choose later". Connect outEdges to it. Click OK.
- Add LocateSingleObject_Edges1 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_Edges1 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
Used Filters
Icon | Name | Description |
---|---|---|
![]() |
WebCamera_GrabImage | Not recommended for industrial operation. |
![]() |
CreateEdgeModel1 | Dynamic creation of models in the runtime environment (normally they are created interactively in Studio). |
![]() |
ResizeImage | Enlarges or shrinks an image to new dimensions. |
![]() |
DrawRectangles_SingleColor | Draws rectangles on an image with a single color. |
![]() |
CreateBoxRegion | Creates a rectangular region corresponding to a given box. |
![]() |
LastNotNil | Returns the last value passed that existed. |
![]() |
LocateSingleObject_Edges1 | Detection of an object whose outlines are sharp and rigid. Often one of the first filters in a program. |
Further Readings
- Template Matching - Most detailed description of the Template Matching technique.