Shape Template Matching

Description

Shape-based template matching is an object detection algorithm that learns the unique shape of an object by analyzing the contour and gradient information of a "template image," and then quickly and accurately locates the object in new images. Unlike deep learning methods that rely on color and texture, this algorithm is insensitive to lighting changes. As long as the object’s shape remains unchanged, it can successfully match even under different brightness, contrast, or color conditions. Additionally, the algorithm can find rotated and scaled targets by configuring parameters and returning their precise angles and dimensions.

The workflow of this operator is divided into two stages:

  1. Training Phase: During initialization, the operator analyzes the uploaded template image, extracts key shape features, and generates a reusable template file based on set parameters such as rotation and scaling.

  2. Matching Phase: At runtime, the operator loads the trained template and performs a global search in the input image to be detected to find all regions similar to the template shape.

Overview

Quickly locates targets with the same shape in images using templates, outputting parameters such as target position, angle, and size.

Scenarios

Shape matching can be attempted in environments with poor lighting conditions or variable product colors, where there is a lack of large amounts of labeled data.

Inputs and Outputs

Inputs

Image: The image to be searched and matched.

Outputs

Detection Results: A list of detection instances. Each element in the list represents a successfully matched target, containing its position, rotation angle, scaling factor, matching score, and contour.

Parameters

Initialization Parameters

Force Training

Description

Controls whether the operator is forced to retrain the template during initialization, even if a template file already exists.

Tuning Instructions

It is recommended to enable this option during the debugging phase to force the operator to generate a new template using the updated parameters. Once the parameters are finalized, this option can be disabled to speed up pipeline startup.

Parameter Range

Default: True

Template Image

Description

The template image used as a reference for matching.

Tuning Instructions

Ensure the image quality is as high as possible, displaying the complete shape of the target clearly and without occlusion.

Parameter Range

Values/Range:

  • .png

  • .jpg

  • .tif

  • .jpeg

Template Generation Path

Description

The folder path used to store the generated template files.

Tuning Instructions

It is recommended to specify a fixed path for easier management and reuse of trained templates. If left empty, the system default cache path will be used.

Template Name

Description

Specifies a unique name for the generated template file.

Parameter Range

Default: template

Enable Scaling

Description

Optimization option for high-resolution images. When enabled, both the template image and the image to be matched will be scaled down by the specified ratio before processing.

Tuning Instructions

If the image resolution is very high, it is recommended to enable this item and set a ratio less than 1 (e.g., 0.5) to improve training and matching speed.

Parameter Range

Values/Range:

  • True

  • False

Default: False

Scaling Ratio

Description

Optimization option for high-resolution images. When enabled, both the template image and the image to be matched will be scaled down by the specified ratio before processing.

Tuning Instructions

If the image resolution is very high, it is recommended to enable this item and set a ratio less than 1 (e.g., 0.5) to improve training and matching speed.

Parameter Range

[0.1, 1.5], Default value: 0.7

Parameter Range

Values/Range: [0.1, 1.5]

Default: 0.7

Scaling Range

Description

Defines the target size range to be searched during matching.

Tuning Instructions

The scaling range is a binary list containing a minimum and a maximum value, e.g., [0.8, 1.2] means searching for 80% to 120% of the original size. The scaling step defines the granularity of the search within this range. A wider range and smaller step increase the likelihood of finding targets of different sizes, but training and matching times will also be longer. Adjust parameters according to the actual situation. If the template image is created from a Generate Template Image node, the output scaling range of that node can be used for this parameter.

Parameter Range

Parameter Range

Default: [0.8, 1.2]

Scaling Step

Description

Defines the target size range to be searched during matching.

Tuning Instructions

The scaling range is a binary list containing a minimum and a maximum value, e.g., [0.8, 1.2] means searching for 80% to 120% of the original size. The scaling step defines the granularity of the search within this range. A wider range and smaller step increase the likelihood of finding targets of different sizes, but training and matching times will also be longer. Adjust parameters according to the actual situation. If the template image is created from a Generate Template Image node, the output scaling range of that node can be used for this parameter.

Parameter Range

Parameter Range

Values/Range: [0.01, 1.2]

Default: 0.1

Rotation Angle Range

Description

The angle range is used to set the target rotation angle range to be searched during matching, and the angle step is used to set the search granularity.

Tuning Instructions

If the target may appear at any angle, set the range to [0, 360]. The smaller the step, the more precise the angle detection, but the longer the time consumption. If the target’s angle variation range is known and limited, narrowing the range can significantly speed up the process.

Parameter Range

Angle range [0, 360], Step [0, 60]

Parameter Range

Default: [0, 360]

Rotation Angle Step

Description

The angle range is used to set the target rotation angle range to be searched during matching, and the angle step is used to set the search granularity.

Tuning Instructions

If the target may appear at any angle, set the range to [0, 360]. The smaller the step, the more precise the angle detection, but the longer the time consumption. If the target’s angle variation range is known and limited, narrowing the range can significantly speed up the process.

Parameter Range

Angle range [0, 360], Step [0, 60]

Parameter Range

Values/Range: [0.0, 60]

Default: 1

Feature Point Count

Description

  • Increase quantity: Describes complex shapes more finely, resulting in stronger robustness, but reduces speed.

  • Decrease quantity: Faster speed, suitable for objects with simple shapes, but may be unstable for complex shapes.

Tuning Instructions

  • Increasing the value: Describes complex shapes more precisely and improves robustness, but reduces processing speed.

  • Decreasing the value: Increases processing speed and is suitable for objects with simple shapes, but may be unstable for complex shapes.

Parameter Range

[0, 1000], Default value: 75

Parameter Range

Values/Range: [0, 1000]

Default: 75

Matching Accuracy Level

Description

Usually, keep the default parameters. The numbers in the list represent the scaling factors (powers of 2) for the search image levels. For example, [4, 8] means searching at 1/4 and 1/8 resolutions respectively. Smaller numbers represent matching at levels closer to the original image resolution, resulting in higher accuracy but slower speed; larger numbers mean faster but coarser searching.

Tuning Instructions

The default parameters are generally sufficient. The numbers in the list represent the scale factors (powers of 2) for each pyramid level during image search. For example, [4, 8] indicates searching at 1/4 and 1/8 of the original resolution respectively. Smaller numbers correspond to levels closer to the original resolution, yielding higher accuracy but slower speed; larger numbers result in faster but coarser searches.

Parameter Range

Parameter Range

Default: [4, 8]

Run Parameters

Score Threshold

Description

  • Increase this value: Filter out results with low similarity to reduce false matches.

  • Decrease this value: Retrieve some targets that might have been missed, but may introduce incorrect matches.

Tuning Instructions

  • Increasing this value: Filters out results with low similarity, reducing false matches.

  • Decreasing this value: Recovers targets that may have been missed, but may introduce false matches.

Parameter Range

[0, 100], Default value: 80

Parameter Range

Values/Range: [0, 100]

Default: 80

Overlap Filtering Threshold

Description

  • Increase this value: Retain more overlapping matching results.

  • Decrease this value: Remove results with large overlaps more strictly.

Tuning Instructions

  • Increase this value: allows more overlapping matching results to be retained;

  • Decrease this value: more aggressively removes results with greater overlap.

Parameter Range

[0, 1], Default value: 0.3

Parameter Range

Values/Range: [0.0, 1.0]

Default: 0.3

Category Name

Description

Manually assigns a category name to successfully matched targets.

Tuning Instructions

The "category" of the detection result is specified by this parameter.

Parameter Range

Values/Range:

  • 0

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

  • 19

  • 20

  • 21

  • 22

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

Default: 0