Template matching method, system, device and storage medium
Through image pyramid technology and normalized cross-correlation method, the failure problem of traditional template matching under light noise is solved, and efficient and accurate template matching is achieved.
Patent Information
- Application Number
- CN202211739844.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-30
- Publication Date
- 2025-07-04
- Estimated Expiration
- 2042-12-30
AI Technical Summary
Traditional template matching algorithms are prone to failure under light noise, and the normalized cross-correlation method is slow to calculate.
The image pyramid technology is used to divide the template image and the search image into multiple levels. The key pixel points are determined by setting the variance threshold, the pixel slopes of the template and the search image are calculated, and the normalized cross-correlation method is used to match layer by layer at the key pixel points.
It improves the accuracy and efficiency of template matching, has the ability to resist light noise, and reduces calculation time.
Smart Images

Figure CN115830357B_ABST
Abstract
Description
Technical Field
[0001] This document relates to the technical field of template matching, and particularly to a template matching method, system, device, and storage medium. Background Art
[0002] Template matching is to carefully compare the template image with the search image. The specific process is to calculate and match the template image on the search image from left to right and from top to bottom, and select the region with the highest similarity as the final result of the match.
[0003] Traditional template matching is to subtract the pixel values of the template image from the corresponding pixel values in the search area of the search image, take the absolute value, and finally sum them up. The region with the smallest calculated difference is the region with the highest similarity. However, this algorithm will result in a failed match when encountering illumination noise. Another traditional template matching method is to use the calculation formula of normalized cross-correlation. This method has high accuracy and strong illumination invariance, but the calculation speed is slow. Summary of the Invention
[0004] The present invention aims to solve the above problems by providing a template matching method, system, device, and storage medium.
[0005] An embodiment of the present invention provides a template matching method, including:
[0006] S1. Grayscale stage: Convert the template image and the search image into grayscale images;
[0007] S2. Image layer division stage: Use the image pyramid technology to divide the template image and the search image converted into grayscale images into F image layers respectively;
[0008] S3. Finding key pixel points stage: Set the variance threshold for each image layer of the template image, and determine the key pixel points of each image layer of the template image according to the variance threshold, where the variance threshold of the upper image layer is less than or equal to the variance threshold of the lower image layer;
[0009] S4. Top layer image layer matching stage: Obtain the template pixel slope according to the key pixel points of the top layer image layer of the template image, denoted as k t , and perform the same operation on the pixel values at the corresponding pixels of the search image to obtain the search pixel slope, denoted as k s , calculate the similarity of the top layer image layer according to the template pixel slope and the search pixel slope, and obtain the region with the highest similarity;
[0010] S5. Non-top layer image layer matching stage: Use the normalized cross-correlation method to obtain the region with the highest similarity in the non-top layer within a specific pixel range centered on the coordinates of the region with the highest similarity;
[0011] S6. Underlying image layer matching stage: In the underlying template image, obtain the region with the highest underlying similarity in the manner of step S5 and mark it, so as to obtain the matching region.
[0012] An embodiment of the present invention provides a template matching system, including:
[0013] A grayscale conversion module for converting the template image and the search image into grayscale images;
[0014] An image layer division module for respectively dividing the template image and the search image converted into grayscale images into F image layers by using the image pyramid technology;
[0015] A key pixel point finding module for setting the variance threshold of each image layer of the template image and determining the key pixel points of each image layer of the template image according to the variance threshold, wherein the variance threshold of the upper image layer is less than or equal to the variance threshold of the lower image layer;
[0016] A top image layer matching module for obtaining the template pixel slope according to the key pixel points of the top image layer of the template image, denoted as k t , and perform the same operation on the pixel value at the corresponding pixel of the search image as on the template image to obtain the search pixel slope, denoted as k s , calculate the similarity of the top image layer according to the template pixel slope and the search pixel slope, and obtain the region with the highest similarity;
[0017] A non-top image layer matching module for obtaining the region with the highest non-top similarity within a specific pixel range centered on the coordinates of the region with the highest similarity by using the normalized cross-correlation method;
[0018] A bottom image layer matching module: for obtaining the region with the highest bottom similarity in the bottom template image and marking it, so as to obtain the matching region.
[0019] An embodiment of the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the template matching method when executing the computer program.
[0020] An embodiment of the present invention provides a computer-readable storage medium, the computer-readable storage medium stores a computer program, wherein the computer program implements the template matching method when executed by a processor.
[0021] The present invention adopts the image pyramid technology, determines the key pixel points of all image layers of the template image, and then at the key pixel points of the top image layer, compares the surrounding pixel features at the key pixel points of the template image with the surrounding pixel features at the corresponding pixel positions in the search area of the search image. Finally, the area with the highest similarity is found, and the best matching coordinates are passed to the next lower image layer. At this time, the top layer matching stage ends. In the non-top layer matching stage, the similarity is calculated at the key pixel points through the normalized cross-correlation formula, searched layer by layer, and then the target is found and marked at the bottom layer of the search image pyramid. Description of the Drawings
[0022] In order to more clearly illustrate the technical solutions in one or more embodiments of the present specification or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings described below are only some embodiments recorded in the present specification. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0023] Figure 1 It is a flowchart of the template matching method according to an embodiment of the present invention;
[0024] Figure 2 It is a schematic diagram of the template matching system according to an embodiment of the present invention;
[0025] Figure 3 It is a distribution diagram of eight pixels around the key pixel according to an embodiment of the present invention;
[0026] Figure 4 It is a schematic diagram of the template image of the second specific example according to an embodiment of the present invention;
[0027] Figure 5 It is a schematic diagram of the search image of the second specific example according to an embodiment of the present invention;
[0028] Figure 6 It is a schematic diagram of the matching result of the second specific example according to an embodiment of the present invention;
[0029] Figure 7 It is a schematic diagram of the template image of the third specific example according to an embodiment of the present invention;
[0030] Figure 8 It is a schematic diagram of the template image of the third specific example according to an embodiment of the present invention;
[0031] Figure 9 It is a schematic diagram of the template image of the third specific example according to an embodiment of the present invention;
[0032] Figure 10 It is a schematic diagram of the template image of the first specific example according to an embodiment of the present invention;
[0033] Figure 11 Schematic diagram of the matching result when r takes 0.5 in the first specific example of the embodiment of the present invention;
[0034] Figure 12 Schematic diagram of the matching failure when r takes 1 in the first specific example of the embodiment of the present invention. Specific implementation manners
[0035] In order to enable those skilled in the art to better understand the technical solutions in one or more embodiments of this specification, the following will clearly and completely describe the technical solutions in one or more embodiments of this specification with reference to the accompanying drawings in one or more embodiments of this specification. Obviously, the described embodiments are only a part of the embodiments of this specification, rather than all the embodiments. Based on one or more embodiments of this specification, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of this document.
[0036] Method embodiments
[0037] The embodiment of the present invention provides a template matching method. Figure 1 It is a flowchart of the template matching method in the embodiment of the present invention. The template matching method in the embodiment of the present invention specifically includes:
[0038] S1. Grayscale stage: Convert the template image and the search image into grayscale images;
[0039] S2. Image layer division stage: Use the image pyramid technology to divide the template image and the search image converted into grayscale images into F image layers respectively;
[0040] S3. Finding key pixel points stage: Set the variance threshold for each image layer of the template image, and determine the key pixel points of each image layer of the template image according to the variance threshold, where the variance threshold of the upper image layer is less than or equal to the variance threshold of the lower image layer;
[0041] S4. Top layer image layer matching stage: Obtain the template pixel slope according to the key pixel points of the top layer image layer of the template image, denoted as k t , and perform the same operation on the pixel values at the corresponding pixels of the search image as on the template image to obtain the search pixel slope, denoted as k s , calculate the similarity of the top layer image layer according to the template pixel slope and the search pixel slope, and obtain the region with the highest similarity;
[0042] S5. Non-top layer image layer matching stage: Use the normalized cross-correlation method to obtain the non-top layer region with the highest similarity within a specific pixel range centered on the coordinates of the region with the highest similarity;
[0043] S6. Bottom layer image layer matching stage: In the bottom layer template image, obtain the area with the highest bottom layer similarity in the manner of step S5 and mark it, so as to obtain the matching area.
[0044] In the embodiment of the present invention, the number of layers of the image pyramid is set to 4, and the specific implementation manner of step S3 is as follows:
[0045] Without exceeding the boundary of the template image, calculate the variance of the pixel value of each pixel point and the eight pixel values of the eight surrounding pixel points centered on this pixel point. This method can obtain the pixel fluctuation degree of each position of the template image. If the variance of the pixel point position is greater than the preset threshold, it can be set as the key pixel point of the template image.
[0046] Regarding the preset threshold, because the fewer the pixel points and the smaller the pixel value distance in the image layer closer to the top layer of the image pyramid, while the more the pixel points and the larger the pixel value distance in the image layer closer to the bottom layer of the image pyramid. If the preset threshold is too high, it will lead to too few key pixel points, thus affecting the accuracy. And if the preset threshold is too low, it will lead to too many key pixel points, thus affecting the matching speed. Therefore, in order to balance speed and accuracy, the preset threshold of the upper layer image should be less than or equal to the preset threshold of the lower layer image. Of course, the segmentation threshold can also be set as a constant value. In this embodiment, the preset thresholds from the top layer image layer to the bottom layer image layer are 120, 200, 200, 200. In addition, when calculating the key pixel points of the top layer image layer of the template image, the number a of key pixel points should be measured to prepare for subsequent acceleration calculation.
[0047] In step S4, calculate the image features of the eight surrounding pixels centered on the key pixel points on the top layer image layer of the template image pyramid, where Figure 3 is the distribution diagram of the eight surrounding pixels of the key pixel of the embodiment of the present invention. Take the pixel values of the pixel points with the labels in Figure 3 as two groups of coordinates. Among them, the serial numbers 1-8 represent the pixel values of the corresponding points. The specific method is:
[0048] Obtain coordinate A(1 + 2 + 3, 3 + 4 + 5) and coordinate B(5 + 6 + 7, 7 + 8 + 1), and then calculate the slope k t =(B.y - A.y) / (B.x - A.x), where A.x and A.y respectively represent the abscissa and ordinate of coordinate A, and B.x and B.y respectively represent the abscissa and ordinate of coordinate B. It should be noted that if B.x - A.x = 0 and (B.y - A.y) is not 0, then take the reciprocal of k t , that is, the value of k t is 0. If both B.x - A.x and (B.y - A.y) are 0, then cancel the calculation of the corresponding pixel point.
[0049] At the key pixel points of the template image, perform the same operation on the pixel values at the corresponding pixels in the search area of the corresponding search image, that is, calculate the slope k through (B.y - A.y) / (B.x - A.x). s , which represents the characteristics of the surrounding pixels at this pixel point of the search image. If k s has a denominator of 0, that is, B.x - A.x of the corresponding point is 0, then cancel the calculation of the corresponding pixel point.
[0050] If k t previously took the reciprocal because the denominator was 0 and the numerator was not 0, then if k s taking the reciprocal makes sense (at this time, do not determine whether the denominator of k s is 0), that is, the denominator of the reciprocal of k s is not 0, also take the reciprocal of k s , and then perform the following operations.
[0051] The k of the template image t can be calculated in advance before search matching to achieve the effect of improving speed, or k t and k s can be calculated together during search matching. In this embodiment, and in all examples in this technical report, the latter method is adopted.
[0052] The calculation method of the similarity between the template image and the search area of the search image is as follows:
[0053] When |k t | < 1, the calculation method of the similarity between the template image and the search area of the search image is as follows:
[0054] N = |k t - k s | + N
[0055] |k t - k s | is the way to compare the surrounding pixel features at the key pixel points of the template image and the surrounding pixel features at the corresponding pixel positions in the search area of the search image. Among them, if |k t - k s | > r, then let |k t - k s | = r. The value of N in this formula is the current |k t - k sThe summation of |. In this example, the value of r is set to 1, which helps to improve the accuracy of the matching, that is, to reduce the influence of a single pixel on the overall matching. It should be noted that the value of r should be set reasonably. If the value of r is too small, there will not be enough difference information, which will affect the accuracy of the matching. If the value of r is too large, the calculation of a single pixel may have a greater impact on the matching and will also affect the accuracy of the matching. The size of the r value is also set by oneself. For example Figures 10 - 12 As shown Figure 10 is the template image Figure 11 is the schematic diagram of the matching result when the r value is 0.5 Figure 12 is the schematic diagram of the matching failure result when the r value is 1
[0056] At the same time, in order to improve the accuracy and stability
[0057] When |k t | > 1, at this time, k t and k s should be taken as the reciprocal at the same time, that is, the formula becomes:
[0058] N = |1 / k t - 1 / s | + N
[0059] Specifically, when |1 / t - 1 / s | > r, also let |1 / t - 1 / s | = r. This operation is to extract more difference information between two points on the premise of ensuring a certain accuracy. The premise of performing this step is to judge whether the numerator of k s is not 0 (the denominator of k s itself can be 0 because the reciprocal is to be taken). If the numerator of k s is 0, the calculation of this pixel point at this step is cancelled.
[0060] The operation of determining whether |k t | > 1 can be judged before the search and matching, that is, if |k t | > 1, the relevant information of 1 / t can be calculated before the search and matching, or the judgment of whether |k t | > 1 can be made during the search and matching and then the corresponding information can be calculated. In this embodiment, and in all examples in this technical report, the latter method is adopted.
[0061] If k t and k s or 1 / t and 1 / k s are successfully calculated, then calculate the effective calculation times h:
[0062] h = h + 1;
[0063] Each time k is successfully calculated t and k s or 1 / k t and 1 / s , calculate the value of N once. The value of N represents the degree of similarity. The smaller N is, the higher the similarity; the larger N is, the lower the similarity. This stage can be accelerated.
[0064] Specifically, a determination is made when the current value of h is equal to the value of a*p, where the range of p is between 0 and 1, and the value of a*p is the number of key pixels used to calculate the similarity in advance.
[0065] When performing accelerated calculation, it is necessary to calculate the matching degree M. The larger M is, the lower the matching degree; the closer it is to 0, the higher the matching degree. The formula is as follows:
[0066] M = N / (a*p)
[0067] If M > the preset threshold Y, then exit the calculation, and do not calculate the subsequent key pixels, so as to achieve the acceleration effect. In Example 1 of this embodiment, the preset threshold is set to 0.1, and the preset threshold of p is It should be noted that: Y and p are set by oneself. If their values are too high, a good acceleration effect cannot be achieved; if they are too low, the matching accuracy will be affected.
[0068] If M < the preset threshold Y, then continue to calculate the similarity on the key pixels in this search area.
[0069] It should be noted that the preset threshold Y should also be less than r.
[0070] Finally, divide N by the number of effective calculations in each search area, that is, take the average value of the value of N. The formula is as follows:
[0071] N = N / h;
[0072] At this time, the value of N ranges from 0 to r. In this embodiment, the value of N ranges from 0 to 1 because the value of r in this embodiment is 1. If the acceleration strategy is not adopted, the anti-noise ability of this embodiment of the present invention is stronger.
[0073] Step S5 of the embodiment of the present invention specifically includes:
[0074] Find the area with the highest similarity in the top-layer image, record the coordinates of the upper left corner of this area, and transfer these coordinates to the next-layer image. Specifically:
[0075] (X new , Y new ) = (2*Xold , 2 * Y old )
[0076] where (X old , Y old ) represents the best matching coordinates of the top - layer image layer, and (X new , Y new ) represents the coordinates of the next - layer image.
[0077] Enter the non - top - layer matching stage. Using the formula of normalized cross - correlation within a range of 5 * 5 centered on (X new , Y new ), calculate and find the region with the highest similarity. The specific formula is as follows:
[0078]
[0079] t represents the pixel size of each pixel point in the template image participating in the calculation, s represents the pixel size of each pixel point in the search image participating in the calculation, t mean represents the average value of the pixels in the template image participating in the calculation, s mean represents the average value of the pixels in the search image participating in the calculation, and n represents the similarity value calculated by this formula. The larger n is, the higher the similarity.
[0080] It should be noted that the non - top - layer matching calculation is performed on the key pixel points of the image layer.
[0081] In the non - top - layer matching stage, find the coordinate point with the highest similarity n value, record it and pass it to the next - layer image layer. Each time searching in the new image layer is performed within a range of 5 * 5 centered on the coordinates passed from the previous - layer image layer until the bottom - most image layer.
[0082] When matching to the bottom - most layer of the pyramid, find the region with the highest similarity in the bottom - most layer and mark it. The template matching ends.
[0083] Figures 4 - 6 This is the specific example two of the embodiments of the present invention. Figure 4 is the template image of the specific example two. Figure 5 is the search image of the specific example two. Figure 6 is the schematic diagram of the matching effect of the specific example two. In the specific example two, excluding the time for image pyramid processing and determining key pixel points of the search image and the template image, the search time during the experiment using the template matching method provided by the embodiments of the present invention is 6 ms. At this time, the preset thresholds from the top layer to the bottom layer are 200, 200, 200, 120 respectively. In addition, the preset thresholds Y and a are 0.1 and the setting of the r value is 1.
[0084] Figures 7 - 9 This is the third specific example of the embodiments of the present invention. Figure 7 It is the template image of the third specific example. Figure 8 The search image of the third specific example. Figure 9 It is the schematic diagram of the matching effect of the third specific example. At this time, the preset thresholds from the top layer to the bottom layer are 200, 200, 200, and 120 respectively. In addition, the preset thresholds Y and a are 0.5 and The r value is set to 1. Excluding the time for image pyramid processing of the search image and the template image and determining the key pixel points, the search time during the experiment using the template matching method provided by the embodiments of the present invention is 9 ms. The matching result shows that this matching method has the ability to resist illumination noise.
[0085] Adopting the embodiments of the present invention has the following beneficial effects:
[0086] The present invention applies the image pyramid technology, determines the key pixel points of all image layers of the template image, and then at the key pixel points of the top-layer image layer, compares the surrounding pixel features at the key pixel points of the template image with the surrounding pixel features at the corresponding pixel positions in the search area of the search image, that is, subtracts the appropriate slopes of the two and takes the absolute value, and limits it within a range, and then sums these values. This stage can be accelerated, and finally the area with the largest similarity is found, and the best matching coordinates are passed to the next lower image layer. At this time, the top-layer matching stage ends. In the non-top-layer matching stage, the similarity is calculated at the key pixel points through the normalized cross-correlation formula, searched layer by layer, and then the target is found and marked at the bottom layer of the search image pyramid. The template matching method of the present invention has the ability to resist illumination noise and a high matching rate.
[0087] System embodiments
[0088] The embodiments of the present invention provide a template matching system. Figure 2 It is the schematic diagram of the template matching system of the embodiments of the present invention. The template matching system of the embodiments of the present invention specifically includes:
[0089] The grayscale module 21 is used to convert the template image and the search image into grayscale images.
[0090] The image layer division module 22 is used to divide the template image and the search image converted into grayscale images into F image layers respectively by using the image pyramid technology.
[0091] The key pixel point finding module 23 is used to set the variance threshold of each image layer of the template image, and determine the key pixel points of each image layer of the template image according to the variance threshold, wherein the variance threshold of the upper image layer is less than or equal to the variance threshold of the lower image layer.
[0092] The top - layer image layer matching module 24 is used to obtain the template pixel slope according to the key pixel points of the top - layer image layer of the template image, denoted as k t , and perform the same operation on the pixel value at the corresponding pixel of the search image as on the template image to obtain the search pixel slope, denoted as k s , calculate the similarity of the top - layer image layer according to the template pixel slope and the search pixel slope, and obtain the region with the highest similarity;
[0093] The non - top - layer image layer matching module 25 is used to obtain the region with the highest similarity of the non - top - layer within a specific pixel range centered on the coordinates of the region with the highest similarity by using the normalized cross - correlation method;
[0094] The bottom - layer image layer matching module 26: is used to obtain the region with the highest similarity in the bottom - layer template image and mark it, so as to obtain the matching region.
[0095] The top - layer image layer matching module in the embodiment of the present invention is specifically used for:
[0096] Calculate the image features of the eight surrounding pixels centered on the key pixel points on the key pixel points of the top - layer image layer of the template image;
[0097] According to the pixel values of the eight pixel points, obtain two key point coordinates, coordinate A(t1 + t2 + t3, t3 + t4 + t5), coordinate B(t5 + t6 + t7, t7 + t8 + t1), and calculate the template pixel slope k through formula 1 t ;
[0098] k t =(By - Ay) / (Bx - Ax) formula 1;
[0099] Wherein, Ax and Ay represent the abscissa and ordinate of coordinate A, Bx and By represent the abscissa and ordinate of coordinate B, and t1 to t8 respectively represent the pixel values of the eight surrounding pixels centered on the key pixel points;
[0100] If the denominator of k t is 0, then take the reciprocal. If the denominator is still 0, then cancel the calculation of the corresponding key pixel point, and obtain the search pixel slope k in the same way s ; If k t took the reciprocal before because the denominator was 0 and the numerator was not 0, then if k s taking the reciprocal makes sense (at this time, do not determine whether the denominator of k s is 0), that is, the denominator of the reciprocal of k s is not 0, also take the reciprocal of k s , and then perform the following operations. The k of the template image tIt can be calculated in advance before search matching to achieve the effect of improving speed, or it can be calculated together with k during search matching t and k s , in this embodiment, and in all examples in this technical report, the latter method is adopted.
[0101] When |k t | < 1, the similarity N between the template image and the search area of the search image is calculated according to Formula 2:
[0102] N = |k t - k s | + N Formula 2;
[0103] |k t - k s | is the way to compare the peripheral pixel features at the key pixel points of the template image with the corresponding pixel positions of the search area of the search image. Among them, if |k t - k s | > r, then let |k t - k s | = r. The value of N in this formula is the sum of the current |k t - k s |. In this example, the value of r is set to 1. This operation helps to improve the accuracy of matching, that is, to reduce the influence of a single pixel point on the overall matching. It should be noted that the setting of the r value should be reasonable. If the r value is too small, there will not be enough difference information, which will affect the accuracy of matching. If the r value is too large, the calculation of a single pixel point may have a greater impact on the matching and will also affect the accuracy of matching. The size of the r value is also set by oneself.
[0104] |k t | > 1, the similarity N between the template image and the search area of the search image is calculated according to Formula 3:
[0105] N = |1 / k t - 1 / k s | + N Formula 3;
[0106] Among them, the initial value of N is set to 0.
[0107] When |1 / t - 1 / k s | > r, also let |1 / t - 1 / k s | = r. This operation is to extract more difference information between two points on the premise of ensuring a certain accuracy. The premise of performing this step is to judge whether the numerator of k s is not 0 (the denominator of k s itself can be 0 because the reciprocal is to be taken). If ks If the numerator is 0, the calculation of the pixel point at this position in this step is cancelled. Determine whether |k t |>1 can be judged before the search and match. That is, if |k t |>1, 1 / t The relevant information can be calculated before the search and match, or it can be calculated by judging whether |k t |>1 during the search and match and then calculating the corresponding information. In this embodiment, and in all examples in this technical report, the latter method is adopted.
[0108] If k is successfully calculated t and k s or 1 / t and 1 / k s , then calculate the effective calculation times h:
[0109] h = h + 1;
[0110] Every time k t and k s or 1 / k t and 1 / k s is successfully calculated, calculate the N value once. The N value represents the degree of similarity. The smaller the N value, the higher the similarity; the larger the N value, the lower the similarity. This stage can be accelerated.
[0111] The non-top image layer matching module in the embodiment of the present invention is specifically used for:
[0112] Obtain the coordinates of the region with the highest similarity in the non-top layer according to Formula 4:
[0113] (X new , Y new ) = (2 * X old , 2 * Y old ) Formula 4;
[0114] Among them, (X old , Y old ) represents the best matching coordinates of the top image layer, and (X new , Y new ) represents the coordinates of the next layer of the image;
[0115] Use the formula of normalized cross-correlation with (X new , Y new ) as the center and within the range of specific pixels, and calculate and find the region with the highest similarity through Formula 5;
[0116]
[0117] Among them, t represents the pixel size of each pixel point participating in the calculation of the template image, s represents the pixel size of each pixel point participating in the calculation of the search image, and t mean represents the average value of the pixels of the template image participating in the calculation, and s mean represents the average value of the pixels of the search image participating in the calculation, and n represents the similarity size calculated by this formula. The larger n is, the higher the similarity is.
[0118] The top - layer image layer matching module further includes: an acceleration sub - module. The acceleration sub - module is specifically used to preset a matching degree threshold Y and obtain a matching degree M through Formula 6:
[0119] M = N / (a * p) Formula 6;
[0120] Among them, the value of a * p is the number of key pixel points for calculating similarity using key pixel points in advance.
[0121] When the number of calculations reaches the set value, if M > Y, then the calculation is exited, and the subsequent key pixel points do not need to be calculated, so as to achieve an acceleration effect. In this embodiment, the preset threshold is set to 0.1, and the preset threshold of p is It should be noted that: Y and p are set by oneself. If their values are too high, a good acceleration effect cannot be achieved; if they are too low, the matching accuracy is affected.
[0122] Otherwise, continue to calculate the similarity on the key pixel points of the corresponding image layer;
[0123] It should be noted that the preset threshold Y should also be less than r.
[0124] Finally, divide N by the number of effective calculations in each search area, that is, take the average value of the value of N. The formula is as follows:
[0125] N = N / h;
[0126] At this time, the value of N ranges from 0 to r. In this embodiment, the value of N ranges from 0 to 1 because the value of r in this embodiment is 1.
[0127] If the acceleration strategy is not adopted, the anti - noise ability of the embodiment of the present invention is stronger.
[0128] Device Embodiment 1
[0129] The embodiment of the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. The feature is that when the processor executes the computer program, it implements the steps of the above - mentioned method embodiment.
[0130] Device Embodiment 2
[0131] A computer-readable storage medium stores a computer program, characterized in that when the computer program is executed by a processor, the steps of the foregoing method embodiments are implemented.
[0132] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements for some or all of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A template matching method, characterized in that, Including: S1. Grayscale stage: Convert the template image and the search image into grayscale images. S2. Image layer division stage: Use the image pyramid technology to divide the template image and the search image converted into grayscale images into F image layers respectively. S3. Finding key pixel points stage: Set the variance threshold for each image layer of the template image, and determine the key pixel points of each image layer of the template image according to the variance threshold, where the variance threshold of the upper image layer is less than or equal to the variance threshold of the lower image layer. S4. Top - layer image layer matching stage: Obtain the template pixel slope according to the key pixel points of the top - layer image layer of the template image, denoted as , and perform the same operation on the pixel value at the corresponding pixel of the search image as on the template image to obtain the search pixel slope, denoted as . Calculate the similarity of the top - layer image layer based on the template pixel slope and the search pixel slope, and obtain the region with the highest similarity; S5. Non-top layer image layer matching stage: Use the normalized cross-correlation method to obtain the region with the highest similarity in the pixel range centered on the coordinates of the region with the highest similarity. S6. Bottom layer image layer matching stage: Obtain the region with the highest similarity in the bottom layer template image in the manner of step S5 and mark it, so as to obtain the matching region.
2. The method according to claim 1, wherein The specific steps of step S4 include: Calculate the image features of the eight surrounding pixel points centered on the key pixel points on the top layer image layer of the template image. Obtain two key point coordinates based on the pixel values of the eight pixel points, coordinate A( t 1 + t 2 + t 3, t 3 + t 4 + t 5), coordinate B( t 5 + t 6 + t 7, t 7 + t 8 + t 1), calculate the template pixel slope through formula 1 ; =(By - Ay) / (Bx - Ax) Formula 1; Where Ax and Ay represent the abscissa and ordinate of coordinate A, Bx and By represent the abscissa and ordinate of coordinate B, and t1 to t8 respectively represent the pixel values of the eight surrounding pixel points centered on the key pixel points. If has a denominator of 0, then and the search pixel slope are inverted. If after inversion, the denominator of or the denominator of is 0 for one or more of them, then the calculation of the corresponding key pixel points is cancelled. Otherwise, the subsequent calculation is performed using their reciprocals. Among them, the calculation method of the search pixel slope is the same as the calculation method of ; When | | < 1, calculate the similarity N between the template image and the search area of the search image according to Formula 2: N = | - | + N formula 2; | | When it is >1, calculate the similarity N between the template image and the search area of the search image according to Formula 3: N = | - | + N formula 3; Where the initial value of N is set to 0, the larger N is, the lower the matching similarity is, and the smaller N is, the higher the matching similarity is. When the calculation of each region is completed, the similarity N should be set to 0.
3. The method according to claim 1, characterized in that The specific steps of step S5 include: Obtain the coordinates of the region with the highest similarity in the non-top layer according to formula 4: Formula 4; Among them, represents the best matching coordinates of the top image layer, represents the coordinates of the next image layer; Taking as the center and using the formula of normalized cross-correlation within the range of pixels, calculate through Formula 5 and find the region with the highest similarity; Formula 5; Among them, t represents the pixel size of each pixel point in the template image participating in the calculation, and s represents the pixel size of each pixel point in the search image participating in the calculation. represents the average value of the pixels in the template image participating in the calculation. represents the average value of the pixels in the search image participating in the calculation, and n represents the similarity size calculated by this formula. The larger n is, the higher the similarity is.
4. The method according to claim 2, wherein An acceleration strategy is further adopted in step S4 to accelerate the matching progress. The acceleration strategy specifically includes: Preset a matching degree threshold Y, and obtain the matching degree M through formula 6: M = N / (a * p) Formula 6; If M > Y when the number of calculations reaches the set value, then exit the calculation; otherwise, continue to calculate the similarity on the key pixel points of the corresponding image layer. Where the value of a * p is the number of key pixel points used to calculate the similarity with the key pixel points in advance.
5. A template matching system, characterized in that, Including: A grayscale module for converting the template image and the search image into grayscale images. An image layer division module for using the image pyramid technology to divide the template image and the search image converted into grayscale images into F image layers respectively. A finding key pixel points module for setting the variance threshold for each image layer of the template image and determining the key pixel points of each image layer of the template image according to the variance threshold, where the variance threshold of the upper image layer is less than or equal to the variance threshold of the lower image layer. The top - layer image layer matching module is used to obtain the template pixel slope according to the key pixel points of the top - layer image layer of the template image, denoted as , and perform the same operation on the pixel value at the corresponding pixel of the search image as on the template image to obtain the search pixel slope, denoted as , calculate the similarity of the top - layer image layer according to the template pixel slope and the search pixel slope, and obtain the region with the highest similarity; A non-top layer image layer matching module for using the normalized cross-correlation method to obtain the region with the highest similarity in the non-top layer in the pixel range centered on the coordinates of the region with the highest similarity. A bottom layer image layer matching module for obtaining the region with the highest similarity in the bottom layer template image and marking it, so as to obtain the matching region.
6. The system according to claim 5, wherein The top layer image layer matching module is specifically used for: Calculate the image features of the eight surrounding pixel points centered on the key pixel points on the top layer image layer of the template image. Obtain two key point coordinates based on the pixel values of the eight pixel points, coordinate A( t 1 + t 2 + t 3, t 3 + t 4 + t 5), coordinate B( t 5 + t 6 + t 7, t 7 + t 8 + t 1), and calculate the template pixel slope through formula 1 ; =(By - Ay) / (Bx - Ax) Formula 1; Among them, Ax and Ay represent the abscissa and ordinate of coordinate A, Bx and By represent the abscissa and ordinate of coordinate B, and t1 to t8 respectively represent the pixel values of the eight surrounding pixel points centered on the key pixel points; If has a denominator of 0, then and the search pixel slope are inverted. If after inversion one or more of the denominators of or is 0, then the calculation of the corresponding key pixel point is cancelled; otherwise, the subsequent calculation is performed using their reciprocals. The calculation method of the search pixel slope is the same as that of ; When | | < 1, calculate the similarity N between the template image and the search area of the search image according to Formula 2: N = | - | + N formula 2; | |When it is >1, calculate the similarity N between the template image and the search area of the search image according to Formula 3: N=| - |+ N formula 3; Among them, the larger N is, the lower the matching similarity is, and the smaller N is, the higher the matching similarity is. When the calculation of each area is completed, the similarity N should be set to 0.
7. The system according to claim 5, characterized in that, The non-top image layer matching module is specifically used for: Obtaining the coordinates of the area with the highest similarity in the non-top layer according to Formula 4: Formula 4; Among them, represents the best matching coordinates of the top image layer, represents the coordinates of the next layer of the image; Centered around and using the formula of normalized cross-correlation within the range of pixels, calculate and find the region with the highest similarity through Formula 5; Formula 5; Among them, t represents the pixel size of each pixel point of the template image participating in the calculation, and s represents the pixel size of each pixel point of the search image participating in the calculation. represents the average value of the pixels of the template image participating in the calculation. represents the average value of the pixels of the search image participating in the calculation, and n represents the similarity size calculated by this formula. The larger n is, the higher the similarity is.
8. The system according to claim 6, wherein The top image layer matching module further includes: an acceleration sub-module, and the acceleration sub-module is specifically used to preset a matching degree threshold Y and obtain a matching degree M through Formula 6: M = N / (a * p) Formula 6; If the number of calculations reaches the set value and M > Y, then exit the calculation; otherwise, continue to calculate the similarity at the key pixel points of the corresponding image layer; Among them, the value of a * p is the number of key pixel points used to calculate the similarity with the key pixel points in advance.
9. A computer device, 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 method described in the template matching method according to any one of claims 1 to 4.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by the processor, it implements the method described in the template matching method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Template matching method, system and device based on normalized cross-correlation and medium
CN113205145A
Detection device, disparity value deriving device, object recognizing device, device control system, detection method, and program
EP3115965A2