A fast template matching method
The template matching method using a multi-level pyramid structure and angle step search mechanism solves the problems of low computational efficiency and poor robustness in existing technologies, and achieves fast and accurate template matching, especially efficient positioning under rotational changes and complex backgrounds.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-06
- Publication Date
- 2026-04-03
AI Technical Summary
Existing template matching methods suffer from low computational efficiency and poor robustness when dealing with large-scale image datasets, complex background interference, and high-precision localization. They are particularly difficult to match accurately when there are scale changes, rotation angle differences, or occlusion.
By employing a multi-level pyramid structure combined with a specific angle step search mechanism, and through a coarse-to-fine step-by-step matching strategy, the search range is narrowed and the rotation angle is fully covered within the 0-360 degree range. The matching is refined layer by layer to improve accuracy.
It significantly improves processing speed and matching accuracy, reduces computational load, lowers false alarm rate, and can accurately find matching targets in complex backgrounds.
Smart Images

Figure CN119649071B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a fast template matching method. Background Technology
[0002] In computer vision and image processing, template matching is a commonly used technique to identify whether there are regions in a target image that are similar to a given template. Traditional template matching methods, such as direct comparison based on grayscale values and correlation measurement, perform well when dealing with small-sized or simple patterns. However, with the diversification of application scenarios and the increasing technical requirements, traditional methods have revealed problems such as low computational efficiency and poor robustness when dealing with large-scale image datasets, complex background interference, and tasks requiring high-precision localization.
[0003] Especially when there are scale variations, rotation angle differences, or partial occlusion between the target template and the search image, traditional template matching algorithms often struggle to accurately locate the matching position, leading to unsatisfactory matching results. To address these issues, researchers have proposed various improvement schemes, such as using multi-scale analysis and introducing rotation-invariant features to enhance the adaptability and accuracy of the matching algorithms. Although these methods improve matching performance to some extent, they typically come with high computational costs, especially when processing high-definition images or real-time video streams. Ensuring fast and accurate template matching remains a challenge.
[0004] In recent years, with the development of hardware technology and the improvement of parallel computing capabilities, the coarse-to-fine stepwise matching strategy using a multi-level pyramid structure has gradually attracted attention. This strategy constructs image representations at different resolution levels (i.e., pyramids), which can quickly narrow the search range at coarser levels and then accurately determine the target position at finer levels, thus effectively balancing the relationship between speed and accuracy. However, most existing methods do not fully consider how to efficiently combine rotation angle information for matching optimization, especially when dealing with targets with rotational changes. How to design a method that can both quickly locate targets and maintain high matching accuracy has become an urgent problem to be solved. Summary of the Invention
[0005] This invention aims to at least address the technical problems existing in the prior art. To this end, this invention proposes a fast template matching method that, by constructing a multi-level pyramid structure and combining it with a specific angle step size search mechanism, can significantly improve processing speed while maintaining high matching accuracy.
[0006] A fast template matching method according to some embodiments of the present invention includes the following steps:
[0007] S100. Based on the size of the target template to be matched, set a pyramid with n layers, store the template of each layer of the pyramid in a structure container or store them in different containers respectively, and obtain n stored template images.
[0008] S200. According to the number of pyramid layers matched by the target template, the same pyramid settings are applied to the image to be searched. The template corresponding to the image of coarse localization is the (n-1)th one in the container. Calculate the angle step size of coarse localization.
[0009] S300. Based on the angle step size of the coarse positioning, set all rotation angles of coarse matching within the range of 0-360 degrees, perform the first stage matching on the nth layer of the pyramid, and store the results that meet the matching conditions each time into the container.
[0010] S400. The results obtained from the first stage of matching are refined layer by layer in the second matching stage. The angle of each matching is the angle value of the result that meets the conditions obtained from the previous pyramid matching, and a coarse positioning angle step value is added or subtracted from the angle value of the result for matching.
[0011] S500: Obtain the matching results of the second stage, filter the overlapping matching results, obtain the results after overlapping filtering, and retain the target with the highest matching score in the overlapping area;
[0012] S600. Based on the results of the previous overlap filtering, further filtering and coordinate transformation are performed according to the matching score and angle range to obtain the final result.
[0013] A fast template matching method according to some embodiments of the present invention has at least the following beneficial effects:
[0014] 1. By using a multi-level pyramid structure, this method can quickly narrow down the search range for the target location at a coarser level, and then gradually refine it to a higher resolution level for precise matching. This coarse-to-fine strategy greatly reduces unnecessary computation, especially when processing large images or video streams, and can significantly shorten the matching time.
[0015] 2. This method designs a first-stage matching process based on angle step size, which can fully cover all possible rotation angles within the range of 0-360 degrees. Subsequently, the angle range is further refined in the second-stage matching to ensure that the matching target can be accurately found even in the presence of significant rotational differences, thereby improving the algorithm's support for rotation invariance.
[0016] 3. By effectively integrating and screening the matching results of the first stage and the second stage, especially the processing method for the overlapping regions, it is ensured that the final output result not only has high accuracy but also reduces the false alarm rate.
[0017] 4. Under complex background conditions, such as partial occlusion and illumination changes, the method provided by the present invention can still maintain good performance. This is because the multi-scale analysis helps to filter out the noise information, and the reasonably set angular search range helps the algorithm to more accurately distinguish the real target from the interference factors in the background.
[0018] According to a fast template matching method of some embodiments of the present invention, before the S100, it includes S010: reading the original image, converting the original image into a single-channel grayscale image, setting the ROI region, and cropping the grayscale image to obtain the ROI image as the target template; before the S100, it includes S020: converting the image to be searched into a single-channel grayscale image.
[0019] According to a fast template matching method of some embodiments of the present invention, the step-by-step refinement matching of the results obtained by the first-stage matching in the second matching stage specifically includes: performing a second template matching on the results that meet the matching condition requirements in the first stage on the (n - 1)-th layer pyramid, and storing the results that meet the condition requirements after matching into a container; performing a third template matching on the results that meet the matching condition requirements in the second stage on the (n - 2)-th layer pyramid, and the matching angle is the angle that meets the requirements after matching on the (n - 1)-th layer of the previous pyramid. Then, storing the results that meet the condition requirements on the (n - 2)-th layer into the container. The same applies to other layer pyramids until the 0-th layer pyramid, that is, the original image template matching.
[0020] According to a fast template matching method of some embodiments of the present invention, the calculation formula for the angular step size of the rough positioning is: dAngleStep = arctan(2.0 / max(image.cols, image.rows)) × 180.0 / π; where image.cols is the width of the image and image.rows is the height of the image.
[0021] According to a fast template matching method of some embodiments of the present invention, between the S200 and the S300, it further includes S210: setting the matching score threshold for each layer of the pyramid;
[0022] Between the S500 and the S600, it further includes S510: deleting the results whose matching scores do not meet the matching score threshold of this layer of the pyramid.
[0023] According to some embodiments of the present invention, a fast template matching method is provided, wherein setting the matching score threshold for each pyramid layer specifically includes: setting a matching threshold that meets the requirements as Score, wherein the template matching score threshold corresponding to the first pyramid layer is S1 = Score * K, where K is a coefficient between 0 and 1, the template matching score threshold corresponding to the second pyramid layer is S2 = S1 * K, and the template matching score thresholds corresponding to other pyramid layers are calculated in the same way.
[0024] According to some embodiments of the present invention, a fast template matching method is provided, wherein storing the template of each pyramid layer in a structure container or in different containers specifically includes: setting a mask image of the same size as the target template, and storing the template and mask image of each pyramid layer in a structure container or in different containers.
[0025] According to some embodiments of the present invention, a fast template matching method, step S600 specifically includes: filtering the results after filtering in step S500, filtering according to the required angle and matching score, and for matching results that meet the matching requirements, magnifying and restoring the coordinates and width and height of the matching target result according to the number of pyramid layers set in the template matching, so that the matching result is correctly mapped onto the image to be searched for display.
[0026] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description
[0027] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the description of the embodiments taken in conjunction with the following drawings, in which:
[0028] Figure 1 This is a flowchart of an embodiment of the present invention.
[0029] Figure 2 This is a schematic diagram of the target template in an embodiment of the present invention.
[0030] Figure 3 This is a schematic diagram illustrating how the matching results of an embodiment of the present invention are correctly mapped onto the image to be searched. Detailed Implementation
[0031] Embodiments of the present invention are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.
[0032] In the description of the present invention, it should be understood that for the orientation description, such as the orientation or positional relationship indicated by up, down, left, right, front, back, etc., is based on the orientation or positional relationship shown in the drawings. It is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the referred modules or elements must have a specific orientation, be constructed and operated in a specific orientation. Therefore, it should not be construed as a limitation to the present invention.
[0033] In the description of the present invention, if the first and second are described only for the purpose of distinguishing technical features, it should not be understood as indicating or implying relative importance or implicitly specifying the quantity of the indicated technical features or implicitly specifying the sequence relationship of the indicated technical features.
[0034] In the description of the present invention, unless otherwise clearly defined, terms such as "set", "install", "connect", etc. should be understood in a broad sense. Those skilled in the art can reasonably determine the specific meanings of the above terms in the present invention in combination with the specific content of the technical solution.
[0035] As Figures 1-3 shown, an embodiment of the present invention provides a fast template matching method.
[0036] A fast template matching method includes the following steps:
[0037] S100. Set a pyramid with a layer number of n according to the size of the target template to be matched, store the templates of each layer of the pyramid in a structure container or store them separately in different containers, and obtain n stored template images;
[0038] S200. Perform the same pyramid setting on the image to be searched according to the pyramid layer number of the target template matching. The template corresponding to the roughly located image is the (n - 1)-th in the container, and calculate the angular step size of the rough positioning;
[0039] S300. Set all rotation angles for rough matching within the range of 0 - 360 degrees according to the angular step size of the rough positioning, perform the first-stage matching on the n-th layer of the pyramid (the corresponding template container is the (n - 1)-th), and store the results that meet the matching condition requirements each time into the container;
[0040] S400. Perform layer-by-layer refinement matching on the results obtained from the first-stage matching in the second matching stage. The angle for each matching is the angle value obtained from the matching of the upper layer of the pyramid that meets the conditions, and perform matching by adding and subtracting an angular step size value of the rough positioning on the angle value of the result;
[0041] S500. Obtain the results of the second-stage matching, screen the overlapping matching results, obtain the results after overlapping screening, and retain one target with the largest matching score in the overlapping area;
[0042] S600. Based on the results of the previous overlap filtering, further filtering and coordinate transformation are performed according to the matching score and angle range to obtain the final result.
[0043] This invention utilizes a multi-level pyramid structure, enabling rapid narrowing of the target location search range at a coarser level, followed by progressive refinement to a higher resolution level for precise matching. This coarse-to-fine strategy significantly reduces unnecessary computation, particularly when processing large images or video streams, drastically shortening matching time. The method employs a first-stage matching process based on angle step size, comprehensively covering all possible rotation angles within a 0-360 degree range. Subsequently, the second-stage matching further refines the angle range, ensuring accurate target finding even with significant rotational differences, thus improving the algorithm's support for rotation invariance. Effective integration and filtering of the first and second-stage matching results, especially the handling of overlapping areas, ensures high accuracy and reduces false alarm rates in the final output. Even under complex background conditions, such as partial occlusion or lighting variations, the method maintains good performance. This is because multi-scale analysis helps filter out noise, while a reasonably set angle search range helps the algorithm more accurately distinguish between the real target and background interference.
[0044] It is understandable that n is an integer greater than zero.
[0045] This embodiment describes a fast template matching method, which includes S010 before S100: reading the original image, converting the original image into a single-channel grayscale image, setting the Region of Interest (ROI), and cropping the grayscale image to obtain the ROI image as the target template. Specifically, before performing formal matching, the original image is first converted into a single-channel grayscale image and the ROI is set and cropped to serve as the target template. This not only simplifies the amount of data processed subsequently but also enhances the focus on specific regions, improving the execution efficiency of the entire process.
[0046] The fast template matching method described in this embodiment includes S010 before S100: and S020 before S100: converting the image to be searched into a single-channel grayscale image. Specifically, the image to be searched is also grayscale processed. This step reduces the interference from color information, allowing the algorithm to focus more on shape and texture features, further improving the accuracy of matching.
[0047] The fast template matching method described in this embodiment involves refining the results obtained from the first-stage matching in the second-stage matching process. Specifically, this includes: performing a second template matching on the (n-1)th layer of the pyramid for the results that meet the matching criteria in the first stage, and storing the matching results in a container; performing a third template matching on the (n-2)th layer of the pyramid for the results that meet the matching criteria in the second stage, matching the angles that meet the criteria from the (n-1)th layer of the previous pyramid, and storing the matching results from the (n-2)th layer in a container. This process continues for other pyramid layers until the template matching is performed on the 0th layer of the pyramid (the original image). This constitutes the second-stage matching. For example, if the user sets the pyramid layer number to 3, there are 28 coarsely defined angles. After the first (3rd layer) template matching, only 8 angles meet the criteria. The number of template matching operations on the 2nd layer of the pyramid is then 8, which is the number of angles selected after the first matching. After the 2nd layer matching, 2 angles meet the criteria. Finally, the matching result is obtained by performing two template matching operations on the 1st layer of the pyramid (the original image).
[0048] The fast template matching method described in this embodiment uses the following formula to calculate the angle step size for coarse positioning: dAngleStep=arctan(2.0 / max(image.cols,image.rows))×180.0 / π; where image.cols is the width of the image and image.rows is the height of the image. Specifically, if the calculated value is a floating-point number and not an integer, it cannot be converted to an integer to avoid loss of precision in subsequent calculations. According to the above calculation formula, the angle step size for coarse positioning is much greater than 1 degree, and the number of rotation matching operations will be much less than 360, greatly improving the matching speed.
[0049] The fast template matching method described in this embodiment further includes S210 between S200 and S300: setting a matching score threshold for each pyramid level; and S510 between S500 and S600: deleting results whose matching scores do not meet the matching score threshold for this pyramid level. Specifically, different matching score thresholds are set for each pyramid level. This dynamic adjustment mechanism helps to eliminate low-quality matching results, especially at coarser levels where obvious mismatches can be eliminated more quickly, thereby accelerating the overall matching process.
[0050] The fast template matching method described in this embodiment includes setting the matching score threshold for each pyramid layer. Specifically, this involves setting a matching threshold of "Score" to meet the requirements. The template matching score threshold for the first pyramid layer is S1 = Score * K, where K is a coefficient between 0 and 1. The template matching score threshold for the second pyramid layer is S2 = S1 * K, and the template matching score thresholds for other pyramid layers are calculated using the same method. Specifically, a coefficient K that decreases progressively with each layer is used to set the matching score threshold, gradually increasing the matching requirements as the number of pyramid layers increases. This method helps to gradually improve matching accuracy while maintaining a certain error tolerance.
[0051] Understandably, in S400, when traversing and matching on each pyramid level, the number of matches per pyramid level is as follows: For example, the coarse positioning angle is between 0 and 360 degrees, with 28 rotation angles to match. After the first stage of matching, 8 angles meet the requirements. Therefore, the number of matches on the next pyramid level is 8*3. After matching on this pyramid level, 2 angles meet the requirements, so the number of matches on the next pyramid level is 2*3, and so on. The rotation angles for coarse positioning generally do not exceed 30, which is much smaller than the 361 angles in the 0-360 degree range. The maximum matching score for each pyramid level is obtained. At the same time, results whose matching scores do not meet the matching score threshold set for this pyramid level are removed. Matches with scores below the threshold continue to the next matching stage. Images meeting the matching score threshold are uniformly converted to the search image at the set pyramid level, and the matching results are stored in a container as the final result filter.
[0052] The fast template matching method described in this embodiment, specifically the step of storing the templates of each pyramid layer in a single structure container or in different containers, includes: setting a mask image of the same size as the target template, and storing the templates and mask images of each pyramid layer in a single structure container or in different containers. Specifically, in addition to storing the template image, a mask image of the same size is added to mark the valid regions. This helps to ignore background noise or irrelevant parts during the matching process, thereby improving the relevance and reliability of the final matching result.
[0053] Understandably, in S500, the same target may have multiple different matching results. The angles may be inconsistent or the matching may overlap due to XY offset. Only the result with the highest matching score in the overlapping area is retained. Then, the matching results are sorted from largest to smallest according to the matching score.
[0054] The fast template matching method described in this embodiment includes step S600, which specifically involves: filtering the results after step S500 based on the required angle and matching score; for matching results that meet the matching requirements, scaling up and restoring the coordinates and dimensions of the matching target result according to the pyramid level set in the template matching, so that the matching result is correctly mapped onto the image to be searched. Specifically, by further transforming the coordinates and restoring the dimensions of the filtered results, it is ensured that the matched target can be correctly mapped back to the original image to be searched, achieving an effective transformation from abstract data to intuitive visual display, which is convenient for users to understand and apply.
[0055] It is understood that the template matching algorithms used in this invention include mean, variance, SSD, NCC, SAD, CC, ZNCC, etc., which have strong versatility. While using pyramid layers for fast matching, it does not rely on the angle step size setting when high accuracy is required, which can reduce the amount of computation and quickly match the target. Compared with traditional methods that only rely on pyramid layer settings, the number of pyramid layers can be set to be larger, and the impact on the matching results is smaller.
[0056] Although embodiments of the invention have been shown and described, those skilled in the art will understand that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the claims and their equivalents.
Claims
1. A fast template matching method, characterized in that, Includes the following steps: S100. Based on the size of the target template to be matched, set a pyramid with n layers, store the template of each layer of the pyramid in a structure container or store them in different containers respectively, and obtain n stored template images. S200. According to the number of pyramid layers matched by the target template, the same pyramid settings are applied to the image to be searched. The template corresponding to the image of coarse localization is the (n-1)th one in the container. Calculate the angle step size of coarse localization. S300. Based on the angle step size of the coarse positioning, set all rotation angles of coarse matching within the range of 0-360 degrees, perform the first stage matching on the nth layer of the pyramid, and store the results that meet the matching conditions each time into the container. S400. The results obtained from the first stage matching are refined layer by layer in the second stage matching. The angle of each matching is the angle value of the result that meets the conditions obtained from the previous pyramid matching, and a coarse positioning angle step value is added or subtracted from the angle value of the result for matching. S500: Obtain the matching results of the second stage, filter the overlapping matching results, obtain the results after overlapping filtering, and retain the target with the highest matching score in the overlapping area. S600. Based on the results of the previous overlap filtering, further filtering and coordinate transformation are performed according to the matching score and angle range to obtain the final result. The process of refining the matching results obtained in the first stage in the second stage specifically includes: performing a second template matching on the (n-1)th layer of the pyramid for the results that meet the matching conditions in the first stage, and storing the matching results that meet the conditions in a container; performing a third template matching on the (n-2)th layer of the pyramid for the results that meet the matching conditions in the second stage, with the matching angle being the angle that meets the requirements after matching at the (n-1)th layer of the previous pyramid, and storing the matching results at the (n-2)th layer in a container, and so on for other pyramid layers, until the 0th layer of the pyramid, i.e., the original image template matching; The formula for calculating the angle step size for coarse positioning is: ; in, The width of the image. The height of the image; if the calculated value is a floating-point number and not an integer, it cannot be converted to an integer to avoid loss of precision in subsequent calculations; Between S200 and S300, there is also S210: setting the matching score threshold for each layer of the pyramid; Between S500 and S600, there is also S510: deleting results whose matching scores do not meet the matching score threshold of this layer of pyramid; The specific steps of setting the matching score threshold for each pyramid layer include: setting the matching threshold that meets the requirements as Score, and the template matching score threshold corresponding to the first pyramid layer as... Where K is a coefficient between 0 and 1, and represents the template matching score threshold corresponding to the second-level pyramid. The template matching score thresholds for other pyramid levels are calculated using the same method.
2. The fast template matching method according to claim 1, characterized in that, Before S100, there is S010: reading the original image and converting the original image into a single-channel grayscale image, setting the ROI region and cropping the grayscale image to obtain the ROI image as the target template; before S100, there is S020: converting the image to be searched into a single-channel grayscale image.
3. The fast template matching method according to claim 1, characterized in that: The step of storing the template of each pyramid layer in a structure container or in different containers specifically includes: setting a mask image of the same size as the target template, and storing the template and mask image of each pyramid layer in a structure container or in different containers.
4. The fast template matching method according to claim 1, characterized in that, S600 specifically includes: filtering the results after S500 filtering, filtering according to the required angle and matching score, and for matching results that meet the matching requirements, magnifying and restoring the coordinates and width and height of the matching target result according to the pyramid layer set by the template matching, so that the matching result is correctly mapped onto the image to be searched for display.
Citation Information
Patent Citations
Normalized cross-correlation image template matching implementation method
CN110210565A
Matching method for edge template with rotation
CN110728326A