Rapid template matching method
By using a multi-level pyramid structure and an angle step search mechanism in image processing, the problems of low computational efficiency and poor robustness in processing large-scale images and complex backgrounds are solved, and fast and accurate template matching is achieved.
Patent Information
- Application Number
- CN202411791771.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-06
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2044-12-06
AI Technical Summary
When traditional template matching methods deal with large-scale image datasets, complex background interference, and tasks that require high-precision positioning, the calculation efficiency is low and the robustness is poor, especially in the presence of scale changes, rotation angle differences or occlusion, which is difficult to accurately find the matching position.
Fast template matching is achieved by building a multi-level pyramid structure and combining a specific angle step search mechanism. The method quickly narrows the search at a thicker level, then gradually refines to higher resolution levels for precise matching, and covers all possible rotation angles in the first stage of matching.
It significantly improves processing speed while maintaining high matching accuracy, enabling accurate target positioning in complex contexts, reducing false alarm rates, and adapting to rotation changes.
Smart Images

Figure CN119649071A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of image processing, and in particular to a fast template matching method. Background Art
[0002] In the field of computer vision and image processing, template matching is a commonly used technique to identify whether there is an area in the target image that is 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 or simple patterns. However, with the diversification of application scenarios and the improvement of technical requirements, traditional methods have exposed problems such as low computational efficiency and poor robustness when dealing with large-scale image datasets, complex background interference, and tasks that require high-precision positioning.
[0003] Especially when there are scale changes, rotation angle differences or partial occlusions between the target template and the image to be searched, traditional template matching algorithms often find it difficult to accurately find the matching position, resulting in unsatisfactory matching results. In order to solve these problems, researchers have proposed a variety of improvement schemes, such as using multi-scale analysis and introducing rotation invariant features to enhance the adaptability and accuracy of the matching algorithm. Although these methods have improved the matching performance to a certain extent, they are usually accompanied by high computational costs. Especially when processing high-definition images or real-time video streams, how to ensure 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 strategy of coarse-to-fine step-by-step matching using a multi-level pyramid structure has gradually attracted attention. This strategy constructs image representations (i.e., pyramids) at different resolution levels, which can quickly narrow the search range at a coarser level, and then accurately determine the target position at a finer level, thereby 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 rotation changes, how to design a method that can quickly locate and maintain high matching accuracy has become an urgent problem to be solved. Summary of the invention
[0005] The present invention aims to at least solve the technical problems existing in the prior art. To this end, the present invention proposes a fast template matching method, which can significantly improve the processing speed while maintaining high matching accuracy by constructing a multi-level pyramid structure and combining a specific angle step search mechanism.
[0006] A fast template matching method according to some embodiments of the present invention comprises the following steps:
[0007] S100, setting a pyramid with n layers according to the size of the matched target template, storing the template of each layer of the pyramid in a structure container or in different containers, and obtaining n stored template images;
[0008] S200, performing the same pyramid setting on the image to be searched according to the number of pyramid layers matched by the target template, the template corresponding to the roughly positioned image is the n-1th template in the container, and calculating the angle step of the roughly positioned image;
[0009] S300, according to the angle step of the rough positioning, all the rotation angles of the rough matching are set within the range of 0-360 degrees, the first stage matching is performed on the n-th layer of the pyramid, and the result that meets the matching condition requirements each time is stored in the container;
[0010] S400, performing layer-by-layer refinement matching on the results obtained by the first-stage matching in the second matching stage, wherein the angle of each matching is the angle value of the result that meets the conditions obtained by the pyramid matching of the previous layer, and a coarse positioning angle step value is added or subtracted from the angle value of the result to perform matching;
[0011] S500, obtaining the second-stage matching results, screening the overlapping matching results, obtaining the overlapping screening results, and retaining a target with the largest matching score in the overlapping area;
[0012] S600 , for the result after the last overlapping screening, perform result screening and coordinate conversion 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 the search range of the target location at a coarse level, and then gradually refine it to a higher resolution level for accurate matching. This strategy from coarse to fine greatly reduces unnecessary calculations, especially when processing large-size images or video streams, and can significantly shorten the matching time.
[0015] 2. This method designs a first-stage matching process based on angle steps, which can fully cover all possible rotation angles in the range of 0-360 degrees. Subsequently, the angle range is further refined in the second-stage matching to ensure that matching targets can be accurately found even in the presence of obvious rotation differences, thereby improving the algorithm's support for rotation invariance.
[0016] 3. Through the effective integration and screening of the matching results of the first and second stages, especially the processing method for overlapping areas, the final output results are guaranteed to have not only high accuracy but also reduce the false alarm rate.
[0017] 4. Under complex background conditions, such as partial occlusion, illumination changes, etc., the method provided by the present invention can still maintain good performance. This is because multi-scale analysis helps to filter out noise information, and the reasonably set angle 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, the method includes S010 before S100: reading the original image and converting the original image into a single-channel grayscale image, setting a ROI area and cropping the grayscale image to obtain the ROI image as the target template; the method includes S020 before S100: 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 layer-by-layer 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 n-1 layer pyramid for the results that meet the matching condition requirements in the first stage matching, and storing the results that meet the condition requirements after matching in a container; performing a third template matching on the n-2 layer pyramid for the results that meet the matching condition requirements in the second stage matching, the matching angle is the angle that is matched and meets the requirements at the n-1 layer of the previous pyramid, and storing the results that meet the conditions at the n-2 layer matching in a container, and the same goes for other layers of the pyramid until the 0th layer pyramid, i.e., the original image template is matched.
[0020] According to a fast template matching method of some embodiments of the present invention, the angle step calculation formula for coarse positioning is: dAngleStep=arctan(2.0 / max(image.cols,image.rows))×180.0 / π; wherein 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, the step between S200 and S300 further includes S210: setting a matching score threshold of each pyramid layer;
[0022] The step between S500 and S600 also includes S510: deleting the result whose matching score does not meet the matching score threshold of this pyramid layer.
[0023] According to a fast template matching method of some embodiments of the present invention, the setting of the matching score threshold of each pyramid layer specifically includes: setting a matching threshold that meets the requirements as Score, the template matching score threshold corresponding to the first pyramid layer is S1=Score*K, where K is a coefficient between 0-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 this way.
[0024] According to a fast template matching method of some embodiments of the present invention, storing the template of each layer of the pyramid in a structure container or storing them in different containers specifically includes: setting a mask image of the same size as the target template, storing the template of each layer of the pyramid and the mask image in a structure container or storing them in different containers.
[0025] According to a fast template matching method of some embodiments of the present invention, S600 specifically includes: filtering the results after S500 filtering, filtering according to the required angle and matching score, and for the matching results that meet the matching requirements, enlarging and restoring the coordinates and width and height of the matching target results according to the number of pyramid layers set by the template matching, so that the matching results are correctly mapped to the image to be searched for display.
[0026] Additional aspects and advantages of the present invention will be given in part in the following description and in part will be obvious from the following description, or will be learned through practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The above and / or additional aspects and advantages of the present invention will become apparent and easily understood from the description of the embodiments in conjunction with the following drawings, in which:
[0028] Figure 1 is a flow chart of an embodiment of the present invention.
[0029] Figure 2 Schematic diagram of a target template according to an embodiment of the present invention.
[0030] Figure 3 This is a schematic diagram showing that the matching results according to an embodiment of the present invention are correctly mapped to the image to be searched. DETAILED DESCRIPTION
[0031] Embodiments of the present invention are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and cannot be understood as limiting the present invention.
[0032] In the description of the present invention, it should be understood that the descriptions involving orientations, such as up, down, left, right, front, back, etc., and the orientations or positional relationships indicated are based on the orientations or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the modules or components referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as a limitation on the present invention.
[0033] In the description of the present invention, if there is a description of first and second, it is only for the purpose of distinguishing the technical features, and cannot be understood as indicating or implying the relative importance or implicitly indicating the number of the indicated technical features or implicitly indicating the order of the indicated technical features.
[0034] In the description of the present invention, unless otherwise clearly defined, terms such as setting, installing, connecting, etc. should be understood in a broad sense, and technicians in the relevant technical field can reasonably determine the specific meanings of the above terms in the present invention based on the specific content of the technical solution.
[0035] like Figure 1-Figure 3 As shown, an embodiment of the present invention provides a fast template matching method.
[0036] A fast template matching method comprises the following steps:
[0037] S100, setting a pyramid with n layers according to the size of the matched target template, storing the template of each layer of the pyramid in a structure container or in different containers, and obtaining n stored template images;
[0038] S200, performing the same pyramid setting on the image to be searched according to the number of pyramid layers matched by the target template, the template corresponding to the roughly positioned image is the n-1th template in the container, and calculating the angle step of the roughly positioned image;
[0039] S300, according to the angle step of the rough positioning, all the rotation angles of the rough matching are set within the range of 0-360 degrees, the first stage matching is performed on the nth layer of the pyramid (the corresponding template container is the n-1th), and the result that meets the matching condition requirements each time is stored in the container;
[0040] S400, performing layer-by-layer refinement matching on the results obtained by the first-stage matching in the second matching stage, wherein the angle of each matching is the angle value of the result that meets the conditions obtained by the pyramid matching of the previous layer, and a coarse positioning angle step value is added or subtracted from the angle value of the result to perform matching;
[0041] S500, obtaining the second-stage matching results, screening the overlapping matching results, obtaining the overlapping screening results, and retaining a target with the largest matching score in the overlapping area;
[0042] S600 , for the result after the last overlapping screening, perform result screening and coordinate conversion according to the matching score and angle range to obtain the final result.
[0043] The present invention uses a multi-level pyramid structure, and the method can quickly narrow the search range of the target position at a coarse level, and then gradually refine it to a higher resolution level for accurate matching. This strategy from coarse to fine greatly reduces unnecessary calculations, especially when processing large-size images or video streams, it can greatly shorten the matching time. The method designs a first-stage matching process based on an angle step, which can fully cover all possible rotation angles within a 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 obvious rotation differences, thereby improving the algorithm's support for rotation invariance. Through the effective integration and screening of the first-stage and second-stage matching results, especially the processing method for overlapping areas, it is ensured that the final output result not only has a high accuracy, but also reduces the false alarm rate. Under complex background conditions, such as partial occlusion, illumination changes, etc., the method provided by the present invention can still maintain good performance. This is because multi-scale analysis helps to filter out noise information, and at the same time, a reasonably set angle search range helps the algorithm to more accurately distinguish between real targets and interference factors in the background.
[0044] It is understood that n is an integer greater than zero.
[0045] In the fast template matching method described in this embodiment, the step S100 includes S010: reading the original image, converting the original image into a single-channel grayscale image, setting the ROI area, and cropping the grayscale image to obtain the ROI image as the target template. Specifically, before the formal matching, the original image is first converted into a single-channel grayscale image and the ROI is set for cropping as the target template. This not only simplifies the amount of data for subsequent processing, but also enhances the focus on specific areas, thereby improving the execution efficiency of the entire process.
[0046] In the fast template matching method described in this embodiment, the step S100 includes S010 before: the step S100 includes S020 before: converting the image to be searched into a single-channel grayscale image. Specifically, the image to be searched is also grayscaled, which reduces the interference caused by color information, allowing the algorithm to focus more on shape and texture features, further improving the accuracy of matching.
[0047] In the fast template matching method described in this embodiment, 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 n-1 layer pyramid for the results that meet the matching condition requirements in the first stage matching, and storing the results that meet the condition requirements after matching in a container; performing a third template matching on the n-2 layer pyramid for the results that meet the matching condition requirements in the second stage matching, the matching angle is the angle that has been matched and met in the n-1 layer of the previous pyramid, and storing the results that meet the condition in the n-2 layer matching in a container, and the same applies to the other layers of the pyramid until the 0th layer pyramid, i.e., the original image template matching, is the second stage matching. For example, the user sets the number of pyramid layers to 3, and there are 28 angles for rough positioning. After the first time, i.e., the 3rd layer pyramid template matching, only 8 angles that meet the condition are screened, and then the number of template matchings performed on the 2nd layer pyramid is 8, i.e., the number of angles screened after the first matching, and there are 2 angles that meet the condition after the 2nd layer pyramid matching, and finally, the template matching is performed twice on the 1st layer pyramid, i.e., the original image, to obtain the matching result.
[0048] In the fast template matching method described in this embodiment, the angle step calculation formula for coarse positioning is: dAngleStep = arctan (2.0 / max (image.cols, image.rows)) × 180.0 / π; wherein 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 of coarse positioning is much larger than 1 degree, and the number of rotation matching will be much less than 360 times, which greatly improves the matching speed.
[0049] In the fast template matching method described in this embodiment, the step between S200 and S300 further includes S210: setting a matching score threshold for each pyramid layer; and the step between S500 and S600 further includes S510: deleting results whose matching scores do not meet the matching score threshold for this pyramid layer. Specifically, different matching score thresholds are set for each pyramid layer. This dynamic adjustment mechanism helps to eliminate low-quality matching results, especially at a coarser level, which can more quickly eliminate obvious mismatches, thereby accelerating the overall matching process.
[0050] In the fast template matching method described in this embodiment, the setting of the matching score threshold of each pyramid layer specifically includes: setting the matching threshold that meets the requirements as Score, the template matching score threshold corresponding to the first pyramid layer is S1=Score*K, where K is a coefficient between 0-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 this way. Specifically, the matching score threshold is set by using a coefficient K that decreases layer by layer, so that the matching requirements are gradually increased as the number of pyramid layers increases. This method helps to gradually improve the accuracy of matching while maintaining a certain fault tolerance rate.
[0051] It can be understood that in S400, when traversing and matching each layer of the pyramid, the number of times each layer of the pyramid is matched, for example, the angle of coarse positioning is between 0-360 degrees, there are 28 rotation angles to match, after the first stage of matching, 8 angles that meet the requirements are obtained, then the number of matches on the next layer of the pyramid is 8*3 times, after matching this layer of the pyramid, 2 angles that meet the requirements are obtained, then the number of matches on the next layer of the pyramid is 2*3, and so on. The rotation angles of coarse positioning generally do not exceed 30, which is much smaller than the 361 angles within the range of 0-360 degrees. The maximum matching score of each layer of the pyramid that meets the requirements is obtained, and at the same time, the results whose matching scores do not meet the matching score threshold set for this layer of the pyramid are removed, that is, the results whose matching scores are less than this matching score threshold continue to the next match, and the results that meet the matching score threshold conditions are uniformly converted to the images to be searched with the set number of pyramid layers, and the matching results are stored in the container as the final result screening.
[0052] The fast template matching method described in this embodiment, wherein the template of each layer of the pyramid is stored in a structure container or in different containers, specifically includes: setting a mask image of the same size as the target template, storing the template of each layer of the pyramid and the mask image in a 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 area, which is conducive to ignoring background noise or non-related parts during the matching process, thereby improving the relevance and reliability of the final matching result.
[0053] It is understandable that in S500, the same target may have multiple different matching results, with inconsistent angles or XY offsets but overlapping matches of the same target. Only the result with the largest matching score in the overlapping area is retained, and then the matching results are sorted from large to small according to the matching scores.
[0054] In the fast template matching method described in this embodiment, S600 specifically includes: filtering the results after S500, filtering according to the required angle and matching score, and for the matching results that meet the matching requirements, enlarging and restoring the coordinates and width and height of the matching target results according to the number of pyramid layers set for template matching, so that the matching results are correctly mapped to the image to be searched for display. Specifically, by further performing coordinate conversion and size restoration on the filtered results, it is ensured that the matched target can be correctly mapped back to the original image to be searched, realizing the effective transformation from abstract data to intuitive visual display, which is convenient for users to understand and apply.
[0055] It is understandable that the template matching algorithms that can be used in the present invention include mean, variance, SSD, NCC, SAD, CC, ZNCC, etc., which have strong versatility. While using pyramid layer fast matching, it does not rely on the setting of angle step when high precision is required, which can reduce the amount of calculation and quickly match the target. Compared with the traditional method that only relies on the pyramid layer setting, the number of pyramid layers that can be set is larger, and the impact on the matching result will be smaller.
[0056] Although the embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the claims and their equivalents.
Claims
1. A fast template matching method, characterized in that: The following steps are involved: S100, setting a pyramid with n layers according to the size of the matched target template, storing the template of each layer of the pyramid in a structure container or in different containers, and obtaining n stored template images; S200, performing the same pyramid setting on the image to be searched according to the number of pyramid layers matched by the target template, the template corresponding to the roughly positioned image is the n-1th template in the container, and calculating the angle step of the roughly positioned image; S300, according to the angle step of the rough positioning, all the rotation angles of the rough matching are set within the range of 0-360 degrees, the first stage matching is performed on the n-th layer of the pyramid, and the result that meets the matching condition requirements each time is stored in the container; S400, performing layer-by-layer refinement matching on the result obtained by the first-stage matching in the second-stage matching, wherein the angle of each matching is the angle value of the result that meets the conditions obtained by the pyramid matching of the previous layer, and a coarse positioning angle step value is added or subtracted from the angle value of the result to perform matching; S500, obtaining the result of the second stage matching, screening the overlapping matching results, obtaining the results after overlapping screening, and retaining a target with the largest matching score in the overlapping area; S600 , for the result after the last overlapping screening, perform result screening and coordinate conversion according to the matching score and angle range to obtain the final result.
2. A fast template matching method according to claim 1, characterized in that: The step before S100 includes S010: reading the original image, converting the original image into a single-channel grayscale image, setting a ROI area and cropping the grayscale image to obtain the ROI image as the target template; the step before S100 includes S020: converting the image to be searched into a single-channel grayscale image.
3. A fast template matching method according to claim 1, characterized in that: 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 n-1 layer pyramid for the results that meet the matching condition requirements in the first stage matching, and storing the results that meet the condition requirements after matching in a container; performing a third template matching on the n-2 layer pyramid for the results that meet the matching condition requirements in the second stage matching, the matching angle is the angle that is matched and meets the requirements at the n-1 layer of the previous layer pyramid, and then storing the results that meet the conditions at the n-2 layer matching in a container, and the same goes for other layers of pyramid until the 0th layer pyramid, i.e., the original image template is matched.
4. A fast template matching method according to claim 1, characterized in that: The angle step calculation formula for the coarse positioning is: dAngleStep=arctan(2.0 / max(image.cols,image.rows))×180.0 / π; Among them, image.cols is the width of the image and image.rows is the height of the image.
5. A fast template matching method according to claim 1, characterized in that: The step between S200 and S300 also includes S210: setting a matching score threshold of each pyramid layer; The step between S500 and S600 also includes S510: deleting the result whose matching score does not meet the matching score threshold of this pyramid layer.
6. A fast template matching method according to claim 5, characterized in that: The setting of the matching score threshold of each pyramid layer specifically includes: setting a matching threshold that meets the requirements as Score, 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 this way.
7. A fast template matching method according to claim 1, characterized in that: The storing of the template of each layer of the pyramid in a structure container or in different containers specifically includes: setting a mask image of the same size as the target template, storing the template of each layer of the pyramid and the mask image in a structure container or in different containers.
8. A fast template matching method according to claim 1, characterized in that: The S600 specifically includes: filtering the results filtered by S500 according to the required angle and matching score, and for the matching results that meet the matching requirements, enlarging and restoring the coordinates and width and height of the matching target results according to the number of pyramid layers set by the template matching, so that the matching results are correctly mapped to 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
Pyramid image layering method capable of reserving edge information and application of pyramid image layering method
CN117745557A
Template matching method and device for the method
JP1998021389A