Method of template matching at image edges

By padding the image edges with zeros and normalizing them using a mask image, the problem of inaccurate template image edge positioning in existing technologies is solved, achieving full-image-level template matching, improving computational efficiency and automation, and making it suitable for various scenarios.

CN115170845BActive Publication Date: 2026-03-17MATRIXTIME ROBOTICS (SHANGHAI) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-14
Publication Date
2026-03-17

AI Technical Summary

Technical Problem

Existing technologies for locating template images at image edges suffer from deviations in correlation or difference calculations due to edge-padding methods, making accurate matching difficult. This is especially problematic when information is scarce, as it prevents effective positioning of the template image.

Method used

By padding zeros around the edges of the modeling image and normalizing it using a mask image, the matching score of the correlation or squared difference is calculated. The normalized denominator value is constructed by introducing an all-one image and the padding mask image, and the normalization process of the matching score is optimized to achieve full-image-level template matching.

Benefits of technology

It achieves fast, convenient, and automated template matching at image edges, has a wide range of applications, high computational efficiency, can provide matching results pixel by pixel, supports both correlation and squared difference methods, and is highly flexible.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115170845B_ABST
    Figure CN115170845B_ABST
Patent Text Reader

Abstract

The application belongs to the technical field of image processing, and particularly relates to a method for template matching on image edges, which comprises the following steps: obtaining a template image with the same pixel size as a modeling image, and normalizing pixel values of the modeling image and the template image; padding the modeling image with 0 to the periphery to obtain a padding modeling image, and creating a padding mask image with the same size as the padding modeling image; obtaining a matching score of the template image and the modeling image by calculating a correlation or square difference correction matrix; introducing a full-1 image with the same size as the template image and the padding mask image to construct a normalized denominator value, normalizing the matching score by using the normalized denominator value, and obtaining a matching score image, i.e., the matching relationship between the template image and the modeling image. The application can quickly realize full-image level template matching by using the mask mode, and the method is convenient to operate, high in automation, and suitable for a wide range of applications and fields.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of image processing technology, and specifically relates to a method for template matching at image edges. Background Technology

[0002] Currently, template matching techniques mainly fall into two categories: one involves extracting features from the main points of the input and template images, comparing and pairing these features, and generating a transformation matrix based on the results to achieve template matching; the other involves comparing the correlation or difference between the input and template images position by position to find the location of the template image within the current image. The former method is suitable for scenarios with complex patterns and no recurring patterns, and its matching typically involves image translation, rotation, stretching, or other transformations. The latter method is suitable for situations where the template image only needs to be translated within the input image to achieve matching, but it cannot effectively handle scenarios where the template image is located at the edge of the input image.

[0003] In industrial inspection scenarios, it's common to need to locate a template image within a modeling image (i.e., the input image) for subsequent inspection. At this point, the template image has already undergone rotation and stretching corrections; only its position within the modeling image needs to be determined. This location can occur not only at the center of the modeling image but also at its edges. A common solution is to pad the edges of the modeling image. There are typically two methods for this: one is to add zero edges, i.e., appending a black border to the modeling image; the other is to use the modeling image repeatedly along its boundaries based on known information. For the former, zero edges can lead to significant deviations in correlation or difference calculations, resulting in positioning failure; for the latter, existing information is unavailable. Therefore, a method is needed to optimize the image for template matching within the edge bounding regions. Summary of the Invention

[0004] The purpose of this invention is to provide a method for template matching at the edge of an image. This invention uses a masking method to quickly achieve full-image-level template matching. Furthermore, this method is convenient to operate, highly automated, and has a wide range of applications and fields.

[0005] To achieve the above objectives, the present invention provides the following technical solution:

[0006] A method for template matching at image edges, characterized in that:

[0007] Obtain a template image with the same pixel size as the modeling image, and normalize the pixel values ​​of the modeling image and the template image;

[0008] The modeling image is padded with zero edges on all four sides to obtain the padded modeling image, and a padded mask image of the same size as the padded modeling image is created.

[0009] By calculating the correction matrix of correlation or squared difference, the matching score between the template image and the modeling image is obtained;

[0010] A normalized denominator value is constructed by introducing an all-1 image and a padded mask image with the same size as the template image. The matching score is normalized by the normalized denominator value to obtain the matching score map, which yields the matching relationship between the template image and the modeling image.

[0011] Furthermore, the zero-padding of the modeled image mainly includes two methods:

[0012] 1. In the "Same" mode, the widths of the zero-padding edges of the modeled image are ((ht-1) / / 2, ht-1-(ht-1) / / 2, (wt-1) / / 2, wt-1-(wt-1) / / 2) respectively.

[0013] Second, in “Full” mode, the widths of the zero-padding edges on the top, bottom, left, and right sides of the modeled image are (ht, ht, wt, wt) respectively.

[0014] Where (wt,ht) represents the width and height of the 2D size of the template image.

