Fast template matching method, device and computer-readable storage medium
Through the pyramid strategy and the SSE2 instruction set optimization matching process, the problems of large and slow computing in the existing technology are solved, and efficient and accurate image matching and positioning are achieved, which is suitable for machine vision systems of high-resolution cameras.
Patent Information
- Application Number
- CN202110514522.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-11
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2041-05-11
AI Technical Summary
The existing matching method based on contour edge gradients is large in processing super-large images, and the calculation data is large, the time is long, and the memory is consumed, which cannot meet the needs of high-precision positioning.
The pyramid strategy is adopted, combined with the SSE2 instruction set and the golden segmentation speed coefficient, the matching results are updated through non-exact matches, and the matching process is optimized using normalized gradient direction and edge point number integral graphs to improve the matching speed and accuracy.
It greatly improves the matching speed between non-top-level pyramid images and template images, ensures high-precision positioning, reduces computing and memory consumption, and adapts to the positioning needs of high-resolution cameras.
Smart Images

Figure CN115410013B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of machine vision, and more specifically, to a fast template matching method, device, and computer-readable storage medium. Background Art
[0002] In various automation equipment, directly using mechanical structures to provide high-precision positioning requires very high machining, installation and debugging requirements, and is costly. Installing a vision system in automation equipment and using machine vision to provide positioning offers advantages such as low cost, simple application, high flexibility, and strong adaptability.
[0003] In machine vision systems, commonly used matching and positioning methods include grayscale-based, feature point-based, and contour-based matching methods. Grayscale-based matching methods directly use grayscale information, which is computationally intensive, slow, and requires high image consistency. Feature point-based matching methods require feature points with specific structures, resulting in complex calculations and limited versatility. Contour-based matching technology uses contour edge information, requiring less computation, providing stable matching results, and achieving higher accuracy.
[0004] Contour-based matching technology is the most commonly used positioning method. Because the image's contour edges contain the most crucial information, they are not easily disturbed by various internal and external factors, and have high stability and consistency. The image's edge gradients are also very convenient for handling translation, scaling, rotation, and other situations, eliminating the need for complex image operations and requiring only simple geometric transformations. Using the edge gradients of image contours for matching and positioning is the most common and effective technique among various current matching and positioning methods. Due to its simple computation, strong anti-interference capabilities, stable and reliable matching results, ease of parallel acceleration, and simple handling of rotation and scaling, it has become the most universal standard method in the field of machine vision.
[0005] like Figure 1 As shown, the existing matching and positioning scheme using the edge gradient of the image contour includes offline training and online matching processes. In the offline training process, a template image containing specific image features undergoes a training process to obtain template information; in the online matching process, the above template information and the target image are used to undergo a matching process, and finally the matching position of the template feature in the target image is obtained.
[0006] At present, the positioning and pick-and-place applications of automated equipment place increasingly higher demands on the accuracy of machine vision systems. High-resolution cameras, including those with a resolution of more than 10 million and even more than 20 million, are increasingly used.
[0007] Existing contour edge gradient matching methods often use a pyramid strategy to accelerate processing of very large images. This has become a common method for acceleration. This method requires layering both the template and target images, then performing layer-by-layer matching from high to low. Furthermore, each layer requires calculating the gradient direction and magnitude, as well as performing contour matching point by point and region by region. This results in a large amount of computational data, a long time, and high memory consumption. Summary of the Invention
[0008] The embodiments of the present invention provide a fast template matching method, device and computer-readable storage medium to address the problems of large amount of data calculation, long time and large memory consumption in the template process of the above-mentioned contour-based matching and positioning solution.
[0009] The technical solution to the above technical problem provided by the embodiment of the present invention is to provide a fast template matching method, comprising:
[0010] Creating a plurality of layers of pyramid images corresponding to the plurality of layers of template images according to the target image, wherein each layer of the template image includes a plurality of edge position points of the image to be recognized and the corresponding gradient direction;
[0011] Fully matching the top pyramid image with the top template image to obtain a plurality of matching results, each of which includes a position, a rotation angle, and a scaling value;
[0012] In each non-top pyramid image, using the matching result of the previous layer to perform incomplete matching with the template image of the corresponding layer and updating the matching result;
[0013] Obtaining the position of the image to be identified in the target image according to the matching result corresponding to the bottom-level pyramid image;
[0014] In each non-top pyramid image, the matching result of the previous layer is used to perform incomplete matching with the template image of the corresponding layer to update the matching result, including performing the following steps on each matching result of the previous layer:
[0015] Calculate the gradient direction of each pixel in the current layer pyramid image and the preset neighborhood relative to the current matching result, and normalize the gradient direction of each pixel into a single-byte expression;
[0016] According to the normalized gradient direction, the SSE2 instruction set is used to perform matching score calculation on the position corresponding to the current matching result and each position in the preset area;
[0017] When the maximum value of the matching scores of all positions is greater than or equal to the preset score of the current layer, the position corresponding to the maximum value of the matching scores will replace the current matching result; when the maximum value of the matching scores is less than the preset score of the current layer, the current matching result will be deleted.
[0018] As a further optimization of the present invention, the matching score calculation is performed on the position corresponding to the current matching result and each position in the preset range using the SSE2 instruction set according to the normalized gradient direction, including:
[0019] Adjacent rows are aligned crosswise and the SSE2 instruction set is used to perform matching score calculations on the position corresponding to the current matching result and each row in the preset area.
[0020] As a further optimization of the present invention, the completely matching the top pyramid image with the top template image includes:
[0021] Obtaining the gradient magnitudes of all pixels in the top pyramid image;
[0022] Before any to-be-matched position in the top pyramid image is matched with the top template image, obtaining a total number of pixels that meet a preset requirement within an area covered by the top template image, wherein the preset requirement is that a gradient amplitude is greater than or equal to a preset gradient amplitude;
[0023] When the total number of pixels meeting the preset requirements within the coverage area of the top template image is greater than a preset number, the to-be-matched position in the top pyramid image is matched with the top template image; otherwise, the to-be-matched position is directly ignored.
[0024] As a further optimization of the present invention, obtaining the total number of pixels meeting preset requirements within the coverage area of the top-level template image includes:
[0025] generating an edge point number integral map according to the gradient amplitude of the pixel points in the top pyramid image, wherein each value in the edge point number integral map represents the total number of pixel points that meet preset requirements in all areas above and to the left of each corresponding pixel point;
[0026] According to the edge point number integral map, the total number of pixel points that meet preset requirements within the coverage area of the top template image at the position to be matched is generated.
[0027] As a further optimization of the present invention, generating an edge point number integral map according to the gradient amplitudes of the pixels in the top pyramid image includes:
[0028] Use the following calculation formula to scan each pixel of the top pyramid image from top to bottom and from left to right and generate each value in the edge point number integral map:
[0029] Intregral(i,j)=Intregral(i-1,j)+Intregral(i,j-1)-Intregral(i-1,j-1)+(Mi(i,j)>ThresEdgeMin)
[0030] Where Intregral(i,j) is the value at the i-row and j-column position in the edge point integral map, and Mi(i,j)>ThresEdgeMin indicates whether the edge gradient information of the pixel in the i-row and j-column of the top-level pyramid image meets the preset requirements. If the requirements are met, the value is 1; otherwise, the value is 0.
[0031] As a further optimization of the present invention, generating the total number of pixels meeting preset requirements within the coverage area of the top template image at the position to be matched based on the edge point number integral map includes:
[0032] The following calculation formula is used to obtain the total number of pixels that meet the preset requirements within the coverage area of the top template image at the position to be matched:
[0033] Ni=Intregral(p+r,q+r)-Intregral(pr,q+r)-Intregral(p+r,qr)+Intregral(pr,qr)
[0034] The position to be matched is located at the pth row and the qth column of the top pyramid image, and the radius of the minimum circumscribed enclosing circle of the edge pixel point set of the top template image is r.
[0035] As a further optimization of the present invention, in each non-top pyramid image, using the matching result of the previous layer to perform incomplete matching with the template image of the corresponding layer and updating the matching result includes calculating the preset score of the current layer by the following calculation formula:
[0036]
[0037] Wherein, SpeedRatio is a preset speed ratio ranging from 0 to 1; lm is the total number of layers of the pyramid image contained in the target image; and level is the layer number of the current pyramid image.
[0038] As a further optimization of the present invention, the method includes:
[0039] For each matching result in the bottom-level pyramid image, the optimal sub-pixel position of the matching position is iteratively calculated using the following formula:
[0040] Among them, the edge gradient direction homogeneous vector of the underlying template image is Sj(Xj, Yj, 1), the edge gradient direction homogeneous vector corresponding to Sj at the current matching position is Dj(Xi, Yi, 1), and the affine transformation matrix of the current matching position is a1 is the sine value of the rotation amount of the matching position, a2 is the cosine value of the rotation amount of the matching position, b1 is the translation value of the matching position in the first direction, and b2 is the translation value of the matching position in the second direction.
[0041] An embodiment of the present invention further provides a fast template matching device, comprising a memory and a processor, wherein the memory stores a computer program executable in the processor, and the processor implements the steps of the fast template matching method described above when executing the computer program.
[0042] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the steps of the fast template matching method described above.
[0043] The fast template matching method, device, and computer-readable storage medium of the present invention utilize the SSE2 instruction set to perform matching score calculations on matching locations and corresponding locations within a preset range, significantly improving the matching speed of non-top-level pyramid images and corresponding template images. Furthermore, by utilizing the golden ratio speed coefficient and granularity control method during the matching process, the present invention eliminates the effects of contour edge deformation on matching while maintaining matching speed, achieving high-precision matching. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 It is a schematic diagram of a matching and positioning scheme using the edge gradient of the image contour;
[0045] Figure 2 1 is a flow chart of a fast template matching method provided by an embodiment of the present invention;
[0046] Figure 3 Schematic diagram of each layer of pyramid images and each layer of template images in the fast template matching method provided by an embodiment of the present invention;
[0047] Figure 4 1 is a schematic diagram of a process for matching a top-level pyramid image in a fast template matching method provided by an embodiment of the present invention;
[0048] Figure 5 1 is a schematic diagram of a process for preprocessing a top-level pyramid image before matching the top-level pyramid image in a fast template matching method provided by an embodiment of the present invention;
[0049] Figure 6 is an integral graph of the number of edge points in the fast template matching method provided by an embodiment of the present invention;
[0050] Figure 7 1 is a schematic diagram of a process for matching non-top-level pyramid images in a fast template matching method provided by an embodiment of the present invention;
[0051] Figure 8 1 is a schematic diagram of area matching when matching non-top pyramid images in the fast template matching method provided by an embodiment of the present invention;
[0052] Figure 9 2 is a schematic diagram of a fast template matching device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0053] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0054] The fast template matching method of the present invention can be applied to a machine vision system based on edge gradient matching. Based on the traditional pyramid strategy matching, the method uses the SSE2 (Streaming SIMD Extensions 2) acceleration algorithm to match non-top-layer pyramid images with non-top-layer template images. While obtaining the precise position of the template image in the target image, the matching speed of the non-top layer is improved.
[0055] like Figure 2 FIG. 1 is a flow chart of a fast template matching method provided by an embodiment of the present invention. The method matches a target image with multiple pyramid-shaped template images to obtain the position of the template images in the target image. The template images are formed by a series of processing of the image to be identified, and each template image includes a number of edge pixels, each of which includes a position and a corresponding gradient direction. Specifically, the method of this embodiment includes the following steps:
[0056] Step S11: Based on the target image, several layers of pyramid images are created, each corresponding to the layers of template images. Each layer of the template image includes several edge locations of the image to be recognized and the corresponding gradient directions. In other words, the pyramid image for the target image is generated in the same manner as the pyramid image for the template image. The number of layers in the pyramid image for the target image is the same as that in the pyramid image for the template image.
[0057] For example Figure 3The image to be identified shown includes 5 layers of template images, and correspondingly, the target image also includes 5 layers of pyramid images. Specifically, the target image can be generated by shooting with a camera, and contains specific image features.
[0058] Step S12: fully matching the top pyramid image with the top template image to obtain a number of matching results, each of which includes a position, a rotation angle, and a scaling value.
[0059] Specifically, in this step, the Sobel operator is used to calculate the gradient direction Ei = (Dx, Dy) for each pixel in the top-level pyramid image. Matching is performed within a preset parameter space, which includes an angle range and a scaling range. For example, the angle range can be set in advance between Angle_min and Angle_max, and the scaling range can be set between Scale_min and Scale_max. The angle step and scaling step are also set. Then, for each angle value a and scaling value s combination within the parameter space, the gradient direction of the edge pixel on the top-level template image is transformed. A matching score is then calculated for each pixel in the top-level pyramid image, thereby fully matching the top-level pyramid image within the entire preset parameter space. Several matching results are obtained, which are stored in a matching result table ListTr.
[0060] Step S13: In each non-top pyramid image, use the matching result of the previous layer to perform incomplete matching with the template image of the corresponding layer and update the matching result.
[0061] After completing the matching process at the top pyramid layer, similar steps are used to match each value in the matching result table ListTr at the remaining layers of the pyramid image. In non-top pyramid images, matching is typically performed within a small range of the corresponding value in the matching result table ListTr. For example, this small range can be a 3×3 or 9×9 neighborhood. Gradient information and matching operations are calculated within this small range. This process is gradually refined from top to bottom, all the way to the bottom pyramid image, ensuring that every matching result is matched in the original target image.
[0062] Step S14: The position of the image to be identified in the target image is obtained based on the matching results corresponding to the bottom-level pyramid image, completing the identification. Specifically, each matching result corresponding to the bottom-level pyramid image of the target image corresponds to the position of each image in the target image that is identical to the image to be identified, as well as the corresponding rotation angle and scaling value.
[0063] Combine Figure 4FIG. 1 is a flow chart of the top-level pyramid image matching process in the fast template matching method provided by an embodiment of the present invention. In this embodiment, the top-level pyramid image is matched completely, which includes the following steps:
[0064] Step S121: In the top-level pyramid image of the target image, the Sobel operator is used to calculate the gradient direction Ei = (Dx, Dy) for each pixel. To speed up the subsequent matching of non-top-level pyramid images, the gradient direction Ei = (Dx, Dy) for each pixel is normalized in this step so that each value of the gradient direction for each pixel can be represented by a single byte.
[0065] Specifically, normalization can be performed so that the maximum values of the two values of the gradient direction are 120. For example, the gradient direction Ei=(Dx, Dy) is normalized by the following calculation formula (1):
[0066]
[0067] Wherein, Ei'(Dx', Dy') is the gradient direction after normalization of the gradient direction Ei=(Dx, Dy).
[0068] Because gradient directions can be both positive and negative, the range of Dx' and Dy' is [-120, 120]. This allows them to be represented using the signed char type on existing computers. Each data point occupies only one byte, significantly reducing memory usage and providing high accuracy. Furthermore, the normalized gradient directions are very convenient for acceleration using the Intel instruction set. Specifically, when performing incomplete matches on non-top pyramid images, SSE2 (Streaming SIMD Extensions 2) can be used for rewriting and acceleration. This theoretically provides a 16x speedup over the original algorithm, and in practice, over 4x. The specific computational process will be described in detail later.
[0069] In addition, the square root operation in the process of solving Dxy in Equation (1) is relatively time-consuming. This square root operation is not only used in offline calculations, but also widely used in online matching calculations, which slows down the overall calculation speed. To solve this problem, the open operation results can be obtained through a table lookup method. That is, a table is set up to store values (for example, the value does not exceed 28800) and their corresponding square root results, and the corresponding square root result is obtained by searching the table based on the value.
[0070] Step S122: performing rotation transformation and scaling transformation on each edge pixel point in the top template image.
[0071] In this step, the angle range of the rotation transformation and the scaling range of the scaling transformation can be set in advance, for example, the angle range is Angle_min and Angle_max, the scaling range is Scale_min and Scale_max, and the step size of the angle transformation and the step size of the scaling transformation are also set at the same time. Specifically, the gradient direction Di = (Dix, Diy) of each edge pixel point on the top template image can be transformed for each angle value a and scaling value s within the preset range using the following calculation formula (2). Assume that the transformed template gradient direction is Di' = (Dix', Diy'):
[0072]
[0073] Step S122: Calculate the matching score S for each position in the top pyramid image, and record the matching angle and scaling value when the matching score is the maximum.
[0074] In this step, the matching formula for the top pyramid image is the dot product Di'·Ei' of the template gradient direction and the target image gradient direction, as shown in the following formula (3). The sum of the dot products of the gradient directions of all edge pixels on the top template image and the pixels in the top pyramid image of the corresponding target image is the matching score S at the corresponding position:
[0075]
[0076] n is the number of edge pixels in the top template image. From the above formula (3), we can see that Di' and Ei' are both gradient information located on the edge of the top template image and the contour edge of the top pyramid image of the target image, that is, contour-based matching.
[0077] In this step, a maximum matching score map can be generated based on the calculation results of the above formula (3). The maximum matching score map includes records corresponding to each pixel in the top pyramid image of the target image. Each record consists of the maximum value of all matching scores at the corresponding pixel (these matching scores correspond to different angle values a and scaling values s), as well as the angle value a' and scaling value s' corresponding to the maximum value.
[0078] In this step, the matching score is calculated at each position of the top pyramid image according to the above formula (3) using the step size of the angle transformation and the step size of the scale transformation within the angle range Angle_min and Angle_max and the scale range Scale_min and Scale_max.
[0079] Step S124: traverse the highest matching score graph, obtain each local maximum, and when the local maximum is greater than the top preset score, use the position corresponding to the local maximum as the matching result.
[0080] When traversing the above-mentioned highest matching score graph, the 4-neighborhood method or the 8-neighborhood method can be used to obtain each local maximum. For example, when using the 4-neighborhood method, four pixel points (positions) can be taken as a group, and the pixel point with the highest matching score S among the four positions is selected. If the matching score S of the pixel point with the highest matching score is greater than the top-level preset score Smin (this value can be entered in advance), the pixel point with the highest matching score can be saved as the matching result in the matching result list ListTr. Each matching result in the matching result list ListTr includes the position and the corresponding rotation angle, scaling value, etc.
[0081] Considering that when matching all spatial locations in the top-level pyramid image of the target image, a large number of operations are invalid. For example, in the top-level pyramid image, there may be some areas with no edge points or too few edge points, making it impossible for a matching target to exist. In other words, the matching score at these locations cannot be greater than the top-level preset score Smin. Therefore, matching position by position results in a large amount of useless calculations. To address this, a minimum edge threshold parameter, ThresEdgeMin, can be set. This parameter is provided by the upper layer of the application (for example, set in advance). In the top-level target image, edge gradient information with a gradient magnitude less than this value is ignored, and only the gradient information of pixels with a gradient magnitude greater than this value is retained. In this way, before matching a certain location, it can be determined whether the number Ni of pixels that meet the preset requirements (i.e., pixels with a gradient magnitude greater than or equal to the minimum edge threshold parameter ThresEdgeMin) in the range near the location (i.e., the range covered by the top-level template image) reaches a preset number, i.e., Ni>Smin×Ndi, where Ndi is the number of edge pixels in the top-level template image.
[0082] Specifically, combined Figure 5 As shown, in one embodiment of the present invention, the top-level pyramid image can be pre-processed (i.e., positions where the image to be recognized is unlikely to exist) before matching the top-level pyramid image by the following method:
[0083] Step S125: Obtain the gradient magnitude Mi of all pixels in the top pyramid image. This step can be combined with the above Figure 4 Step S121 in the above step is executed simultaneously, that is, the gradient direction Ei and gradient magnitude Mi of each pixel point in the top pyramid image are calculated simultaneously using the Sobel operator.
[0084] Step S126: Before any to-be-matched position in the top pyramid image is matched with the top template image, the total number of pixels in the coverage area of the top template image that meet a preset requirement is obtained, where the preset requirement is that the gradient amplitude Mi is greater than or equal to a preset gradient amplitude (i.e., the minimum edge threshold parameter ThresEdgeMin).
[0085] Step S127: Determine whether the total number of pixels meeting the preset requirements within the area covered by the top template image at the position to be matched is greater than a preset number. Specifically, the preset number may be Smin × Ndi. If the total number of pixels meeting the preset requirements within the area covered by the top template image is greater than the preset number, execute step S128; otherwise, execute step S129.
[0086] Step S128: performing matching calculation on the position to be matched in the top pyramid image and the top template image, i.e., calculating the matching score at the position under different rotation angles and scaling values of the top template image, and then selecting the maximum matching score.
[0087] Step S129: directly ignore the position to be matched, that is, do not calculate the matching score for the position to be matched.
[0088] Through the above method, the matching operation of the top-level pyramid image can be greatly reduced.
[0089] In the above preprocessing process, determining the total number of pixels that meet the preset requirements position by position is very time-consuming and inconvenient to program. To solve this problem, we can use the edge point count integral map method to count the number of edge points. Using the edge point count integral map, only a single scan of the top image is required.
[0090] Specifically, combined Figure 6 As shown, the validity of the edge gradient information for the pixel in row i and column j in the top pyramid image can be expressed as Mi(i, j) > ThresEdgeMin. This is similar to the judgment in the C programming language, where the value is 1 if the condition is met and 0 if it is not. The edge point integral map stores the number of valid edge points in all areas to the upper left of each pixel. When generating the edge point integral map, assuming that the value at position i in row i and column j in the edge point integral map is Intregral(i, j), its value can be expressed as:
[0091]
[0092] According to the above formula (4), the calculation of the integral map of the number of edge points can be completed quickly by scanning the top pyramid image from top to bottom and from left to right once.
[0093] When making a matching judgment, if we need to know the number of effective edge gradients (i.e., the number of pixels Ni that meet the requirements) within a pixel range of 10 near the position of row i and column j in the image (the radius of the minimum circumscribed circle of the edge pixel set of the top template image is 5, i.e., the coverage of the top template image is a circle with a radius of 5), we can directly calculate it using the following formula (5):
[0094]
[0095] By utilizing this characteristic of the edge point integral map, we can quickly calculate whether the number of valid edge gradients at each matching position meets the requirements. When the number of points at a certain position does not meet the requirements, the matching operation involving a large amount of scaling and rotation of the template gradient information can be omitted at that position. This can avoid invalid operations in a large number of areas and speed up the matching operation of the top-level pyramid image.
[0096] Combine Figure 7 FIG. 1 is a flow chart illustrating the matching of non-top-level pyramid images in the fast template matching method provided by an embodiment of the present invention. After completing the matching at the top-level pyramid layer, similar steps are used to continue matching at other levels of the pyramid image for each matching result in the result list ListTr. Specifically, in each non-top-level pyramid image, the following steps are performed for each matching result of the previous level (i.e., the previous level matching result list ListTr):
[0097] Step S131: Calculate the gradient direction of each pixel in the current pyramid image layer within the preset neighborhood and the current matching result, and normalize the gradient direction of each pixel into a single-byte representation. Generally, the preset area can be a 3×3 or 9×9 area, and the calculation of gradient information and matching operations are performed within this small area. In addition, the gradient direction can be normalized using formula (1).
[0098] Specifically, when using a 3×3 neighborhood, if the upper-level matching position finds a slightly large position offset, it may not be able to effectively correct the position offset, resulting in an inability to match a valid target within the local range and causing matching failure; when using a 9×9 neighborhood, the neighborhood area can be fully covered.
[0099] Step S132: Based on the normalized gradient direction, use the SSE2 instruction set to perform a matching score operation on the position corresponding to the current matching result and each position within the preset area.
[0100] When using the SSE2 acceleration algorithm, the size of the read area must be 8 or a multiple of 8 to fully and effectively use the SSE2 instruction set acceleration. Since 8 is an even number, when aligning the center of the non-top pyramid image for matching, there will be 3 points on one side and 4 points on the other side, resulting in an imbalance on both sides. To solve the above problem, Figure 8 As shown, the adjacent rows can be aligned in an interleaved manner, that is, matching is performed only in the grayscale display area of the 9×9 neighborhood of the local position. This can cover a larger local matching range, improve the error correction capability of the upper-layer matching results, and also ensure left-right and top-bottom balance. It can also make full use of the requirement that the read size of the SSE2 instruction set must be a multiple of 8.
[0101] Step S133: Determine whether the maximum value of the matching scores at all positions is greater than or equal to the current layer preset score Slmin. If the above maximum value is greater than or equal to the current layer preset score Slmin, execute step S134; otherwise, execute step S135.
[0102] Step S134: Replace the current matching result in the matching result list ListTr with the position corresponding to the maximum value in the matching score.
[0103] Step S135: Delete the current matching result from the matching result list ListTr.
[0104] In the matching operation of non-top-level pyramid images, by using normalized gradient values and combining with the SSE2 instruction set, it is only necessary to traverse nine rows (when using a 9×9 neighborhood) of edge pixels in all corresponding layer template images to obtain all matching results, without traversing all pixels (91 times or 72 times). This can greatly improve the matching speed of non-top-level pyramid images, theoretically increasing the matching speed by more than 8 times.
[0105] During the matching process of a target image's pyramid image from the top layer to the next, a preset matching score Smin is used as the basis for determining whether the matching result at each position can be used as the matching score for the next layer. However, because the pyramid layering operation weakens the image edge strength when transitioning to the next layer, using the same preset matching score for the upper layer's pyramid image will result in a matching failure, resulting in no matching candidate points (i.e., the matching results for the upper layer) available in the lower layer. To address this, one embodiment of the present invention introduces another parameter, SpeedRatio, whose value ranges from 0 to 1. This SpeedRatio parameter can influence the preset matching score for each layer of the pyramid image: a larger value indicates faster matching, while a smaller value indicates slower matching. This allows the preset matching results for non-top pyramid images to be adjusted based on image edge effects and matching results. Specifically, in each non-top pyramid image, when using the matching result of the previous layer to perform a partial match with the template image of the corresponding layer to update the matching result, the preset score for the current layer is calculated using the following formula:
[0106]
[0107] Where Lm is the total number of pyramid image layers contained in the target image, and level is the layer the current pyramid image is located in. Using Slmin as the preset matching score for each pyramid image layer ensures that the preset matching scores for each pyramid image layer are distributed according to the golden section, resulting in better matching performance.
[0108] exist Figure 2 After step S14, the target position of the image to be identified will be matched in the original layer of the target image. However, the current calculated position is pixel-level accuracy. For high-precision matching algorithms, sub-pixel positioning accuracy is often required. At this time, the accuracy needs to be further optimized.
[0109] In one embodiment of the present invention, an operation is provided for achieving sub-pixel positioning accuracy using an improved iterative closest point (ICP) method. First, it is assumed that the gradient direction of each edge pixel on the underlying template image is completely consistent with the edge gradient direction of the corresponding position in the target image, that is, the matching score S calculated by formula (2) is 1, which will be the most complete match. The goal of the improved iterative closest point is to iteratively adjust the position of each pixel so that the gradient direction of each point on the underlying template image becomes increasingly similar to the gradient direction of the corresponding position in the underlying pyramid image of the target image. In this way, after each iteration, the matching score S will also become increasingly close to 1.
[0110] Specifically, assuming that the number of edge pixels in the underlying template image is N, the edge gradient direction homogeneous vector of the underlying template image is Sj(Xj,Yj,1), and at a certain matching position, the edge gradient direction homogeneous vector of the underlying pyramid image of the target image corresponding to Sj is Di(Xi,Yi,1), and the affine transformation matrix of the current matching position is:
[0111]
[0112] Where a1 is the sine value of the rotation of the current matching position, a2 is the cosine value of the rotation of the current matching position, b1 is the translation value of the current matching position in the first direction, and b2 is the translation value of the current matching position in the second direction (for example, the first direction is perpendicular to the second direction). The inference process of the minimum gradient error optimization equation at the current matching position is as follows:
[0113]
[0114]
[0115]
[0116]
[0117]
[0118] In formula (13),
[0119] From Equation (13), it is clear that the x value can be solved using least squares. Since a1 and a2 represent the rotation of the current matching position, and b1 and b2 represent the translation of the current matching position, these four values are used to solve the small rotation angle and small translation of the current position. After multiple iterations, by judging the change in the matching score before and after each iteration, when the change is less than a minimum difference, it can be considered that the iterative solution has obtained the sub-pixel position and convergence has ended. Using this method, iteratively approaching the pixel position to its optimal sub-pixel position is very fast, and convergence can generally be completed within 10 times.
[0120] The embodiment of the present invention further provides a fast template matching device, which can be located in a machine vision system and realize matching positioning. The fast template matching device 9 of this embodiment includes a memory 91 and a processor 92, wherein the memory 91 stores a computer program that can be executed in the processor 92, and when the processor 92 executes the computer program, the following is realized. Figure 2-8 The steps of the fast template matching method described in the embodiment.
[0121] The fast template matching device in this embodiment is similar to the above Figure 2-7 The fast template matching method in the corresponding embodiment belongs to the same concept, and its specific implementation process is detailed in the corresponding method embodiment. The technical features in the method embodiment are applicable to the device embodiment and will not be repeated here.
[0122] One embodiment of the present invention provides a computer-readable storage medium storing computer-executable instructions, wherein the computer-executable instructions are used to enable a computer to execute the fast template matching device described above.
[0123] The computer-readable storage medium in this embodiment is the same as the above Figure 2-7 The fast template matching method in the corresponding embodiment belongs to the same concept, and its specific implementation process is detailed in the corresponding method embodiment. The technical features in the method embodiment are applicable to the device embodiment and will not be repeated here.
[0124] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0125] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0126] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0127] In the embodiments provided in this application, it should be understood that the disclosed fast template matching method and device can be implemented in other ways.
[0128] The present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, it can implement the steps of the above-mentioned various method embodiments. The computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or interface switching device that can carry the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.
[0129] While the embodiments of the present invention have been described in detail above with reference to the accompanying drawings, the present invention is not limited to the embodiments described above. Various modifications may be made within the scope of knowledge possessed by a person skilled in the art without departing from the spirit of the present invention. Furthermore, the embodiments of the present invention and the features thereof may be combined with one another unless there is a conflict.
Claims
1. A fast template matching method, characterized in that: include: Creating a plurality of layers of pyramid images corresponding to the plurality of layers of template images according to the target image, wherein each layer of the template image includes a plurality of edge position points of the image to be recognized and the corresponding gradient direction; Fully matching the top pyramid image with the top template image to obtain a plurality of matching results, each of which includes a position, a rotation angle, and a scaling value; In each non-top pyramid image, using the matching result of the previous layer to perform incomplete matching with the template image of the corresponding layer and updating the matching result; Obtaining the position of the image to be identified in the target image according to the matching result corresponding to the bottom-level pyramid image; In each non-top pyramid image, using the matching result of the previous layer to perform incomplete matching with the template image of the corresponding layer and updating the matching result, including performing the following steps on each matching result of the previous layer: Calculate the position of the current matching result and the gradient direction of each pixel in a preset neighborhood, and normalize the gradient direction of each pixel into a single-byte expression; According to the normalized gradient direction, the SSE2 instruction set is used to perform matching score calculation on the position corresponding to the current matching result and each position in the preset area; When the maximum value of the matching scores of all positions is greater than or equal to the preset score of the current layer, the position corresponding to the maximum value of the matching scores will replace the current matching result; when the maximum value of the matching scores is less than the preset score of the current layer, the current matching result will be deleted.
2. The fast template matching method according to claim 1, characterized in that: The matching score calculation is performed on the position corresponding to the current matching result and each position within the preset range using the SSE2 instruction set according to the normalized gradient direction, including: Adjacent rows are aligned crosswise and the SSE2 instruction set is used to perform matching score calculations on the position corresponding to the current matching result and each row in the preset area.
3. The fast template matching method according to claim 1, characterized in that: The completely matching the top-level pyramid image with the top-level template image includes: Obtaining the gradient magnitudes of all pixels in the top pyramid image; Before any to-be-matched position in the top pyramid image is matched with the top template image, obtaining a total number of pixels that meet a preset requirement within an area covered by the top template image, wherein the preset requirement is that a gradient amplitude is greater than or equal to a preset gradient amplitude; When the total number of pixels meeting the preset requirements within the coverage area of the top template image is greater than a preset number, the to-be-matched position in the top pyramid image is matched with the top template image; otherwise, the to-be-matched position is directly ignored.
4. The fast template matching method according to claim 3, characterized in that: The obtaining of the total number of pixels meeting preset requirements within the coverage area of the top-level template image includes: generating an edge point number integral map according to the gradient amplitude of the pixel points in the top pyramid image, wherein each value in the edge point number integral map represents the total number of pixel points that meet preset requirements in all areas above and to the left of each corresponding pixel point; According to the edge point number integral map, the total number of pixel points that meet preset requirements within the coverage area of the top template image at the position to be matched is generated.
5. The fast template matching method according to claim 4, characterized in that: Generating an edge point number integral map according to the gradient amplitude of the pixel points in the top pyramid image includes: Use the following calculation formula to scan each pixel of the top pyramid image from top to bottom and from left to right and generate each value in the edge point number integral map: Intregral(i,j)=Intregral(i-1,j)+Intregral(i,j-1)-Intregral(i-1,j-1)+(Mi(i,j)>ThresEdgeMin) Where Intregral(i,j) is the value at the i-row and j-column position in the edge point integral map, and Mi(i,j)>ThresEdgeMin indicates whether the edge gradient information of the pixel in the i-row and j-column of the top-level pyramid image meets the preset requirements. If the requirements are met, the value is 1; otherwise, the value is 0.
6. The fast template matching method according to claim 4, characterized in that: Generating the total number of pixels meeting preset requirements within the coverage area of the top template image at the position to be matched according to the edge point number integral map includes: The following calculation formula is used to obtain the total number of pixels that meet the preset requirements within the coverage area of the top template image at the position to be matched: Ni=Intregral(p+r,q+r)-Intregral(pr,q+r)-Intregral(p+r,qr)+Intregral(pr,qr) The position to be matched is located at the pth row and the qth column of the top pyramid image, and the radius of the minimum circumscribed enclosing circle of the edge pixel point set of the top template image is .
7. The fast template matching method according to claim 1, characterized in that: In each non-top pyramid image, using the matching result of the previous layer to perform incomplete matching with the template image of the corresponding layer and updating the matching result includes calculating the preset score of the current layer by the following calculation formula: Wherein, SpeedRatio is a preset speed ratio ranging from 0 to 1; Lm is the total number of layers of the pyramid image contained in the target image; and level is the layer number of the current pyramid image.
8. The fast template matching method according to claim 1, characterized in that: The method comprises: For each matching result in the bottom-level pyramid image, the optimal sub-pixel position of the matching position is iteratively calculated using the following formula: Among them, the edge gradient direction homogeneous vector of the underlying template image is Sj(Xj, Yj, 1), the edge gradient direction homogeneous vector corresponding to Sj at the current matching position is Dj(Xi, Yi, 1), and the affine transformation matrix of the current matching position is a1 is the sine value of the rotation amount of the matching position, a2 is the cosine value of the rotation amount of the matching position, b1 is the translation value of the matching position in the first direction, and b2 is the translation value of the matching position in the second direction.
9. A fast template matching device, comprising a memory and a processor, characterized in that: The memory stores a computer program executable in the processor, and the processor implements the steps of the fast template matching method according to any one of claims 1 to 8 when executing the computer program.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the steps of the fast template matching method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Fast object matching algorithm
CN109801318A
Image processing method and device, electronic apparatus, and storage medium
WO2021027344A1