🟠 Shape-Based Template Matching

Function Description

Shape-based template matching is a target detection algorithm that can learn unique shapes of objects by analyzing contour and gradient information of a "template image", then quickly and accurately locate 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 targets that have undergone rotation and scaling by configuring parameters, and return their precise angles and sizes.

The operator’s workflow is divided into two stages:

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

  2. Matching stage: During runtime, the operator loads the trained template and performs global search in the input image to be detected, finding all regions similar to the template shape.

Usage Scenarios

In environments with poor lighting conditions or variable product colors, lacking large amounts of annotated data, you can try using shape matching.

Input Output

Input

Image: Image to be searched and matched.

Output

Detection result: A detection instance list. Each element in the list represents a successfully matched target, containing its position, rotation angle, scaling ratio, matching score and contour.

Parameter Description

Force Training

Parameter Description

Used to select whether to force retrain the template during initialization, even if a template file already exists.

Parameter Adjustment

It’s recommended to enable this option during debugging to force the operator to generate new templates using new parameters; after parameters are fixed, this option can be disabled to speed up process startup.

Template Image

Parameter Description

Template image used as matching reference.

Parameter Adjustment

Ensure image quality as much as possible, clearly and unobstructedly displaying the complete shape of the target.

Template Generation Path

Parameter Description

Folder path for storing generated template files.

Parameter Adjustment

It’s recommended to specify a fixed path for easy management and reuse of trained templates; if left empty, the system default cache path will be used.

Template Name

Parameter Description

Specify a unique name for the generated template file.

Enable Scaling/Scaling Ratio

Parameter Description

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

Parameter Adjustment

If image resolution is very high, it’s recommended to enable this option and set a ratio smaller than 1 (such as 0.5) to improve training and matching speed.

Parameter Range

[0.1,1.5], default value: 0.7

Scale Range/Scale Step

Parameter Description

Define the target size range to search during matching.

Parameter Adjustment

Scale range is a binary list containing minimum and maximum values, such as [0.8,1.2] representing search from 80% to 120% of original size. Scale step defines the fineness of search within this range. The broader the range and smaller the step, the more likely to find targets of different sizes, but training and matching time will be longer. Parameters can be adjusted according to actual conditions. If the template image is made from generate template image node, you can use the scale range output by that node for this parameter.

Parameter Range

[0.8,1.2]

Rotation Angle Range/Rotation Angle Step

Parameter Description

Angle range sets the target rotation angle range to search during matching, angle step sets the search fineness.

Parameter Adjustment

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

Parameter Range

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

Feature Point Count

Parameter Description

Number of feature points extracted from the template image to describe its shape.

Parameter Adjustment

  • Increase count: Describes complex shapes more finely, stronger robustness, but will reduce speed.

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

Parameter Range

[0,1000], default value: 75

Matching Precision Level

Parameter Description

Optimizes speed and precision by performing matching on images of different resolutions. The algorithm first roughly locates on low-resolution images, then precisely locates on high-resolution images.

Parameter Adjustment

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

Parameter Range

[4,8]

Score Threshold

Parameter Description

Minimum acceptable score for matching results, higher scores represent higher similarity between matching results and template shape.

Parameter Adjustment

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

  • Decrease this value: Retrieve some targets that might be missed, but may introduce false matches.

Parameter Range

[0,100], default value: 80

Overlap Filter Threshold

Parameter Description

Used to remove multiple highly overlapping detection results for the same target, keeping only the one with highest score.

Parameter Adjustment

  • Increase this value: Can retain more overlapping matching results;

  • Decrease this value: Can more strictly remove results with larger overlap.

Parameter Range

[0,1], default value: 0.3

Class Name

Parameter Description

Manually specify a class name for successfully matched targets.

Parameter Adjustment

The "class" of detection results is specified by this parameter.