[0015] Furthermore, the edge-padding mask image has the same size as the modeling image and is filled with 1s; the edge-padding mode of the edge-padding mask image is the same as the edge-padding mode of the modeling image, and 0s are also used as padding when padding.

[0016] Furthermore, when using correlation for matching, the template image T is used as the kernel to perform correlation calculation on the edge-padding modeling image I to obtain the correlation matching score. The calculation formula is as follows:

[0017]

[0018] Furthermore, when using the squared difference for matching, the template image T is used as the kernel to calculate the squared difference matching score on the edge-padding modeling image I. The calculation formula is as follows:

[0019]

[0020] Furthermore, the squared difference matching score is corrected using the template image T and the edge-padding mask image M, and the correction formula is as follows:

[0021]

[0022] The corrected difference of squares matching score is:

[0023] R sqdiff (x,y)=R sqdiff_raw(x,y)-R sub (x,y).

[0024] Furthermore, an all-1 image M with the same size as the template image is introduced. T Construct the normalized denominator value with the padded mask image M:

[0025]

[0026] in,

[0027]

[0028]

[0029] Furthermore, the normalized relevance matching score is calculated as follows:

[0030]

[0031] Further, calculate the normalized squared difference matching score:

[0032]

[0033] Compared with the prior art, the beneficial effects of the present invention are as follows:

[0034] (1) The present invention uses a mask method to quickly achieve full-image template matching, and this method is convenient to operate, highly automated, and has a wide range of applications and fields.

[0035] (2) Through the above technical solution, this invention achieves template matching at the edges of the modeled image, and the width added to the edges can be changed according to actual needs. In "Same" and "Full" modes, this invention supports full-image matching using either correlation or squared difference methods. During the calculation process, full-image template matching with borders can be achieved without inputting any parameters, making it convenient, flexible, and automated.

[0036] (3) This operation only uses conventional operators to implement the function, which has high overall computational efficiency and can give matching results pixel by pixel. Attached Figure Description

[0037] Figure 1 This is a flowchart of a method for template matching at image edges in an embodiment.

[0038] Figure 2 This is a schematic diagram of the template matching process of the semiconductor template image in the modeling image in the embodiment. Detailed Implementation

[0039] The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to specific examples. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative effort are within the scope of protection of the present invention.

[0040] This solution, based on zero-padding of modeled image edges, designs a method for optimizing template matching in the edge bounding region image (a method for template matching at image edges), such as... Figure 1 As shown, it includes the following steps:

[0041] Step A. Obtain a template image with the same pixel dimensions as the modeling image.

[0042] The template image needs to be scaled proportionally to ensure that the physical dimensions represented by each pixel are consistent with the actual dimensions of each pixel in the modeling image. This is to eliminate stretching differences between the template and modeling images during matching, ensuring that the template image can find its proper position in the modeling image without stretching or rotation.

[0043] Step B. Normalize the pixel values ​​of the modeling image and the template image.

[0044] Pixel value normalization refers to dividing the pixel values ​​of the template image and the modeling image by their maximum possible values. For example, a regular grayscale image or color image needs to be divided by 255 in each channel to ensure that the pixel value is between 0 and 1.

[0045] Step C. Add zero edges to the modeling image to obtain the modeling image with added edges.

[0046] There are two main ways to pad zero edges in image modeling:

[0047] One is the "Same" mode. In the "Same" mode, the widths of the edges that need to be padded on the top, bottom, left, and right sides of the image are ((ht-1) / / 2,ht-1-(ht-1) / / 2,(wt-1) / / 2,wt-1-(wt-1) / / 2).

[0048] Another mode is "Full". In "Full" mode, the top, bottom, left and right sides of the image need to be padded with 0 widths of (ht, ht, wt, wt) respectively.

[0049] All padded areas are filled with 0. To simplify the following explanation, the template image here uses (wt, ht) to represent its 2D width and height. In addition, custom padded strategies are also supported.

[0050] Step D. Create a patch mask image of the same size as the patch modeling image.

[0051] The newly created mask image must have the same dimensions as the modeling image, and should be filled with 1s throughout. Its edge-padding mode should be the same as the modeling image's edge-padding mode, using 0s as fill. See below for further details. Figure 2 .

[0052] Step E. Calculate the correction matrix of correlation or squared difference to obtain the unstandardized matching score; either correlation or squared difference can be used to achieve the matching method.

[0053] The template image T is overlaid on the modeling image I and translated. Then, a target matching the template image T is searched in the modeling image I. The area of ​​the template image T covering the modeling image I is called the subimage. (x,y) are the coordinates of the lower left (or upper left) corner of the subimage on the modeling image I. (x′,y′) are the coordinates of the template image T.

[0054] (1) When using correlation for matching, the normalized template image T is used as the kernel to perform correlation calculation on the modeling image I with zero-padding edges to obtain the correlation matching score. The calculation formula is as follows:

[0055]

[0056] When a modeling image I with zero-padded edges is used, the added zero values ​​will not affect the relevant scores within the effective region during related operations, so it does not need to be corrected.

