A template matching method, device, apparatus, and storage medium
By calculating the central moments and Hu moments of the feature point columns of the template and the image to be matched, the scaling factor is determined and the feature point columns are scaled, which solves the problem of low efficiency of the template matching method when the target is enlarged or reduced, and realizes efficient template matching.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BYD CO LTD
- Filing Date
- 2024-01-15
- Publication Date
- 2026-08-04
AI Technical Summary
Existing template matching methods are computationally inefficient when the target to be matched is enlarged or reduced, making them unsuitable for practical application. Furthermore, the Gaussian pyramid method loses image details and has low matching accuracy when the number of layers is increased.
By acquiring the feature point columns of the template image and the image to be matched, calculating the central moment and Hu moment, determining the scaling factor and scaling, and finally judging the matching status between the template and the image to be matched based on the matching degree.
It improves the computational efficiency and applicability of template matching, can quickly adapt to the magnification or reduction of the target to be matched, simplifies the calculation process, and preserves image details.
Smart Images

Figure CN118279615B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of image processing, and more particularly to a template matching method, apparatus, device, and storage medium. Background Technology
[0002] Template matching refers to the process of matching a template image with a target image based on certain features of the image. Currently, most template matching methods suffer from low computational efficiency and are unsuitable for situations where the target image is magnified or reduced. Since the workpiece in the image to be matched may be magnified or reduced to varying degrees, template matching presents significant challenges. Therefore, there is an urgent need in this field for a method that performs template matching specifically for workpieces. Summary of the Invention
[0003] This application provides a template matching method, apparatus, device, and storage medium, which can improve the efficiency of template matching.
[0004] In a first aspect, this application provides a template matching method, the method comprising: acquiring a first feature point column corresponding to a first workpiece in a first image and a second feature point column corresponding to a second workpiece in a second image; determining the central moments corresponding to the first feature point column and the second feature point column; determining a scaling factor of the first feature point column based on the central moments corresponding to the first feature point column and the second feature point column; scaling the first feature point column according to the scaling factor to obtain a third feature point column; determining the matching degree between the second feature point column and the third feature point column, and determining that the first workpiece and the second workpiece match if the matching degree is less than or equal to a preset threshold.
[0005] In one possible implementation of the first aspect, determining the matching degree between the second feature point column and the third feature point column includes: determining the Hu moment corresponding to the second feature point column based on the central moment of the second feature point column; determining the central moment of the third feature point column; determining the Hu moment corresponding to the third feature point column based on the central moment of the third feature point column; and determining the matching degree based on the Hu moments corresponding to the second feature point column and the Hu moments corresponding to the third feature point column.
[0006] In one possible implementation of the first aspect, determining the central moments corresponding to the first feature point column and the second feature point column includes: determining the centroid coordinates corresponding to the first feature point column; determining the central moments corresponding to the first feature point column based on the centroid coordinates corresponding to the first feature point column; determining the centroid coordinates corresponding to the second feature point column; and determining the central moments corresponding to the second feature point column based on the centroid coordinates corresponding to the second feature point column.
[0007] In one possible implementation of the first aspect, the method includes: determining the first moment and the zero moment corresponding to the first feature point column; determining the centroid coordinates corresponding to the first feature point column based on the first moment and the zero moment corresponding to the first feature point column; determining the first moment and the zero moment corresponding to the second feature point column; and determining the centroid coordinates corresponding to the second feature point column based on the first moment and the zero moment corresponding to the second feature point column.
[0008] In one possible implementation of the first aspect, obtaining the first feature point sequence corresponding to the first workpiece in the first image and the second feature point sequence corresponding to the second workpiece in the second image includes: performing image preprocessing on the first image, performing edge detection on the preprocessed first image to obtain the first feature point sequence; performing image preprocessing on the second image, performing edge detection on the preprocessed second image to obtain the second feature point sequence; wherein, the image preprocessing includes any one or more of grayscale conversion, grayscale stretching, image binarization, or noise reduction processing.
[0009] By preprocessing an image, points with significant brightness changes can be identified. These points often reflect important events and changes in the image. Furthermore, image preprocessing can significantly reduce the amount of data, remove irrelevant information, and retain important structural attributes of the image.
[0010] Secondly, this application provides a template matching device, the device comprising:
[0011] The acquisition unit is used to acquire the first feature point column corresponding to the first workpiece in the first image and the second feature point column corresponding to the second workpiece in the second image;
[0012] The processing unit is configured to determine the central moments corresponding to the first feature point column and the second feature point column; determine the scaling factor of the first feature point column based on the central moments corresponding to the first feature point column and the second feature point column; and scale the first feature point column according to the scaling factor to obtain a third feature point column.
[0013] The matching degree between the second feature point column and the third feature point column is determined. If the matching degree is less than or equal to a preset threshold, the first workpiece and the second workpiece are determined to be matched.
[0014] In one possible implementation of the second aspect, the processing unit is specifically configured to: determine the Hu moment corresponding to the second feature point column based on the central moment of the second feature point column; determine the central moment of the third feature point column; determine the Hu moment corresponding to the third feature point column based on the central moment of the third feature point column; and determine the matching degree based on the Hu moment corresponding to the second feature point column and the Hu moment corresponding to the third feature point column.
[0015] In one possible implementation of the second aspect, the processing unit is further specifically configured to: determine the centroid coordinates corresponding to the first feature point column; determine the central moment corresponding to the first feature point column based on the centroid coordinates corresponding to the first feature point column; determine the centroid coordinates corresponding to the second feature point column; and determine the central moment corresponding to the second feature point column based on the centroid coordinates corresponding to the second feature point column.
[0016] In one possible implementation of the second aspect, the processing unit is further specifically configured to: determine the first moment and the zero moment corresponding to the first feature point column; determine the centroid coordinates corresponding to the first feature point column based on the first moment and the zero moment corresponding to the first feature point column; determine the first moment and the zero moment corresponding to the second feature point column; and determine the centroid coordinates corresponding to the second feature point column based on the first moment and the zero moment corresponding to the second feature point column.
[0017] In one possible implementation of the second aspect, the processing unit is further configured to perform image preprocessing on the first image, perform edge detection on the preprocessed first image to obtain the first feature point sequence; perform image preprocessing on the second image, perform edge detection on the preprocessed second image to obtain the second feature point sequence; wherein the image preprocessing includes any one or more of grayscale conversion, grayscale stretching, image binarization, or noise reduction processing.
[0018] Thirdly, this application provides a computing device, which includes a processor and a memory, wherein the memory stores instructions that, when executed by the processor, implement the method described in the first aspect and any possible implementation thereof.
[0019] Fourthly, this application provides a computer-readable storage medium including computer instructions that, when executed by a processor, implement the method described in the first aspect and any possible implementation thereof.
[0020] In the aforementioned template matching method, the feature point columns of both the template image and the image to be matched are first calculated, and their central moments are then calculated. The scaling factor between the template image and the image to be matched is determined using these central moments. The feature point columns of either the template image or the image to be matched are then scaled using this scaling factor to ensure they are the same size. The matching degree of the feature point columns of the template image and the image to be matched under this size condition is then obtained, and the degree of matching is used to determine whether the template image and the image to be matched are a match. This template matching method is applicable when the target to be matched is enlarged or reduced. Furthermore, because the method is computationally simple, requiring only the calculation of the central moments and matching degree of the feature point columns to achieve template matching, it also possesses high efficiency and broad applicability. Attached Figure Description
[0021] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 This is a schematic diagram of a template matching process provided in this application;
[0023] Figure 2 This is a schematic diagram of a template matching method provided in this application;
[0024] Figure 3 This is a schematic diagram of an image preprocessing method provided in this application;
[0025] Figure 4 This is a schematic diagram of a template matching device provided in this application;
[0026] Figure 5 This is a schematic diagram of the structure of a computing device provided in this application. Detailed Implementation
[0027] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0028] The terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish different objects, not to describe a specific order. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to these processes, methods, products, or apparatuses.
[0029] It should be noted that the terminology used in the embodiments of this application is for the purpose of describing particular embodiments only and is not intended to limit the application. The singular forms “a,” “the,” and “the” used in the embodiments of this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.
[0030] For ease of understanding, the relevant terms and nouns that may be involved in the embodiments of this application will be introduced below, including (1) to (2).
[0031] (1) Image matching is the process of matching a template image and a target image based on some features or combinations of features of the image. Image matching mainly includes template matching, contour matching and feature point matching. Template matching compares the grayscale of the template and the target image to find the position of the template in the target image.
[0032] (2) Feature point series refers to a set of key points in an image used to identify and recognize image features. These points play an important role in image processing (including image rotation and scaling). Feature point series can identify and describe key information in an image, including image edges, textures, etc. Feature points are not easily confused in different images and have a certain degree of invariance after the image undergoes changes such as rotation and scaling.
[0033] Current template matching methods have the following problems: First, for template matching that has been scaled up or down, most methods use each sub-image in the template image set to match the image to be matched. This results in a very cumbersome process that consumes a lot of time, and also greatly limits the widespread application of template matching technology. Second, most current matching acceleration methods use Gaussian pyramids. The Gaussian pyramid method reduces the number of pixels in the image to reduce the computational load and thus accelerate the matching process. However, this method also has obvious problems. The number of layers in the Gaussian pyramid cannot be too high, and it is generally limited to four layers. If the number of layers in the Gaussian pyramid is increased further, a lot of image details will be lost, ultimately leading to low matching accuracy.
[0034] To address the problems and shortcomings of the template matching methods mentioned above, this application provides a template matching method. Please refer to [link to relevant documentation]. Figure 1 , Figure 1 This is a flowchart illustrating a template matching process provided in this application, such as... Figure 1 As shown, the computing device acquires a first feature point column corresponding to a first workpiece in a first image and a second feature point column corresponding to a second workpiece in a second image; determines a first feature value corresponding to the first feature point column and a second feature value corresponding to the second feature point column; determines a scaling factor for the first feature point column based on the first and second feature values; scales the first feature point column using the scaling factor to obtain a third feature point column; determines the matching degree between the third and second feature point columns; if the matching degree is less than or equal to a preset threshold, the first workpiece and the second workpiece are determined to be successfully matched; if the matching degree is greater than the preset threshold, the first workpiece and the second workpiece are determined to be unmatched. This method solves the problems of current template matching methods losing details and having complex matching processes when using matching acceleration methods. Furthermore, by using the method provided in this application embodiment, the image corresponding to the template workpiece or the workpiece to be matched can be quickly scaled, thereby quickly performing template matching and optimizing the template matching calculation process. Matching can be completed simply by judging the matching degree between the template workpiece and the feature point column corresponding to the image of the workpiece to be matched, thus improving the computational efficiency of template matching.
[0035] The template matching method provided in the embodiments of this application is described in detail below with reference to the accompanying drawings.
[0036] Please see Figure 2 , Figure 2 This is a schematic diagram of a template matching method provided in this application. For example... Figure 2 As shown, the template matching method includes the following steps:
[0037] S201: The computing device acquires the first feature point column corresponding to the first workpiece in the first image and the second feature point column corresponding to the second workpiece in the second image.
[0038] The first image mentioned above is the image of the template workpiece, and the second image is the image of the workpiece to be matched. In the embodiments of this application, the template workpiece is referred to as the first workpiece, and the workpiece to be matched is referred to as the second workpiece.
[0039] After acquiring the first image and the second image, the computing device performs edge detection on the first image and the second image to obtain the first feature point column corresponding to the first workpiece in the first image and the second feature point column corresponding to the second workpiece in the second image.
[0040] In one possible implementation, before performing edge detection on the first and second images, the computing device performs image preprocessing on both images. This image preprocessing includes any one or more of grayscale image processing, grayscale stretching, image binarization, or noise reduction. It should be understood that the image preprocessing performed on the first and second images by the computing device is consistent.
[0041] In the embodiments of this application, the first workpiece and the second workpiece are objects that are not easily deformed, such as machine parts, tools, etc.
[0042] S202: The computing device determines the central moments corresponding to the first feature point column and the central moments corresponding to the second feature point column.
[0043] By analyzing the feature point sequence, the centroid coordinates corresponding to the feature point sequence can be obtained, and then the central moment of the feature point sequence can be determined based on the centroid coordinates.
[0044] The analysis of the feature point sequence includes constructing the XY coordinate system in which the feature point sequence is located. In one possible embodiment, the XY coordinate system is established with the lower left corner of the image in which the feature point sequence is located as the origin, one side of the image as the X-axis, and the other side of the image perpendicular to the X-axis as the Y-axis. The feature point sequence is a set of multiple discrete points distributed in the XY coordinate system, and each discrete point can be represented by the horizontal coordinate x and the vertical coordinate y.
[0045] The p+q moments of the feature point sequence in the XY coordinate system can be expressed by Equation 1:
[0046]
[0047] In Formula 1 above, M pq * Let p+q be the moment corresponding to a certain feature point sequence, x and y be the x-coordinate and y-coordinate of each feature point in the XY coordinate system, and d(x, y) be the gray value of the image at coordinates (x, y) in the XY coordinate system.
[0048] After obtaining the p+q moments corresponding to the feature point sequence using Formula 1, the computing device determines the centroid coordinates (x0, y0) of the feature point sequence based on the p+q moments. The relationship between the centroid coordinates (x0, y0) and the p+q moments of the feature point sequence satisfies the following Formula 2:
[0049]
[0050] In Formula 2 above, M 10 * M 00 * M01 * M in Formula 1 pq * This is obtained by assigning corresponding values to p and q, for example, M. 00 * It is for M pq * The result after setting p to 0 and q to 0.
[0051] Substituting (p=1, q=0), (p=0, q=0), and (p=0, q=1) into Formula 1, we get:
[0052]
[0053] By M 10 * With M 00 * Dividing by the coordinates yields the x-coordinate x0 of the centroid coordinates corresponding to the feature point column. This can be achieved by dividing by M. 01 * With M 00 * Dividing the two yields the ordinate y0 of the centroid coordinates corresponding to the feature point column.
[0054] After obtaining the centroid coordinates (x0, y0) corresponding to the feature point column, the computing device can obtain the central moment corresponding to the feature point column based on the centroid coordinates (x0, y0) and Formula 1. The central moment corresponding to the feature point column and the centroid coordinates (x0, y0) satisfy the following Formula 3:
[0055]
[0056] Here, u pq It is the central moment corresponding to the feature point sequence.
[0057] The computing device obtains the central moments corresponding to the first feature point column and the second feature point column through the above steps.
[0058] S203: The computing device determines the scaling factor of the first feature point column based on the central moments corresponding to the first feature point column and the central moments corresponding to the second feature point column.
[0059] After the computing device obtains the central moments corresponding to the first feature point column and the second feature point column through S202, it determines the scaling factor of the first feature point column based on the central moments of the first and second feature point columns. The relationship between the scaling factor of the first feature point column and the central moments of the first and second feature point columns satisfies the following formula 4:
[0060] K = u pq# / u pq * Formula 4
[0061] In Formula 4, K is the scaling factor of the first feature point column, u pq # u is the central moment corresponding to the second feature point column. pq * The central moment corresponding to the first feature point column.
[0062] S204: The computing device scales the first feature point column according to the scaling factor to obtain the third feature point column.
[0063] S205: The computing device determines the matching degree between the second feature point column and the third feature point column.
[0064] In one possible implementation, the computing device determines the matching degree between the second and third feature point sequences based on the Hu moments of the second and third feature point sequences.
[0065] The computing device determines the Hu moment corresponding to the feature point sequence based on the central moment of the feature point sequence.
[0066] After obtaining the central moments of a certain feature point sequence, the computing device normalizes the central moments using the zeroth-order central moment to obtain the normalized central moments. The relationship between the normalized central moments, the central moments, and the zeroth-order central moments satisfies the following formula:
[0067] Formula 5:
[0068]
[0069] In the above formula 5, n pq U represents the normalized p+q order central moments. pq Denotes the central moment of order p+q. It represents the r-th power of the zeroth central moment.
[0070] The computing device obtains the second-order normalized central moments and the third-order normalized central moments corresponding to the feature point sequence through formula 5, and determines the Hu moment corresponding to the feature point sequence through the second-order normalized central moments and the third-order normalized central moments. The relationship between the Hu moment and the second-order normalized central moments and the third-order normalized central moments satisfies the following formula 6:
[0071]
[0072] In the above formula 6, n 20 n 02 and n 11 Let n be the second-order normalized central moment. 30 n 03n 12 and n 21 Let φ1, φ2, φ3, φ4, φ5, φ6, and φ7 be third-order normalized central moments, and let φ7 be Hu moments, which will be discussed later. i Represents the i-th Hu moment corresponding to the feature point sequence, where i is a positive integer less than or equal to 7.
[0073] After acquiring the third feature point sequence, the computing device obtains the central moment corresponding to the third feature point sequence according to the method described in S202. Then, according to the method for calculating Hu moments described above, it obtains the Hu moments of the third feature point sequence and the second feature point sequence. Finally, it determines the matching degree between the third feature point sequence and the second feature point sequence based on the Hu moments of the third feature point sequence and the second feature point sequence. The relationship between the matching degree between the third feature point sequence and the second feature point sequence and the Hu moments of the third feature point sequence and the second feature point sequence satisfies the following formula 7:
[0074]
[0075] In Formula 7 above, L represents the matching degree. This represents the i-th Hu moment of the second feature point sequence. Let represent the i-th Hu moment of the third feature point sequence.
[0076] S206: The computing device determines whether the matching degree is less than or equal to a preset threshold. If the matching degree is less than or equal to the preset threshold, it determines that the first workpiece and the second workpiece are successfully matched.
[0077] After the computing device determines the matching degree of the third feature point column and the second feature point column using Formula 7, it determines whether the matching degree is less than or equal to a preset threshold. If the matching degree is less than or equal to the preset threshold, it determines that the first workpiece and the second workpiece match. If the matching degree is greater than the preset threshold, it determines that the first workpiece and the second workpiece do not match.
[0078] The aforementioned preset threshold can be manually specified or automatically determined by an algorithm. The size of the preset threshold can be adjusted according to actual needs. This application embodiment does not impose specific limitations. For example, the preset threshold is generally set to 0.5. When the matching degree is less than or equal to 0.5, it means that the first workpiece and the second workpiece are highly similar, and the matching is determined to be successful. When the matching degree is greater than 0.5, it means that the first workpiece and the second workpiece are less similar, and the matching is determined to be unsuccessful.
[0079] In another possible embodiment, the matching degree in S205 above can also be calculated as follows: after scaling the first feature point column in S204 to obtain the third feature point column, the computing device will also align the third feature point column with the second feature point column. After the alignment is completed, the matching degree between the third feature point column and the second feature point column will be calculated using Formula 8.
[0080] L = S / (S + S) * ) Formula 8
[0081] In Formula 8 above, L represents the matching degree between the third feature point column and the second feature point column, and S represents the area of the overlapping portion of the first workpiece and the second workpiece after the third feature point column and the second feature point column are aligned. * This indicates the area of the non-overlapping portion of the first and second workpieces after the third feature point column is aligned with the second feature point column.
[0082] In this embodiment, the computing device can also use Formula 9 to calculate the matching degree between the third feature point column and the second feature point column.
[0083] L=M / (M+M * ) Formula 9
[0084] In Formula 9 above, L represents the matching degree between the third feature point column and the second feature point column, and M represents the number of overlapping feature points between the third feature point column and the second feature point column after alignment. * This indicates the number of non-overlapping feature points between the third and second feature point columns after alignment.
[0085] The specific process of aligning the third feature point sequence and the second feature point sequence using the aforementioned computing device includes: the computing device analyzes the third feature point sequence to obtain its centroid and circumcenter, and analyzes the second feature point sequence to obtain its centroid and circumcenter; the direction from the centroid of the third feature point sequence to the circumcenter of the second feature point sequence is taken as the direction vector of the third feature point sequence, and the direction from the centroid of the second feature point sequence to the circumcenter of the second feature point sequence is taken as the direction vector of the second feature point sequence; the centroid of the third feature point sequence is made to coincide with the centroid of the second feature point sequence, and then the third feature point sequence is adjusted so that the direction of the direction vector of the third feature point sequence is consistent with the direction of the direction vector of the second feature point sequence. The above steps for aligning the third feature point sequence and the second feature point sequence are merely examples provided in this application. In practical applications, other methods can also be used, such as the computing device obtaining the circumcenters and centroids of the third and second feature point sequences, and then aligning the third and second feature point sequences using these circumcenters and centroids.
[0086] After the computing device determines the matching degree of the third feature point column and the second feature point column using formula 8 or formula 9, it determines whether the matching degree is greater than or equal to a preset threshold. If the matching degree is greater than or equal to the preset threshold, it determines that the first workpiece and the second workpiece match. If the matching degree is less than the preset threshold, it determines that the first workpiece and the second workpiece do not match.
[0087] It should be noted that the above method steps are merely examples listed in this application. In practical scenarios, other similar steps can also be used to implement the template matching method provided in this application. For example, in step S203 above, the computing device may determine the scaling factor of the second feature point column based on the first feature value and the second feature value. In this case, formula 10 is needed to calculate the scaling factor of the second feature point column.
[0088] K * =u pq * / u pq # Formula 10
[0089] In Formula 10, K * u is the scaling factor for the second feature point column. pq # u is the central moment corresponding to the second feature point column. pq * The central moment corresponding to the first feature point column.
[0090] Next, S204 is adjusted so that the computing device scales the second feature point column according to the scaling factor to obtain the third feature point column. Subsequent steps are similar and will not be described in detail here.
[0091] Through the Figure 2 Analysis of each step reveals that when template matching is required, the computing device first calculates the feature point sequences of the template workpiece and the workpiece to be matched in the template workpiece image and the workpiece image to be matched, respectively. Then, it obtains the corresponding feature values based on their respective feature point sequences. Based on the feature values of the template workpiece and the workpiece to be matched, it determines the scaling factor corresponding to the feature point sequence of the template workpiece. This scaling factor is then used to scale the feature point sequence of the template workpiece, and the feature values of the scaled feature point sequence of the template workpiece are obtained. Finally, the matching degree is calculated based on the feature values of the scaled template workpiece and the workpiece to be matched. The magnitude of the matching degree determines whether the template workpiece and the workpiece to be matched are compatible. This template matching method is applicable when the target to be matched is enlarged or reduced. Furthermore, due to its simple calculation, it can improve the transportation efficiency of template matching.
[0092] The image preprocessing process before edge detection in the first and second images can be found in [reference needed]. Figure 3 , Figure 3 This is a schematic diagram of an image preprocessing method provided in this application. The specific steps are as follows:
[0093] S301: The computing device converts a color image into a grayscale image.
[0094] In one possible embodiment, since the camera used to photograph the first workpiece and the second workpiece is a color camera, both the first image and the second image are color images. In order to facilitate subsequent image preprocessing and to enhance edge pixels and suppress non-edge pixels, it is necessary to convert the color image into a grayscale image.
[0095] In another possible scenario, the camera used to photograph the first and second workpieces is a monochrome camera. In this case, the first and second images obtained are grayscale images, and the process proceeds directly to the next step.
[0096] S302: The computing device performs grayscale stretching processing on the grayscale image.
[0097] After obtaining the grayscale images of the first and second images, the computing device needs to perform grayscale stretching on the two images to eliminate the effects of changes in image illumination intensity.
[0098] S303: The computing device selects an appropriate threshold to convert the grayscale image into a binary image.
[0099] After the computing device performs grayscale stretching on the first and second grayscale images, it needs to select an appropriate threshold to convert the grayscale images into binary images. The main purpose is to separate the workpiece from the background in which the workpiece is located. The threshold can be determined manually or automatically using a binarization algorithm.
[0100] S304: The computing device performs noise reduction processing on the binary image and converts it into a noise-reduced image.
[0101] After obtaining the binary image of the image, the computing device will perform noise reduction processing on the binary image to eliminate noise interference and facilitate the implementation of subsequent steps.
[0102] S305: The computing device performs edge detection on the denoised image to obtain a list of feature points of the edge.
[0103] The computing device uses an edge detection operator to perform edge detection on the noise-removed image and obtain the feature point sequence of the image edge.
[0104] The purpose of edge detection is to identify points in an image where brightness changes significantly. These points usually reflect important events and changes in the image. Edge detection can significantly reduce the amount of data, remove irrelevant information, and preserve the important structural properties of the image.
[0105] Please see Figure 4 , Figure 4 This is a schematic diagram of a template matching device provided in an embodiment of this application. Figure 4 As shown, the template matching device 400 includes an acquisition unit 401 and a processing unit 402. This template matching device 400 can be implemented using hardware, software, or a combination of both.
[0106] The acquisition unit 401 is used to acquire the first feature point column corresponding to the first workpiece in the first image and the second feature point column corresponding to the second workpiece in the second image.
[0107] The processing unit 402 is used to determine the central moments corresponding to the first feature point column and the second feature point column; determine the scaling factor of the first feature point column based on the central moments corresponding to the first feature point column and the second feature point column; scale the first feature point column according to the scaling factor to obtain the third feature point column; determine the matching degree between the second feature point column and the third feature point column; and determine that the first workpiece and the second workpiece are matched if the matching degree is less than or equal to a preset threshold.
[0108] In one possible implementation, the processing unit 402 is specifically used to: determine the Hu moment corresponding to the second feature point column based on the central moment of the second feature point column; determine the central moment of the third feature point column; determine the Hu moment corresponding to the third feature point column based on the central moment of the third feature point column; and determine the matching degree based on the Hu moments corresponding to the second feature point column and the Hu moments corresponding to the third feature point column.
[0109] In one possible implementation, the processing unit 402 is further specifically used to: determine the centroid coordinates corresponding to the first feature point column; determine the central moment corresponding to the first feature point column based on the centroid coordinates corresponding to the first feature point column; determine the centroid coordinates corresponding to the second feature point column; and determine the central moment corresponding to the second feature point column based on the centroid coordinates corresponding to the second feature point column.
[0110] In one possible implementation, the processing unit 402 is further specifically used to: determine the first moment and the zero moment corresponding to the first feature point column; determine the centroid coordinates corresponding to the first feature point column based on the first moment and the zero moment corresponding to the first feature point column; determine the first moment and the zero moment corresponding to the second feature point column; and determine the centroid coordinates corresponding to the second feature point column based on the first moment and the zero moment corresponding to the second feature point column.
[0111] In one possible implementation, the processing unit 402 is further specifically used to: perform image preprocessing on the first image, perform edge detection on the preprocessed first image to obtain a first feature point column; perform image preprocessing on the second image, perform edge detection on the preprocessed second image to obtain a second feature point column; wherein, the image preprocessing includes any one or more of grayscale conversion, grayscale stretching, image binarization or noise reduction processing.
[0112] Through the Figure 4 Analysis of the functions of each unit reveals that when template matching is required, the template matching device 400 first acquires the image to be matched and the template image through the acquisition unit 401; then, the processing unit 402 performs image preprocessing on the image to be matched and the template image to acquire the first feature point column of the template image and the second feature point column of the image to be matched; the processing unit 402 calculates the central moment corresponding to the first feature point column, and calculates the central moment and Hu moment corresponding to the second feature point column; the processing unit 402 then determines the scaling factor of the first feature point column based on the central moments corresponding to the first and second feature point columns; the processing unit 402 scales the first feature point column according to the scaling factor, and the scaled first feature point column is recorded as the third feature point column; the processing unit 402 calculates the Hu moment of the third feature point column; the processing unit 402 calculates the matching degree between the third feature point column and the second feature point column based on the Hu moments of the third and second feature point columns; the processing unit 402 determines whether the image to be matched matches the template image by judging whether the matching degree is less than or equal to a preset threshold. The template matching device can be used when the target to be matched is enlarged or reduced. At the same time, since the calculations involved in the device are relatively simple, it can also improve the efficiency and applicability of template matching.
[0113] The functional units of the template matching device 400 can be used to implement Figure 1 , Figure 2 and Figure 3 The method shown. Figure 2 , Figure 3 For example, the acquisition unit 401 can be used to execute Figure 3 Image acquisition before image preprocessing, processing unit 402 can be used to execute steps S201, S202, S203, S204, S205, S206 and steps S301, S302, S303, S304, S305.
[0114] like Figure 5 As shown, Figure 5This is a schematic diagram of the structure of a computing device provided in this application. The computing device 500 includes a processor 501, a memory 502, a communication interface 503, and a bus 504. The processor 501, memory 502, and communication interface 503 can be interconnected through the internal bus 504 or can communicate through wireless transmission or other means.
[0115] Processor 501 includes at least one general-purpose processor, such as a central processing unit (CPU), or a combination of a CPU and a hardware chip. The hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof. Processor 501 is used to execute various types of digital storage instructions. The processor can implement [specific functions / operations] by executing the corresponding instructions. Figure 2 , Figure 3 Any step corresponding to the computing device in the method.
[0116] Memory 502 can be volatile memory, such as random access memory (RAM), dynamic random access memory (DRAM), static random access memory (SRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR), cache, etc., and the memory can also include combinations of the above types. Memory 502 can include programs and data, and processor 501 can execute programs by executing program code. Figure 1 The steps are shown.
[0117] The communication interface 503 can be used to realize data interaction between the computing device 500 and the camera. For example, after the camera captures an image containing the second workpiece, it sends the image data to the computing device 500. The computing device 500 can receive the image through the communication interface 503. This application does not specifically limit the function of the communication interface.
[0118] This application also provides a computer storage medium that stores a computer program for electronic data interchange, which causes a microcontroller to perform some or all of the steps of any of the methods described in the above method embodiments.
[0119] This application also provides a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program operable to cause a computer to perform some or all of the steps of any of the methods described in the above method embodiments. The computer program product may be a software installation package, and the computer may include an electronic device.
[0120] In the above embodiments, the descriptions of each embodiment have different focuses. For parts that are not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0121] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection between apparatuses or units through some interfaces, and may be electrical, mechanical, or other forms.
[0122] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0123] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0124] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a device (which may be a personal computer, server, network device, robot, microcontroller, chip, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory, random access memory, magnetic disks, or optical disks.
[0125] The embodiments of this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only for the purpose of helping to understand the methods and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A template matching method, characterized in that, The method includes: Obtain the first feature point column corresponding to the first workpiece in the first image and the second feature point column corresponding to the second workpiece in the second image; Determine the central moments corresponding to the first feature point column and the second feature point column; The scaling factor of the first feature point column is determined based on the central moments corresponding to the first feature point column and the central moments corresponding to the second feature point column. The first feature point column is scaled according to the scaling factor to obtain the third feature point column; The matching degree between the second feature point column and the third feature point column is determined. If the matching degree is less than or equal to a preset threshold, the first workpiece and the second workpiece are determined to be matched.
2. The method according to claim 1, characterized in that, Determining the matching degree between the second feature point column and the third feature point column includes: Determine the Hu moment corresponding to the second feature point column based on the central moment of the second feature point column; Determine the central moments of the third feature point column; The Hu moment corresponding to the third feature point column is determined based on the central moment of the third feature point column; The matching degree is determined based on the Hu moments corresponding to the second feature point column and the Hu moments corresponding to the third feature point column.
3. The method according to claim 1 or 2, characterized in that, Determining the central moments corresponding to the first feature point column and the second feature point column includes: Determine the centroid coordinates corresponding to the first feature point column; Determine the central moment corresponding to the first feature point column based on the centroid coordinates of the first feature point column; Determine the centroid coordinates corresponding to the second feature point column; The central moment corresponding to the second feature point column is determined based on the centroid coordinates of the second feature point column.
4. The method according to claim 3, characterized in that, The method includes: Determine the first-order moment and the zero-order moment corresponding to the first feature point column; Determine the centroid coordinates of the first feature point column based on the first moment and the zero moment corresponding to the first feature point column; Determine the first and zeroth moments corresponding to the second feature point column; The centroid coordinates of the second feature point sequence are determined based on the first and zero moments corresponding to the second feature point sequence.
5. A template matching device, characterized in that, The device includes: The acquisition unit is used to acquire the first feature point column corresponding to the first workpiece in the first image and the second feature point column corresponding to the second workpiece in the second image; The processing unit is configured to determine the central moments corresponding to the first feature point column and the second feature point column; determine the scaling factor of the first feature point column based on the central moments corresponding to the first feature point column and the second feature point column; and scale the first feature point column according to the scaling factor to obtain a third feature point column. The matching degree between the second feature point column and the third feature point column is determined. If the matching degree is less than or equal to a preset threshold, the first workpiece and the second workpiece are determined to be matched.
6. The apparatus according to claim 5, characterized in that, The processing unit is specifically used for: Determine the Hu moment corresponding to the second feature point column based on the central moment of the second feature point column; Determine the central moments of the third feature point column; The Hu moment corresponding to the third feature point column is determined based on the central moment of the third feature point column; The matching degree is determined based on the Hu moments corresponding to the second feature point column and the Hu moments corresponding to the third feature point column.
7. The apparatus according to claim 5 or 6, characterized in that, The processing unit is specifically used for: Determine the centroid coordinates corresponding to the first feature point column; Determine the central moment corresponding to the first feature point column based on the centroid coordinates of the first feature point column; Determine the centroid coordinates corresponding to the second feature point column; The central moment corresponding to the second feature point column is determined based on the centroid coordinates of the second feature point column.
8. The apparatus according to claim 7, characterized in that, The processing unit is specifically used for: Determine the first-order moment and the zero-order moment corresponding to the first feature point column; Determine the centroid coordinates of the first feature point column based on the first moment and the zero moment corresponding to the first feature point column; Determine the first and zeroth moments corresponding to the second feature point column; The centroid coordinates of the second feature point sequence are determined based on the first and zero moments corresponding to the second feature point sequence.
9. A computing device, characterized in that, The computing device includes a processor and a memory, the memory storing instructions that, when executed by the processor, implement the method as described in any one of claims 1-4.
10. A computer-readable storage medium, characterized in that, Includes computer instructions that, when executed by a processor, implement the method as described in any one of claims 1-4.