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)

  1. 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.
  2. Add WebCamera_GrabImage filter.
  3. Add ResizeImage filter and connect outFrame from previous filter with inImage input.
  4. Set inNewWidth to 640 and inNewHeight to 480 in Properties window of ResizeImage filter.
  5. Add new variant step and name it CreateModel.
    • Name the input inButtonPressed of Bool data type.
    • Connect prevModel macrofilter Main input with CreateModel macrofilter. Data type will be set automatically - just click OK.
    • Connect outImage to CreateModel macrofilter as a new input. Click OK for a default data type.
  6. 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:
  7. Perform these connections between HMI and program:
    • Connect outValue from HMI's Create Model button with inPressedButton input of Create Model macrofilter.
    • Connect outSelection from HMI's selectingVideoBox with CreateModel filter. Name the input inSelection. Set data type to Box? and click OK.
  8. 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.
  9. Add CreateEdgeModel filter.
  10. 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.
  11. Add LocateSingleObject_Edges filter.
  12. 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.
  13. 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