Adaptive step size matching method and device based on gradient direction displacement constraint
Patent Information
- Application Number
- CN202611309710.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-27
- Publication Date
- 2026-09-25
AI Technical Summary
[0004]本发明的主要目的在于提供一种基于梯度方向位移约束的自适应步长的匹配方法及装置,旨在解决现有模板匹配在角度与尺度搜索时采用固定步长,无法根据模板轮廓特征自动调整,步长过小则计算量激增,步长过大则易跳过真实匹配峰,精度与效率难以兼顾的技术问题
[0015]本发明通过分析轮廓特征点位置与梯度方向,计算旋转敏感度和缩放敏感度,以最大敏感度约束相邻采样点在梯度方向的位移不超过1像素,自动导出角度步长和对数缩放步长,从而生成最优采样模板集进行梯度方向匹配,在保证不漏检的前提下最大限度减少冗余计算,实现精度与效率的自适应最优平衡。
Smart Images

Figure CN122821182A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image recognition, and more particularly to an adaptive step-size matching method and apparatus based on gradient direction displacement constraints. Background Technology
[0002] Template matching is a fundamental and widely used technique in computer vision, playing a crucial role in industrial inspection, target recognition, and visual localization. In practical applications, the target to be identified often undergoes rotation and scale changes, necessitating multi-dimensional searches in angle and scale spaces to find the target pose that best matches the template. The choice of search step size directly determines the matching accuracy and computational efficiency: if the angle or scale step size is set too small, a large number of sampled templates must be generated, leading to a sharp increase in matching computation and making it difficult to meet real-time requirements; if the step size is set too large, the feature differences between adjacent sampled templates become significant, easily skipping the true matching score peak, resulting in missed detections or decreased localization accuracy.
[0003] In existing template matching methods, the step size is usually a fixed value or depends on manual experience, lacking consideration for the geometric features of the template itself. For example, grayscale or edge-based matching methods often perform traversal searches with uniform angular and scale intervals, ignoring the differences in sensitivity of templates of different shapes to rotation and scaling. For templates whose contour features are far from the center of rotation, even a small angular change can cause a large shift in the edge position, requiring more refined angular sampling; while for near-circular or centrally symmetric templates, the same angular step size is too dense, wasting computational resources. Similarly, in scaling searches, a fixed step size cannot adapt to the rate of change of template details at different scales, often resulting in insufficient sampling at small scales and excessive sampling at large scales. Summary of the Invention
[0004] The main objective of this invention is to provide a matching method and apparatus based on gradient direction displacement constraints with an adaptive step size. This aims to solve the technical problems of existing template matching, which uses a fixed step size when searching for angles and scales, and cannot automatically adjust according to the template contour features. If the step size is too small, the computational load will increase dramatically, and if the step size is too large, it will easily skip the true matching peak, making it difficult to balance accuracy and efficiency.
[0005] To achieve the above objectives, this invention proposes an adaptive step-size matching method based on gradient direction displacement constraints, comprising: Obtain a template image, extract contour feature points from the template image, and obtain the position information and gradient direction information of each contour feature point; Based on position information, gradient direction information, and a preset upper limit for gradient direction displacement, the angle search step size and scaling search step size are determined. The angle search step size ensures that the displacement of each contour feature point in its own gradient direction under adjacent angle sampling templates is not greater than the upper limit of displacement. The scaling search step size ensures that the displacement of each contour feature point in its own gradient direction under adjacent logarithmic scale sampling templates is not greater than the upper limit of displacement. By using the angle search step size and the scaling search step size, a template set consisting of multiple sampling templates with different angles and scales is generated, and gradient direction features are extracted and normalized from the image to be matched to obtain a unit gradient direction feature map. The template set is used to perform traversal matching on the feature map of the unit gradient direction, the matching score is calculated, and the matching target is determined based on the matching score.
[0006] Furthermore, the angle search step size is determined, including: Based on the position information and gradient direction information, calculate the rotation sensitivity value of each contour feature point; the rotation sensitivity value represents the amount of displacement along its gradient direction when the contour feature point rotates around the center of the template by a unit angle. Obtain the maximum rotation sensitivity value among all the rotation sensitivity values of the contour feature points; The angle search step size is determined based on the maximum rotation sensitivity value and the upper limit of the gradient direction displacement.
[0007] Further, determine the scaling search step size, including: Based on the location information and gradient direction information, the scaling sensitivity value of each contour feature point is calculated; the scaling sensitivity value represents the displacement of the contour feature point along its gradient direction under a unit logarithmic scale change; Obtain the maximum scaling sensitivity value among all scaling sensitivity values of contour feature points; The scaling search step size under the logarithmic scale is determined based on the maximum scaling sensitivity value, the preset upper limit of the scaling factor, and the upper limit of the gradient direction displacement.
[0008] Furthermore, the contour feature points are the edge contour feature points of the template image; the position information is the normalized coordinates (xi, yi) with the template center as the origin; and the gradient direction information is the unit gradient direction vector (gxi, gyi).
[0009] Furthermore, the rotation sensitivity value is calculated using the following formula: si_rot=|xi·gyi-yi·gxi|; in, The angle search step size Δθ is calculated using the following formula: Δθ = 1 / max(si_rot); Where max(si_rot) is the maximum rotation sensitivity value, and || is the absolute value symbol.
[0010] Furthermore, the scaling sensitivity value si_sca = |xi·gxi + yi·gyi|; the scaling search step size Δt under the logarithmic scale is calculated by the following formula: Δt = 1 / (smax·max(si_sca)), where smax is the preset upper limit of the scaling factor, max(si_sca) is the maximum scaling sensitivity value, and || is the absolute value sign.
[0011] Furthermore, the angle search step size Δθ is the angle search step size, and the scaling search step size Δt under the logarithmic scale is the scaling search step size; Specifically, by utilizing the angle search step size and the scaling search step size, a template set consisting of multiple sampling templates with different angles and scales is generated, including: Multiple rotation angles θ_k = k·Δθ are generated using the angle search step size Δθ as the increment; Multiple logarithmic scale values t_j=j·Δt are generated using the scaling search step size Δt under logarithmic scale as the increment, and each logarithmic scale value is converted into a scaling factor s_j=exp(t_j); The template image is transformed by a combination of rotation and scaling according to each rotation angle and each scaling factor to obtain multiple sampling templates.
[0012] Furthermore, the matching score is the sum of the dot products of the unit gradient direction vectors of all contour feature points in the sampling template and the unit gradient direction vectors at the corresponding positions in the unit gradient direction feature map.
[0013] Furthermore, extracting contour feature points from the template image includes: performing edge detection on the template image and extracting edge points as contour feature points.
[0014] The present invention also proposes a matching device for adaptive step size based on gradient direction displacement constraint, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the steps of the matching method for adaptive step size based on gradient direction displacement constraint.
[0015] This invention analyzes the position and gradient direction of contour feature points, calculates rotation sensitivity and scaling sensitivity, and constrains the displacement of adjacent sampling points in the gradient direction to no more than 1 pixel with the maximum sensitivity. It automatically derives the angle step size and logarithmic scaling step size, thereby generating the optimal sampling template set for gradient direction matching. Under the premise of ensuring no missed detection, it minimizes redundant calculations and achieves an adaptive optimal balance between accuracy and efficiency. Attached Figure Description
[0016] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0017] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This is a flowchart illustrating the adaptive step size matching method based on gradient direction displacement constraints of the present invention. Figure 2 This is a schematic diagram illustrating the angle step size calculation of the adaptive step size matching method based on gradient direction displacement constraints of the present invention. Figure 3 This is a schematic diagram illustrating the scaling step size calculation of the adaptive step size matching method based on gradient direction displacement constraints according to the present invention. Figure 4 This is a schematic diagram of the module structure of the adaptive step size matching device based on gradient direction displacement constraint of the present invention.
[0019] The objectives, features, and advantages of this invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0020] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of the present invention and are not intended to limit the present invention.
[0021] To better understand the technical solution of the present invention, a detailed description will be provided below in conjunction with the accompanying drawings and specific embodiments.
[0022] like Figure 1 As shown, Figure 1 This is a flowchart illustrating the adaptive step size matching method based on gradient direction displacement constraints of the present invention.
[0023] This invention proposes an adaptive step-size matching method based on gradient direction displacement constraints, comprising: S10, Obtain the template image, extract the contour feature points in the template image, and obtain the position information and gradient direction information of each contour feature point; S20: Based on position information, gradient direction information and preset upper limit of gradient direction displacement, determine the angle search step size and scaling search step size. The angle search step size ensures that the displacement of each contour feature point in its own gradient direction under adjacent angle sampling templates is not greater than the upper limit of displacement. The scaling search step size ensures that the displacement of each contour feature point in its own gradient direction under adjacent logarithmic scale sampling templates is not greater than the upper limit of displacement. S30 uses the angle search step size and the scaling search step size to generate a template set consisting of multiple sampling templates with different angles and scales, and performs gradient direction feature extraction and normalization on the image to be matched to obtain a unit gradient direction feature map. S40 uses the template set to perform traversal matching on the unit gradient direction feature map, calculates the matching score, and determines the matching target based on the matching score.
[0024] This invention analyzes the position and gradient direction of contour feature points, calculates rotation sensitivity and scaling sensitivity, and constrains the displacement of adjacent sampling points in the gradient direction to no more than 1 pixel with the maximum sensitivity. It automatically derives the angle step size and logarithmic scaling step size, thereby generating the optimal sampling template set for gradient direction matching. Under the premise of ensuring no missed detection, it minimizes redundant calculations and achieves an adaptive optimal balance between accuracy and efficiency.
[0025] S10, Obtain the template image, extract the contour feature points in the template image, and obtain the position information and gradient direction information of each contour feature point; Specifically, edge contour feature points are extracted from the template image, and the position and orientation information of each feature point are recorded to form a template feature set, where the feature point positions are normalized to the image center. Here, mod is the template contour feature set, xi is the x-coordinate of the edge feature point, yi is the y-coordinate of the edge feature point, cx is the x-coordinate of the template image center, cy is the y-coordinate of the template image center, and (gxi, gyi) is the unit gradient direction vector of the feature point.
[0026] The template image to be matched is obtained, and edge detection is performed on the template image to be matched, extracting the edge contour feature points in the image. Each contour feature point records its position information and gradient direction information. The position information is normalized with the center of the template image as the origin to obtain the coordinates (xi, yi). The gradient direction information is the unit gradient direction vector (gxi, gyi) at the point. It is perpendicular to the edge direction and represents the direction of the most drastic gray-level change of the feature point. It is also the direction that is most sensitive to positional offset in the matching.
[0027] To facilitate subsequent description, unified conventions for the mathematical symbols involved are made first. After edge detection is performed on a template image, the extracted contour feature points are recorded as a set P={pi}. For each feature point pi, its position coordinates (xi,yi) are coordinate values normalized by size with the geometric center of the template image as the origin, where horizontally right is the positive direction of the x-axis and vertically upward is the positive direction of the y-axis. The unit gradient direction vector (gxi,gyi) at this point is obtained by normalizing the gradient amplitude, satisfying (gxi)²+(gyi)²=1, whose direction is perpendicular to the trend of the edge and points to the direction of increasing gray value. The pixel size of the template image is width W and height H. After normalization, the value range of xi is [-W / 2,W / 2], and the value range of yi is [-H / 2,H / 2]. The scale factor s is defined as the ratio of the target size to the original size of the template. s>1 means enlargement, 0<s<1 means reduction, and s=1 corresponds to the original template size. The logarithmic scale coordinate t=ln(s), and equal-interval sampling of t on the scale axis corresponds to equal-ratio sampling of s. The preset upper limit of the scaling factor smax is usually set according to actual detection requirements. For example, smax=2.0 means that the target is allowed to be enlarged to a maximum of 2 times the template.
[0028] S20, determining an angle search step size and a scaling search step size based on the position information, gradient direction information and a preset upper limit of gradient direction displacement, wherein the angle search step size enables the displacement of each contour feature point in its own gradient direction under adjacent angle sampling templates to be not greater than the displacement upper limit, and the scaling search step size enables the displacement of each contour feature point in its own gradient direction under adjacent logarithmic scale sampling templates to be not greater than the displacement upper limit; Further, the angle step size is calculated according to the template feature point information, wherein the angle step size is not randomly selected but automatically calculated according to the geometric characteristics of the template contour features. In principle, the angle step size should be sufficiently small to ensure that the displacement of the template contour feature points in the gradient direction under two adjacent angles does not exceed 1 pixel, thereby ensuring that the matching score does not miss the real peak due to too coarse sampling.
[0029] Since template contour feature points detect boundaries, their effective information direction is the gradient direction, i.e., the direction perpendicular to the boundary. If a feature point moves along the boundary direction, the boundary changes little, and therefore the gradient direction also changes little. However, if it moves perpendicular to the boundary direction (i.e., the gradient direction changes), the gradient magnitude and direction will change drastically when the feature point leaves or enters the boundary, causing a drastic change in the matching score. Therefore, the gradient direction is the direction in which template feature points are most sensitive to angle changes. Thus, when the angle factor changes, the position of the template feature point moves radially perpendicularly, and its displacement in the gradient direction is controlled within 1 pixel, where radial refers to the direction from the template origin to the feature point. The sensitivity of the template feature point to angle depends on the distance of the feature point from the rotation center and the gradient direction. The greater the sensitivity, the smaller the angle step size, ensuring that the feature point does not "jump" across the boundary during rotation, leading to matching failure.
[0030] If the template feature point pi=(xi,yi) is rotated around the origin (the center of rotation) by an angle Δθ, then the position after rotation is p'i=R(Δθ). pi. Where R(Δθ) is the rotation matrix. The displacement vector generated before and after the feature point rotation. When Δθ is near 0 (i.e., a small angle approximation), the Taylor expansion of the trigonometric functions is: ; .
[0031] therefore Since Δθ is sufficiently small, we can take a first-order approximation to obtain... Therefore, the projection length of the rotation displacement vector onto the gradient direction of the feature point is... Let the sensitivity of the feature point in terms of angle be... .because ,therefore Calculate the maximum sensitivity max(si_rot) of all feature points in the template to obtain the angle step size. .
[0032] Furthermore, such as Figure 2 The diagram of the angle step size calculation of the adaptive step size matching method based on gradient direction displacement constraint of the present invention is shown. The template feature point A is rotated around the rotation center o by an angle Δθ to obtain the feature point B. The displacement generated by the feature point A during rotation is AB. The gradient direction of the feature point A is AG. The displacement generated by AB in the gradient direction AG is AB'.
[0033] In this embodiment, the optimal step size for searching in angle space is determined. This process utilizes the concept of geometric displacement constraints. When the template rotates around its center by a small angle, each feature point will generate a circular arc displacement. What truly affects the change in matching similarity is the projection of this displacement onto the gradient direction of the feature point. If the projection length is too large, the feature point may completely cross the boundary, causing a sharp drop in the matching score and missing the correct matching position. Therefore, to ensure that the matching peak is not lost, the gradient direction projection displacement of all feature points should be controlled to not exceed 1 pixel. Based on the small angle approximation, after the rotation angle Δθ causes the feature point position to change, the projection length in the gradient direction can be expressed as |xi·gyi-yi·gxi|·Δθ. Here, si_rot=|xi·gyi-yi·gxi| is defined as the rotation sensitivity of the feature point, which is determined by the distance from the feature point to the rotation center and the angle between the gradient direction and the tangent. The greater the distance and the more parallel the gradient direction and the tangent, the higher the sensitivity. By traversing all contour feature points of the template and obtaining the maximum rotation sensitivity max(si_rot), the angle search step size should be Δθ = 1 / max(si_rot) to ensure that the upper limit of displacement does not exceed 1 pixel. The step size calculated in this way constrains the gradient direction displacement of the most sensitive feature point to 1 pixel, while the displacement of other feature points is less than this value. Thus, the maximum angle sampling interval is obtained while ensuring that no feature point skips edges.
[0034] After obtaining the angle search step size Δθ, it is necessary to combine it with a preset rotation search range to generate a specific rotation angle sequence. The rotation search range is set by the user based on the possible attitude changes of the target, usually denoted as [θ_min, θ_max]. For example, when the target may appear in any direction, θ_min = -180°, θ_max = 180° can be set; if the target's orientation changes are limited, a smaller range can be set to reduce the amount of computation. Within the range [θ_min, θ_max], samples are uniformly taken with a step size of Δθ to generate a series of rotation angles θ_k = θ_min + k·Δθ, k = 0, 1, 2, ..., until θ_k exceeds θ_max. The template generated in this way covers the required angle space, and the gradient direction displacement between adjacent angle templates satisfies the constraint that the displacement does not exceed 1 pixel.
[0035] S30 uses the angle search step size and the scaling search step size to generate a template set consisting of multiple sampling templates with different angles and scales, and performs gradient direction feature extraction and normalization on the image to be matched to obtain a unit gradient direction feature map. The scaling step size is calculated based on the template feature point information. For scaling, the scaling step size should also be based on a similar rule as the angle step size. When the scaling factor changes, the position of the template feature points will move along the radial direction, thus causing the template feature points to be displaced in the gradient direction. Similarly, this displacement should be controlled to not exceed 1 pixel.
[0036] Calculating template scaling on a logarithmic scale aligns better with human visual perception. A logarithmic scale allows for proportional sampling on the scale axis and arithmetic sampling on the logarithmic scale, meaning the relative rate of change of the scaling factor, Δs / s, remains constant within each scale interval. This enables finer sampling at smaller scales. For example, when s is small (e.g., 0.5), the absolute step size Δs between adjacent sampled templates is small, capturing subtle changes; when s is large (e.g., 2.0), Δs is large to avoid unnecessary over-sampling. For template feature points pi=(xi,yi), a scaling factor is defined. Therefore, the location of the template feature points in the image When t changes slightly, Δt causes the displacement of the feature points before and after scaling to... When Δt is near 0 (i.e., a small-scale approximation), the Taylor expansion of the exponential function is: Taking a first-order approximation, we get , Therefore, the projection length of the displacement vector generated by the scaling transformation onto the gradient direction of the feature point is... Let the sensitivity of feature points to scaling be... .because ,therefore The scaling step size is related to the scaling scale *s* itself. To ensure that the displacement does not exceed 1 pixel in the worst case (i.e., when *s* is at its maximum), *s* can be taken as the upper limit of the scaling range, *smax*. The maximum sensitivity *max*(si_sca) of all feature points in the template is calculated to obtain the scaling step size on a logarithmic scale. .
[0037] For scale-based search, the step size in the logarithmic scale space needs to be determined. During scale transformation, feature points move radially, and their displacement in the gradient direction also determines the drasticness of the change in matching similarity.
[0038] Furthermore, representing the scale factor s in logarithmic coordinates, let t = ln(s). Then, the projection of the feature point displacement caused by a small change Δt onto the gradient direction is |xi·gxi + yi·gyi|·Δt·s. We define si_sca = |xi·gxi + yi·gyi| as the scaling sensitivity. Considering that the projected displacement is also related to the current scale s, to ensure that the displacement does not exceed the upper limit even at the maximum scale, we take a preset upper limit of the scaling range smax. Using the largest scaling sensitivity max(si_sca) among all feature points, we calculate the logarithmic scale step size according to Δt = 1 / (smax·max(si_sca)). This step size ensures that, in the worst case, the gradient direction displacement of the feature point is exactly 1 pixel, thus sampling at the sparsest but safest interval on the scale axis.
[0039] Similarly, the scale search range also needs to be preset according to the actual application. Let the lower limit of the scale factor be smin and the upper limit be smax, then the corresponding logarithmic scale range is [ln(smin), ln(smax)]. After determining the logarithmic scale step size Δt, sampling starts from t_min=ln(smin) with Δt as the increment, obtaining a series of logarithmic scale values t_j=t_min+j·Δt, j=0,1,2,..., until t_j exceeds ln(smax). Then, each t_j is converted into a linear scale factor by s_j=exp(t_j). Since Δt is based on the maximum sensitivity and smax constraint, even at the largest scale, the gradient direction displacement of feature points between two adjacent scaling templates will not exceed the preset upper limit, ensuring that no matching peaks are missed throughout the entire scale search range.
[0040] like Figure 3 The diagram shows the scaling step calculation of the adaptive step size matching method based on gradient direction displacement constraint of the present invention. The template feature point A is scaled by Δs around the rotation center o to obtain feature point B. The displacement generated by feature point A during scaling is AB, the gradient direction of feature point A is AG, and the displacement generated by AB in the gradient direction AG is AB'.
[0041] S40 uses the template set to perform traversal matching on the unit gradient direction feature map, calculates the matching score, and determines the matching target based on the matching score.
[0042] In this embodiment, a series of sampling templates are generated based on the calculated angle step size and scaling step size to construct a template dataset. After obtaining the angle step size Δθ and the logarithmic scale step size Δt, a template set covering the search range is generated. A series of rotation angles θ_k = k·Δθ are generated in increments of Δθ; a series of logarithmic scale values t_j = j·Δt are generated in increments of Δt, and then converted back to a linear scale factor s_j = exp(t_j). The original template image is geometrically transformed one by one according to each rotation angle and each scaling factor to obtain multiple sampling templates with different angles and scales. These templates together constitute the template set used for matching. Because the step size design strictly follows the gradient direction displacement constraint, adjacent templates within the template set have smooth similarity changes at the feature level, and the target pose will not be missed due to an excessively large step size.
[0043] Then, the gradient direction features of the image to be matched are calculated in the same way as the template feature points are extracted, and the gradient direction features are normalized to obtain unit gradient direction features (sxi, syi). The template mod is used to slide row by row and column by column on the image to be matched to find the target, where the matching score of the target at position (x, y) is the sum of the similarity between all template feature points and their corresponding feature points on the image to be matched.
[0044] The similarity between template feature points and feature points in the image to be matched is calculated using the cosine of the direction angle. ,in , Let (gxi, gyi) and (sxi, syi) be the magnitudes of the direction vectors of the template feature points and the direction vectors of the feature points in the image to be matched, respectively, and let (gxi, gyi) and (sxi, syi) be unit vectors. Therefore, the similarity calculation simplifies to si = gxi·sxi + gyi·syi, where s(x, y) is the matching score of the template feature at position (x, y) in the image to be matched. After calculating the scores for all locations on the image to be matched, targets whose scores do not meet the score threshold are removed, and the matching is completed.
[0045] The same feature extraction operation as the template is performed on the image to be matched: contour points are obtained using edge detection, and the unit gradient direction vector of each point is calculated to form a unit gradient direction feature map of the entire image to be matched. This feature map reflects the edge direction and intensity information at each position in the image, providing a basis for subsequent similarity calculation. A sliding window traversal matching is performed on the unit gradient direction feature map using the generated template set. At each search position, for each contour feature point in the current sampled template, its unit gradient direction vector (gxi, gyi) is multiplied by the corresponding unit gradient direction vector (sxi, syi) in the feature map, i.e., the cosine of the angle between the two vectors is calculated. The sum of the dot product results of all feature points is obtained to get the matching score at that position. Since both vectors are unit vectors, the dot product value is between [-1, 1], and the closer it is to 1, the more consistent the direction and the higher the matching degree. After traversing all possible positions and all templates in the image to be matched, the target that meets the requirements is selected according to the preset score threshold, completing the matching.
[0046] Furthermore, the score threshold involved in the above matching process can be determined as follows. Since the matching score is defined as the sum of the dot products of the unit gradient direction vectors of all feature points of the template and the corresponding points in the image to be matched, theoretically, the maximum similarity of a single feature point is 1. Therefore, for a template with n feature points, its maximum matching score is n. The score threshold can be set to a certain percentage of the maximum score, such as 0.6n to 0.8n. This percentage can be adjusted according to situations such as target occlusion or incomplete edge extraction. Another approach is adaptive thresholding: after calculating the matching scores at all positions in the image to be matched, the mean and standard deviation are calculated. The threshold is set to the mean plus a certain multiple of the standard deviation, allowing the threshold to automatically adjust with changes in image content, enhancing robustness to illumination and noise. Non-maximum suppression can also be combined to retain only the positions with the highest scores near local peaks among the candidate positions, further reducing false detections.
[0047] Furthermore, in the above method, contour feature points can be extracted using conventional edge detection operators such as Canny, and the gradient direction can be calculated using the Sobel operator or center difference. Setting the upper limit of gradient direction displacement to one pixel is an optimal value that balances accuracy and efficiency. This constraint stems directly from the requirement for pixel-level matching accuracy, ensuring that theoretically no correct target position or pose will be lost.
[0048] From a technical perspective, the angle step size Δθ = 1 / max(|xi·gyi-yi·gxi|) and the logarithmic scale step size Δt = 1 / (smax·max(|xi·gxi+yi·gyi|)) automatically derived by analyzing the template's own geometric characteristics ensure that the sampling density matches the template's contour sensitivity distribution: for templates with rich details and clear contours, the sensitivity is high, and the step size automatically decreases to ensure fine matching; for templates with simple contours or central symmetry, the sensitivity is low, and the step size automatically increases to accelerate the search. This adaptive mechanism significantly reduces unnecessary template generation and matching calculations while maintaining matching integrity, significantly improving matching speed compared to the fixed step size method while maintaining high matching accuracy.
[0049] It should be noted that this invention is not only applicable to single-target matching, but can also be directly applied to multi-target matching scenarios. When there are multiple targets similar to the template in the image to be matched, after calculating the matching score position by position, multiple position points with scores exceeding a threshold will be obtained. These points may be spatially clustered, corresponding to multiple adjacent detection windows of the same real target. To avoid duplicate output, a non-maximum suppression strategy can be adopted: all detection positions exceeding the threshold are sorted from high to low according to the matching score, the highest-scoring position is taken as the detection result, and other candidate positions with an overlap higher than the preset intersection-union threshold are eliminated. This process is repeated until the candidate list is empty. The final output is the position, rotation angle, and scale factor of multiple non-overlapping matching targets.
[0050] Figure 4 This is a schematic diagram of the module structure of the adaptive step size matching device based on gradient direction displacement constraint of the present invention.
[0051] This invention also provides a matching device with an adaptive step size based on gradient direction displacement constraints, including a memory 10 and a processor 20. The memory 10 stores a computer program, and the processor executes the program to implement the various steps of the above-described method. This device can be embedded in a machine vision system, industrial inspection equipment, or robot positioning module. Through adaptive step size optimization, it can achieve real-time, high-precision multi-scale, multi-angle target matching on a hardware platform with limited computing power.
[0052] Furthermore, in addition to the memory 10 and processor 20, the matching device may also include an image acquisition module, a feature extraction module, a step size calculation module, a template generation module, a matching calculation module, and a result output module. The image acquisition module acquires template images and images to be matched; the feature extraction module performs edge detection on the input image and calculates the gradient direction of each pixel, outputting a normalized gradient feature map; the step size calculation module receives template feature point data and calculates the angle step size and scaling step size using the aforementioned method; the template generation module generates a multi-angle, multi-scale template set based on the step size and search range; the matching calculation module calculates the matching score position by position on the feature map of the image to be matched using the template set; and the result output module filters and outputs the position, angle, and scale information of the matching target based on the score threshold. The modules are connected via a data bus and execute sequentially or in parallel under the scheduling of the processor 20, realizing a complete adaptive template matching process.
[0053] The above are only some embodiments of the present invention and do not limit the patent scope of the present invention. All equivalent structural transformations made under the technical concept of the present invention using the contents of the present invention specification and drawings, or direct / indirect applications in other related technical fields, are included within the patent protection scope of the present invention.
Claims
1. A matching method for adaptive step size based on gradient direction displacement constraints, characterized in that, include: Obtain a template image, extract contour feature points from the template image, and obtain the position information and gradient direction information of each contour feature point; The contour feature points are the edge contour feature points of the template image; the position information is the normalized coordinates (xi, yi) with the template center as the origin; the gradient direction information is the unit gradient direction vector (gxi, gyi). Based on the position information, the gradient direction information, and the preset upper limit of gradient direction displacement, the angle search step size and the scaling search step size are determined. The rotation sensitivity value is calculated using the following formula: si_rot=|xi·gyi-yi·gxi|; in, The angle search step size Δθ is calculated using the following formula: Δθ = 1 / max(si_rot); Where max(si_rot) is the maximum rotation sensitivity value, and || is the absolute value sign; The scaling sensitivity value si_sca = |xi·gxi + yi·gyi|; the scaling search step size Δt under the logarithmic scale is calculated by the following formula: Δt = 1 / (smax·max(si_sca)), where smax is the preset upper limit of the scaling factor, max(si_sca) is the maximum scaling sensitivity value, and || is the absolute value sign; The angle search step size ensures that the displacement of each contour feature point in its own gradient direction under adjacent angle sampling templates is not greater than the upper limit of displacement, and the scaling search step size ensures that the displacement of each contour feature point in its own gradient direction under adjacent logarithmic scale sampling templates is not greater than the upper limit of displacement. Using the angle search step size and the scaling search step size, a template set consisting of multiple sampling templates with different angles and scales is generated, and gradient direction features are extracted and normalized from the image to be matched to obtain a unit gradient direction feature map. The template set is used to perform traversal matching on the unit gradient direction feature map, the matching score is calculated, and the matching target is determined based on the matching score.
2. The method according to claim 1, characterized in that, The determined angle search step size includes: Based on the position information and the gradient direction information, the rotation sensitivity value of each contour feature point is calculated; the rotation sensitivity value represents the displacement along its gradient direction when the contour feature point rotates around the center of the template by a unit angle. Obtain the maximum rotation sensitivity value among all the rotation sensitivity values of the contour feature points; The angle search step size is determined based on the maximum rotation sensitivity value and the upper limit of the gradient direction displacement.
3. The method according to claim 1, characterized in that, Determining the scaling search step size includes: Based on the location information and the gradient direction information, the scaling sensitivity value of each contour feature point is calculated; the scaling sensitivity value represents the displacement of the contour feature point along its gradient direction under a unit logarithmic scale change; Obtain the maximum scaling sensitivity value among all scaling sensitivity values of contour feature points; The scaling search step size under the logarithmic scale is determined based on the maximum scaling sensitivity value, the preset upper limit of the scaling factor, and the upper limit of the gradient direction displacement.
4. The method according to claim 1, characterized in that, The process of generating a template set consisting of multiple sampling templates with different angles and scales using the angle search step size and the scaling search step size includes: Multiple rotation angles θ_k = k·Δθ are generated using the angle search step size Δθ as the increment; Multiple logarithmic scale values t_j=j·Δt are generated using the scaling search step size Δt under the logarithmic scale as the increment, and each logarithmic scale value is converted into a scaling factor s_j=exp(t_j); The template image is transformed by a combination of rotation and scaling according to each rotation angle and each scaling factor to obtain the plurality of sampling templates.
5. The method according to claim 1, characterized in that, The matching score is the sum of the dot product of the unit gradient direction vector of all contour feature points in the sampling template and the unit gradient direction vector at the corresponding position in the unit gradient direction feature map.
6. The method according to claim 1, characterized in that, The step of extracting contour feature points from the template image includes: performing edge detection on the template image and extracting edge points as the contour feature points.
7. A matching device for adaptive step size based on gradient direction displacement constraint, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the adaptive step size matching method based on gradient direction displacement constraints as described in any one of claims 1 to 6.