[0057] (2) When using the squared difference for matching, the normalized template image T is used as the kernel to perform the squared difference calculation on the modeling image I with zero-padding edges to obtain the initial matching score. The calculation formula is as follows:

[0058]

[0059] When calculating the squared difference, the zero-added values ​​in the modeled image I with zero-padded edges will affect the matching score within the effective region, so correction is required.

[0060] The correction value is obtained using the template image T and the edge-padding mask image M through the following formula:

[0061]

[0062] The corrected difference of squares matching score is:

[0063] R sqdiff (x,y)=R sqdiff_raw (x,y)-R sub (x,y)

[0064] At this point, the total matching score obtained near the border has removed the influence of edge padding. The total number of pixels entering the correlation and squared difference calculation is significantly less than that in the non-border area, and its score cannot be directly compared with the score in the non-border area, so normalization processing is required.

[0065] Step F. Normalize the results and obtain the matching score map, thereby obtaining the matching relationship between the template image and the modeling image.

[0066] Without considering edge padding, both of the above score descriptions can be normalized using the following denominator value:

[0067]

[0068] To remove the influence of the padded area, a padded mask image M is introduced, and the normalized denominator value is changed as follows:

[0069]

[0070]

[0071]

[0072] Among them, M T This is an all-1 image with the same size as the template image, used to calculate the sum of squares of pixel values ​​within the effective region.

[0073] Based on this, the normalized relevance matching score or the normalized squared difference matching score can be calculated:

[0074]

[0075]

[0076] Used for final positioning.

[0077] In the normalized correlation matching score map, the region with the highest score represents the best match when the center of the template image is at that location. Similarly, in the normalized squared difference matching score map, the region with the lowest score, i.e., the smallest difference, represents the best match.

[0078] Of all the above calculation formulas, except for R... sqdiff_raw Apart from the formulas, all other formulas can be calculated using simple matrix operations or related methods. Therefore, the matching time is not significantly increased compared to matching algorithms that do not consider borders.

[0079] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.

Claims

1. A method of template matching at image edges, characterized in that: a template image consistent with the pixel size of a modeling image is obtained, and the pixel values of the modeling image and the template image are normalized; a 0-padded modeling image is obtained by padding the modeling image with 0 around it, and a 0-padded mask image of the same size as the 0-padded modeling image is created; a matching score of the template image and the modeling image is obtained by calculating a correlation or a square difference correction matrix; a full-1 image consistent with the size of the template image and the 0-padded mask image are introduced to construct a normalized denominator value, and the matching score is normalized by the normalized denominator value to obtain a matching score image, i.e., the matching relationship between the template image and the modeling image is obtained; the 0-padded mask image has the same size as the modeling image and is filled with 1; the padding mode of the 0-padded mask image is consistent with that of the modeling image, and 0 is used for padding when padding; when correlation is used for matching, the correlation matching score is obtained by using the template image T as the kernel to calculate the correlation on the 0-padded modeling image I, and the calculation formula is as follows: ; (x, y) is the coordinate of the lower left corner or the upper left corner of the sub-image on the modeling image I, and (x', y') is the coordinate on the template image T; Introducing an all-ones image consistent with the template image size and the padded mask image M to construct the normalization denominator value: , wherein, ; the normalized correlation matching score is calculated as: ; where (x,y) are the coordinates of the lower left or upper left corner of the sub-image on the modeled image I and (x',y') are the coordinates on the template image T.

2. The method of claim 1, wherein: the modeling image padding with 0 mainly includes two modes: one is "Same" mode, in which the width of the 0-padded modeling image needs to be padded on the top, bottom, left and right of the modeling image is ((ht-1) / / 2, ht-1-(ht-1) / / 2, (wt-1) / / 2, wt-1-(wt-1) / / 2); two is "Full" mode, in which the width of the 0-padded modeling image needs to be padded on the top, bottom, left and right of the modeling image is (ht, ht, wt, wt); wherein, (wt, ht) represents the width and height of the 2D size of the template image.

3. The method of template matching at image edges according to claim 1, wherein: when the square difference is used for matching, the square difference matching score is obtained by using the template image T as the kernel to calculate the square difference on the 0-padded modeling image I, and the calculation formula is as follows: 。 4. The method of template matching at image edges of claim 3, wherein: the square difference matching score is corrected using the template image T and the 0-padded mask image M, and the correction formula is: , the corrected square difference matching score is: 。 5. The method of template matching at image edges of claim 4, wherein: Introducing an all-ones image consistent with the template image size and the padded mask image M to construct the normalization denominator value: , wherein, 。 6. The method of template matching at image edges of claim 5, wherein: the normalized square difference matching score is calculated as: 。

Citation Information

Patent Citations

  • Paper packaging box inside and outside detection method based on nine camera models

    CN105321185A

  • Quantitative stokehole PCB patch defect online detection method and system

    CN111899241A