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 the 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 the Web Camera and the Template Matching filters.

Labeling connections is explained in this article.

Solution (AVS):

  1. Add the WebCamera_GrabImage filter to the ACQUIRE section.
  2. Add the ResizeImage filter to the ACQUIRE section and connect the outFrame from the previous filter with the inImage input.
  3. Set the inNewWidth to 640 and the inNewHeight to 480 in the Properties window of the ResizeImage filter.
  4. Add a new Variant macrofilter to the PROCESS section and name it CreateModel.
    • Name the input inButtonPressed of Bool data type.
    • Connect the prevModel macrofilter Main input with the CreateModel macrofilter. The data type will be set automatically - just click OK.
    • Connect the outImage to CreateModel macrofilter as a new input. Click OK for a default data type.
  5. Create an HMI by clicking on the HMI button on the upper toolbar.
    • Add to the HMI a selectingVideoBox and an impulseButton beneath.
    • Name the button Create Model (changes are made in the Properties window in the lower left corner of AVS)
    • Make your HMI look like on this image:
  6. Perform these connections between HMI and the program:
    • Connect the outValue from HMI's Create Model button with the inPressedButton input of the Create Model macrofilter.
    • Connect the outSelection from HMI's selectingVideoBox with the CreateModel filter. Name the input inSelection. Set the data type to Box? and click OK.
  7. In the CreateModel step for the variant "true" add the CreateBoxRegion filter.
    • Right-click on the inImage macrofilter input, find Property Outputs and select the Width and the Height.
    • Connect them to the inFrameWidth and the inFrameHeight respectively.
    • Connect the inSelection? from the macrofilter's input with the inBox of the CreateBoxRegion filter's input.
  8. Add the CreateEdgeModel1 filter.
  9. Add the LastNotNil filter after the CreateModel macrofilter. In a new window of choosing generic filter data type, select "Choose later". Connect the outEdges to it. Click OK.
  10. Add the LocateSingleObject_Edges1 filter.
  11. Add the the DrawRectangles_SingleColor and connect the outObject.Match to the inRectangles and connect the image from the ResizeImage filter to the inImage. Connect the outImage to the inImage on the HMI input of the selectingVideoBox.
  12. Connect the outEdgeModel from the CreateModel to the macrofilter's output nextModel.

Macrofilter Main

Used Filters

Icon Name Description
CreateBoxRegion Creates a rectangular region corresponding to a given box.
CreateEdgeModel1 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.
LocateSingleObject_Edges1 Detection of an object whose outlines are sharp and rigid. Often one of the first filters in a program.
ResizeImage Enlarges or shrinks an image to new dimensions.
WebCamera_GrabImage Not recommended for industrial operation.

Further Readings