A fast template matching method based on grayscale and related device

By combining image pyramids and integral images, key pixels are extracted for template matching, which solves the problems of high computational load and weak anti-interference ability in traditional methods, and achieves fast and stable template matching results.

CN122289264APending Publication Date: 2026-06-26ZHONGSHAN POWER SUPPLY BUREAU OF GUANGDONG POWER GRID

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ZHONGSHAN POWER SUPPLY BUREAU OF GUANGDONG POWER GRID
Filing Date
2026-05-20
Publication Date
2026-06-26

AI Technical Summary

Technical Problem

Traditional grayscale-based template matching methods are computationally intensive, slow, and sensitive to noise and lighting, making them difficult to apply effectively in complex scenarios.

Method used

An image pyramid strategy is used to construct templates and search image pyramids. Key pixels are extracted based on image edge features, and integral images are used for matching. The resolution is increased layer by layer to determine the target matching position.

Benefits of technology

It significantly reduces computational load, improves matching speed, enhances resistance to noise and light variations, and achieves fast and stable template matching.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122289264A_ABST
    Figure CN122289264A_ABST
Patent Text Reader

Abstract

This invention provides a fast template matching method and related apparatus based on grayscale, belonging to the field of computer vision. The method includes converting a template image and a search image to grayscale to obtain a grayscale template and a search image; constructing a template and search image pyramid using an image pyramid strategy; extracting key pixels at each layer of the template image pyramid and constructing integral images at each layer of the search image pyramid; using the key pixels and integral images, matching the top layer to determine the initial position; using the initial position as a reference, matching layer by layer from the top layer to the highest resolution layer; and determining the coordinates of the region with the highest matching degree in the highest resolution layer as the target position. This invention employs a coarse-to-fine matching method from the top to the bottom of the pyramid, replacing full-pixel calculation with key pixels and quickly solving for similarity using integral images, significantly reducing computational load, improving matching speed, and reducing noise and lighting interference.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer vision technology, specifically relating to a fast template matching method and related apparatus based on grayscale. Background Technology

[0002] In the field of computer vision, image analysis and processing are frequently required. Techniques for finding regions in an image that are similar to a given template have wide applications, playing a crucial role in scenarios such as object recognition and image retrieval. Template matching, as a classic technique for achieving this function, is used in numerous research and practical applications.

[0003] Template matching is mainly divided into gray-level-based matching and feature-based matching. Gray-level-based template matching directly uses the gray-level information of an image for matching. It has good applications in some simple scenarios because gray-level information is relatively easy to obtain and process. It was widely used in early computer vision research and some application scenarios where the accuracy requirements were not particularly high.

[0004] Traditional grayscale template matching methods typically calculate the similarity score between the template and the base image using methods such as sum of squared differences or normalized product correlation. However, these methods suffer from high computational costs, resulting in slow matching speeds. In particular, the sum of squared differences method is extremely sensitive to noise; when there are inconsistencies in lighting between the template and search images, the matching will fail, severely limiting its application in complex real-world scenarios. Summary of the Invention

[0005] In view of this, the present invention provides a fast template matching method and related apparatus based on grayscale, aiming to solve the problems of large computational load, slow speed, sensitivity to noise and susceptibility to light effects of traditional grayscale template matching methods, and to achieve fast and stable template matching.

[0006] To achieve the above objectives, the technical solution provided by the present invention is as follows:

[0007] In a first aspect, the present invention provides a fast template matching method based on grayscale, comprising the following steps:

[0008] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0009] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0010] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0011] For each layer of the search image pyramid, construct an integral image;

[0012] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0013] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0014] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0015] Furthermore, an image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct a template image pyramid and a search image pyramid, including:

[0016] Let the total number of layers in the pyramid be C;

[0017] An image pyramid strategy is used to generate a C-layer template image pyramid and a C-layer search image pyramid for grayscale template images and grayscale search images respectively.

[0018] Furthermore, on each layer of the template image pyramid, key pixels are extracted based on image edge features, including:

[0019] The Canny algorithm is used to extract image edges on each layer of the template image pyramid.

[0020] Adjust the high and low thresholds of the operators in the Canny algorithm based on the total number of pyramid levels;

[0021] Within the image edge of each template image layer, points with pixel gradients greater than the high threshold are identified as strong edge pixels, while points with pixel gradients between the high and low thresholds and connected to strong edge pixels are identified as weak edge pixels. Both strong and weak edge pixels are used as key pixels.

[0022] Furthermore, the high threshold is set to 150-Z*C, and the low threshold is set to 50-Z*C, where C is the total number of pyramid layers and Z is an adjustable constant.

[0023] Furthermore, before extracting key pixels based on image edge features, the process also includes:

[0024] For each layer of the template image pyramid, calculate the average pixel value of the template image.

[0025] Save the average pixel value of the corresponding template image according to the corresponding layer number of each template image.

[0026] Furthermore, using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position, including:

[0027] For the top search image of the search image pyramid, the average pixel value of each region to be matched is calculated using an integral image; the size of the region to be matched is the same as the size of the top template image of the template image pyramid.

[0028] Extract the average pixel value of the template image of the top template image, and the grayscale template image pixel value corresponding to each key pixel point on the top template image;

[0029] Extract the average pixel value of the search image for each region to be matched in the top-level search image, and the grayscale pixel value of the search image at each key pixel position of the top-level template image in each region to be matched.

[0030] For each key pixel, the overlap is calculated as follows:

[0031] H = (t / Tmean) / (s / Smean);

[0032] In the formula, H is the overlap value, t is the grayscale template image pixel value corresponding to the key pixel point, Tmean is the average pixel value of the template image, s is the grayscale search image pixel value at the corresponding key pixel point position, and Smean is the average pixel value of the search image.

[0033] If the overlap value is greater than 1, then the reciprocal of the overlap value is taken.

[0034] The matching degree of the region to be matched is obtained by summing the overlap values ​​of all key pixels and taking the average value.

[0035] Traverse all possible regions to be matched in the top-level search image, and determine the region with the highest matching degree as the initial matching position.

[0036] Furthermore, based on the initial matching position at the top layer, matching is performed layer by layer downwards in both the template image pyramid and the search image pyramid, up to the highest resolution layer, including:

[0037] Based on the center pixel coordinates of the initial matching position, and combined with the resolution relationship between the current image layer and the top layer, the baseline matching region of the current image layer in the search image pyramid is determined;

[0038] Based on the baseline matching region and the calculation methods of overlap and matching degree, the region with the highest matching degree in the current image layer is determined as the optimal matching region of the current image layer.

[0039] The process involves updating the higher-resolution layers to the current image layer, with each layer using the best matching region from the previous layer to determine the baseline matching region, and then performing region matching until the highest-resolution layer of the search image pyramid is matched.

[0040] Secondly, the present invention provides a grayscale-based rapid template matching device, comprising:

[0041] The grayscale processing module is used to perform grayscale processing on the template image and the search image to obtain a grayscale template image and a grayscale search image;

[0042] The image pyramid construction module is used to construct the template image pyramid and the search image pyramid by applying an image pyramid strategy to the grayscale template image and the grayscale search image. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0043] The key pixel extraction module is used to extract key pixels based on image edge features on each layer of the template image pyramid.

[0044] The integral image construction module is used to construct integral images for each layer of the search image pyramid.

[0045] The first matching module is used to match the top layer of the template image pyramid and the search image pyramid using key pixels and integral images to determine the initial matching position.

[0046] The second matching module is used to match layer by layer down the template image pyramid and the search image pyramid, based on the initial matching position at the top layer, until the highest resolution layer.

[0047] The target location determination module is used to determine the coordinates of the region with the highest matching degree in the highest resolution layer as the target matching location.

[0048] Furthermore, the image pyramid construction module is used to apply an image pyramid strategy to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid, including:

[0049] Let the total number of layers in the pyramid be C;

[0050] An image pyramid strategy is used to generate a C-layer template image pyramid and a C-layer search image pyramid for grayscale template images and grayscale search images respectively.

[0051] Furthermore, the key pixel extraction module is used to extract key pixels based on image edge features on each layer of the template image pyramid, including:

[0052] The Canny algorithm is used to extract image edges on each layer of the template image pyramid.

[0053] Adjust the high and low thresholds of the operators in the Canny algorithm based on the total number of pyramid levels;

[0054] Within the image edge of each template image layer, points with pixel gradients greater than the high threshold are identified as strong edge pixels, while points with pixel gradients between the high and low thresholds and connected to strong edge pixels are identified as weak edge pixels. Both strong and weak edge pixels are used as key pixels.

[0055] Furthermore, in the key pixel extraction module, the high threshold is set to 150-Z*C, and the low threshold is set to 50-Z*C, where C is the total number of pyramid layers and Z is an adjustable constant.

[0056] Furthermore, it also includes a storage module for performing the following steps before extracting key pixels based on image edge features:

[0057] For each layer of the template image pyramid, calculate the average pixel value of the template image.

[0058] Save the average pixel value of the corresponding template image according to the corresponding layer number of each template image.

[0059] Furthermore, the first matching module is used to match the top layers of the template image pyramid and the search image pyramid using key pixels and integral images to determine the initial matching position, including:

[0060] For the top search image of the search image pyramid, the average pixel value of each region to be matched is calculated using an integral image; the size of the region to be matched is the same as the size of the top template image of the template image pyramid.

[0061] Extract the average pixel value of the template image of the top template image, and the grayscale template image pixel value corresponding to each key pixel point on the top template image;

[0062] Extract the average pixel value of the search image for each region to be matched in the top-level search image, and the grayscale pixel value of the search image at each key pixel position of the top-level template image in each region to be matched.

[0063] For each key pixel, the overlap is calculated as follows:

[0064] H = (t / Tmean) / (s / Smean);

[0065] In the formula, H is the overlap value, t is the grayscale template image pixel value corresponding to the key pixel point, Tmean is the average pixel value of the template image, s is the grayscale search image pixel value at the corresponding key pixel point position, and Smean is the average pixel value of the search image.

[0066] If the overlap value is greater than 1, then the reciprocal of the overlap value is taken.

[0067] The matching degree of the region to be matched is obtained by summing the overlap values ​​of all key pixels and taking the average value.

[0068] Traverse all possible regions to be matched in the top-level search image, and determine the region with the highest matching degree as the initial matching position.

[0069] Furthermore, the second matching module is used to perform matching layer by layer down the template image pyramid and the search image pyramid, from the initial matching position at the top layer down, until the highest resolution layer, including:

[0070] Based on the center pixel coordinates of the initial matching position, and combined with the resolution relationship between the current image layer and the top layer, the baseline matching region of the current image layer in the search image pyramid is determined;

[0071] Based on the baseline matching region and the calculation methods of overlap and matching degree, the region with the highest matching degree in the current image layer is determined as the optimal matching region of the current image layer.

[0072] The process involves updating the higher-resolution layers to the current image layer, with each layer using the best matching region from the previous layer to determine the baseline matching region, and then performing region matching until the highest-resolution layer of the search image pyramid is matched.

[0073] Thirdly, the present invention provides a computer device, the device including a processor and a memory:

[0074] The memory is used to store computer programs and send the instructions of the computer programs to the processor;

[0075] The processor executes the following steps according to the instructions of the computer program:

[0076] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0077] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0078] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0079] For each layer of the search image pyramid, construct an integral image;

[0080] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0081] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0082] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0083] Fourthly, the present invention provides a computer-readable storage medium on which a computer program is stored, and when executed by a processor, the computer program performs the following steps:

[0084] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0085] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0086] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0087] For each layer of the search image pyramid, construct an integral image;

[0088] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0089] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0090] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0091] Fifthly, the present invention provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:

[0092] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0093] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0094] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0095] For each layer of the search image pyramid, construct an integral image;

[0096] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0097] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0098] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0099] In summary, this invention provides a fast template matching method and related apparatus based on grayscale, comprising: converting a template image and a search image to grayscale to obtain a grayscale template image and a grayscale search image; constructing a template image pyramid and a search image pyramid using an image pyramid strategy; in the image pyramid, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, with the image resolution increasing progressively downwards from the top layer; extracting key pixels based on image edge features on each layer of the template image pyramid; constructing integral images for each layer of the search image pyramid; using the key pixels and integral images, matching the top layer of the template image pyramid and the search image pyramid to determine the initial matching position; using the initial matching position of the top layer as a reference, matching is performed layer by layer downwards from the non-top layer of the template image pyramid and the search image pyramid until the highest resolution layer of the search image pyramid; and determining the coordinates corresponding to the region with the highest matching degree in the highest resolution layer as the target matching position. This invention employs a coarse-to-fine grayscale template matching method from the top to the bottom of the pyramid, using key pixels instead of all pixels in the calculation and integral images to quickly solve similarity problems. This significantly reduces the computational load of grayscale template matching and greatly improves the matching speed. Furthermore, by relying on the characteristics of edge features and key pixels, it effectively reduces the interference of noise and light changes on the matching, solving the problem that traditional methods are easily affected by environmental factors and thus fail to match. This invention achieves a balance between the speed and stability of grayscale template matching. Attached Figure Description

[0100] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0101] Figure 1 A flowchart illustrating a fast template matching method based on grayscale provided in an embodiment of the present invention;

[0102] Figure 2 This is a schematic diagram illustrating the calculation of the average pixel value of a region based on an integral image, as provided in an embodiment of the present invention.

[0103] Figure 3 A block diagram illustrating the composition of a grayscale-based rapid template matching device provided in an embodiment of the present invention;

[0104] Figure 4 This is a block diagram of a computer device provided in an embodiment of the present invention. Detailed Implementation

[0105] To make the objectives, features, and advantages of this invention more apparent and understandable, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described below are only some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.

[0106] The background technology of this invention will be further introduced below.

[0107] Template matching is a fundamental and crucial technique in computer vision. Its purpose is to locate regions in a target search image that are similar to a given template image. It is widely used in image recognition, target tracking, industrial inspection, medical image analysis, and many other technical fields. Based on different similarity measurement methods, template matching techniques are mainly divided into two categories: grayscale-based matching and feature-based matching. Grayscale-based template matching methods, due to their simple principles, lack of complex feature extraction processes, and wide applicability, have become one of the most commonly used template matching methods in industrial practice and daily applications.

[0108] In existing technologies, traditional grayscale-based template matching methods work by defining specific similarity metrics. The template image is slid pixel-by-pixel across the search image, and the similarity between the template region at each sliding position and the sub-region of the search image is calculated. The region with the highest similarity score is then selected as the matching result. Currently, commonly used similarity metrics include sum-of-squared differences (SSD) and normalized cross-correlation (NCC).

[0109] Among them, the Sum of Squared Differences (SSD) method calculates the sum of squares of the differences in grayscale values ​​of corresponding pixels in sub-regions of the template image and the search image. The smaller the sum of squares, the higher the matching degree. This method is simple in principle and intuitive in calculation logic, but it has obvious technical defects: On the one hand, its computational load is extremely large. The higher the resolution of the template image and the search image, the more times the pixel-by-pixel sliding calculation is performed, resulting in a slow matching speed, which is difficult to meet the needs of real-time matching scenarios. On the other hand, this method is extremely sensitive to image noise and changes in lighting. When there are inconsistencies in light intensity or local noise interference between the template image and the search image, the difference in pixel grayscale values ​​will increase significantly, resulting in matching failure and inability to achieve accurate positioning.

[0110] While the Normalized Product Correlation (NCC) method improves the impact of illumination changes on matching results to some extent by normalizing the gray values ​​of sub-regions in the template image and search image, thus reducing interference from uniform illumination changes, the computational complexity of this method remains high. It requires a large number of multiplication and summation operations for each matching region, and also suffers from slow matching speed, making it difficult to adapt to high-resolution images or real-time matching applications.

[0111] To address the technical pain points of traditional grayscale template matching methods, such as high computational load, slow speed, and weak anti-interference ability, some improvement schemes have been proposed in existing technologies. However, most of them have limitations: some schemes only optimize a single defect, such as improving matching speed without improving anti-interference ability, or enhancing noise resistance but leading to a further increase in computational load; other schemes require the introduction of complex feature extraction or preprocessing processes, which increases the complexity and cost of technical implementation, making it difficult to achieve rapid deployment and widespread application.

[0112] In summary, existing grayscale-based template matching methods generally suffer from drawbacks such as high computational load, slow matching speed, sensitivity to noise and illumination changes, and poor adaptability. They cannot simultaneously achieve matching speed, accuracy, and anti-interference capability, making it difficult to meet the demands of practical applications such as high-resolution images and real-time matching. Therefore, developing a fast, accurate, and anti-interference-capable grayscale-based template matching method has become a pressing technical problem for those skilled in the art. Based on this, this invention proposes a fast grayscale-based template matching method and related apparatus. The various embodiments of this invention are described in detail below.

[0113] Please see Figure 1 This embodiment provides a fast template matching method based on grayscale, including the following steps:

[0114] S11: Convert the template image and the search image to grayscale to obtain a grayscale template image and a grayscale search image.

[0115] It should be noted that the template image is the reference image to be used for matching. The search image is the target image in which regions similar to the template image need to be located. Grayscale processing refers to the image processing method of converting the color / multi-channel template image and search image into single-channel grayscale images. The purpose is to eliminate the interference of color channels on matching and retain only the grayscale features of pixels. The grayscale template image and grayscale search image are the single-channel images obtained after grayscale processing of the template image and search image, respectively.

[0116] This step eliminates interference from the image's color channels by converting it to grayscale, thus transforming both the template image and the search image into a single-channel image where grayscale values ​​are the only pixel features.

[0117] S12: An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer. The image resolution increases from the top layer down.

[0118] It should be noted that the image pyramid strategy refers to an image processing strategy that scales single-channel grayscale images in layers according to their resolution from high to low, forming a pyramid-shaped set of layered images. The template image pyramid and the search image pyramid are layered image sets constructed using grayscale template images and grayscale search images as raw data, respectively, employing the image pyramid strategy. The top and bottom layers are the lowest and highest resolution image layers in the image pyramid, respectively.

[0119] This step applies the same scaling rules to the grayscale template image and the grayscale search image to construct an image pyramid with a consistent structure, so that the resolution of the pyramid increases layer by layer from the top to the bottom, and the resolution of the template and the search image at the same layer is matched.

[0120] S13: Extract key pixels based on image edge features on each layer of the template image pyramid.

[0121] It should be noted that image edge features are the regional features of pixels in an image whose pixel grayscale values ​​meet the set requirements. Key pixels are pixels that are extracted from each layer of the template image pyramid based on image edge features and are of practical significance for matching. This can reduce the number of pixels involved in the calculation and accelerate the matching process.

[0122] This step extracts edge features from each layer of the template image pyramid and filters out the key pixels of the template image.

[0123] S14: Construct integral images for each layer of the search image pyramid.

[0124] It should be noted that an integral image is an image representation method used to quickly calculate the sum / mean of pixel gray values ​​in any region of an image. Its characteristic is that the value of any point on the integral image is equal to the sum of the gray values ​​of all pixels in the upper left corner of that point.

[0125] This step constructs an integral image for each layer of the search image pyramid. The integral image can be used to quickly calculate the pixel mean of any region in the search image, greatly reducing the amount of computation on the search image side.

[0126] S15: Using key pixels and integral images, match the top layers of the template image pyramid and the search image pyramid to determine the initial matching position.

[0127] It should be noted that top-level matching is a coarse matching performed at the top level (lowest resolution layer) of the template image pyramid and the search image pyramid. The initial matching position is the coordinate of the region in the top level of the search image pyramid that has a high similarity to the top level of the template image pyramid, obtained through top-level matching. In this step, these are the reference coordinates for non-top-level layer-by-layer matching.

[0128] This step involves reusing the key pixels of the top-level template and the top-level search integral image at the top of the image pyramid (the lowest resolution layer) for coarse matching: using the key pixels as the core of the template side calculation, the gray-scale mean of the corresponding region in the search image is quickly obtained through the integral image, the matching degree between the two is calculated, and the initial matching position is located by comparing the matching degree of each region at the top of the search image.

[0129] S16: Using the initial matching position at the top layer as a reference, perform matching layer by layer down the non-top layer of the template image pyramid and the search image pyramid until the highest resolution layer.

[0130] It should be noted that the non-top-level layer-by-layer matching is based on the initial matching position and is an iterative fine matching completed in the non-top-level (from the second-to-last to the bottom-level) of the template image pyramid and the search image pyramid. In this step, the image resolution increases layer by layer and the matching range decreases layer by layer, realizing the matching logic from coarse to fine.

[0131] This step uses the initial matching position as a reference and performs iterative matching sequentially in the non-top layer of the image pyramid (from the second-to-top layer to the bottom layer, with the resolution increasing layer by layer): each layer narrows the matching range based on the matching results of the previous layer, and reuses the key pixels extracted from the corresponding layer and the integral image to complete the matching calculation.

[0132] S17: Determine the coordinates of the region with the highest matching degree in the highest resolution layer as the target matching position.

[0133] It should be noted that the matching degree is a metric used to quantify the similarity between corresponding regions of the template image and the search image. It is calculated based on the grayscale features of key pixels and the mean features of the integral image regions. In this step, a higher matching degree indicates a higher similarity between the corresponding regions. The target matching position is the coordinate of the region with the highest matching degree at the bottom layer (highest resolution layer) of the search image pyramid.

[0134] This step involves matching layer by layer down to the bottom layer of the search image pyramid (the highest resolution layer), and then selecting the coordinates of the region with the highest matching degree in that layer as the final target matching location.

[0135] The method provided in this embodiment, by constructing a template and a search image pyramid and employing a method of coarse matching at the top lowest resolution layer and fine matching by progressively improving the resolution of non-top layers, not only quickly narrows the matching range but also gradually improves the matching accuracy, balancing matching efficiency and positioning precision. On the template side, key pixels are extracted for each layer of the pyramid, and invalid background pixels are discarded, reducing the number of pixels involved in the matching calculation from the data source. On the search side, integral maps are constructed for each layer of the pyramid, enabling rapid calculation of the pixel mean in any region, abandoning the traditional method of repeatedly traversing pixels, and significantly reducing the overall computational load. At the same time, the entire process is based on grayscale features and combined with layered feature extraction and matching, preserving the core effective features of the image, reducing the interference of noise and illumination changes on the matching results, and improving the method's anti-interference ability.

[0136] In one embodiment of the present invention, an image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct a template image pyramid and a search image pyramid, including:

[0137] S21: Set the total number of pyramid layers to C.

[0138] Where C represents the number of image pyramid segmentation layers set by the user.

[0139] S22: Apply an image pyramid strategy to the grayscale template image and the grayscale search image to generate a C-layer template image pyramid and a C-layer search image pyramid in sequence.

[0140] The same image pyramid scaling rules are applied to the grayscale template image and the grayscale search image to generate a C-layer template image pyramid and a C-layer search image pyramid in sequence. The hierarchical structure and resolution of the two pyramids are completely corresponding, and the number of pixels in the top-level template image with the lowest resolution is not less than 256, ensuring the effectiveness of the top-level coarse matching.

[0141] In this embodiment, by setting a fixed number of layers C and generating corresponding layered templates and search image pyramids, a layered structure of "low resolution at the top and high resolution at the bottom" is formed.

[0142] In one embodiment of the present invention, key pixel points are extracted based on image edge features on each layer of the template image pyramid, including:

[0143] S31: On each layer of the template image pyramid, the Canny algorithm is used to extract image edges.

[0144] In this process, the Canny algorithm is used to extract image edges on each layer of the template image pyramid, and edge regions in the image whose grayscale values ​​meet the requirements are selected.

[0145] S32: Adjust the high and low thresholds of the operators in the Canny algorithm based on the total number of pyramid layers.

[0146] Specifically, based on the total number of pyramid layers, the high and low thresholds of the Canny operator are adaptively adjusted to make the thresholds match the resolution of the corresponding layer template image, thus avoiding edge omissions or false detections due to resolution differences.

[0147] S33: Within the image edge of each template image layer, points with pixel gradients greater than the high threshold are determined as strong edge pixels, and points with pixel gradients between the high and low thresholds and connected to strong edge pixels are determined as weak edge pixels. Strong edge pixels and weak edge pixels are used together as key pixels.

[0148] In each layer of the template image, points with pixel gradients greater than the high threshold are identified as strong edge pixels, and points with pixel gradients between the high and low thresholds and connected to strong edge pixels are identified as weak edge pixels. Strong edge pixels and weak edge pixels are used together as key pixels, and other pixels that do not meet the requirements are discarded.

[0149] In this embodiment, key pixels are extracted for each layer of the template image pyramid. This not only preserves the core effective matching features of the template image, but also reduces the amount of subsequent matching operations from the data source by discarding invalid background pixels. At the same time, the threshold is adaptively adjusted to adapt to template images of different resolution layers, ensuring the extraction accuracy of key pixels in each layer.

[0150] In a further embodiment of the present invention, the high threshold is set to 150-Z*C, the low threshold is set to 50-Z*C, C is the total number of pyramid layers, and Z is an adjustable constant.

[0151] For example, the more layers C there are in the pyramid, the lower the resolution of the template image in each layer and the more blurred the edge features. Therefore, the threshold of the Canny operator needs to be reduced to avoid missing edge detection. For example, when C=3 and Z=5, the high threshold = 150-5×3=135 and the low threshold = 50-5×3=35; when C=5 and Z=7, the high threshold = 150-7×5=115 and the low threshold = 50-7×5=15.

[0152] In this embodiment, C is the number of segmentation layers in the image pyramid. The higher the number of layers, the lower the image resolution, and the threshold for determining the edge should be adaptively reduced. The constant Z in the formula can be adjusted according to the specific matching scenario. It is recommended to use a constant of 5 or 7.

[0153] In one embodiment of the present invention, before extracting key pixels, the method further includes:

[0154] S41: For each layer of the template image pyramid, calculate the average pixel value of the template image.

[0155] Specifically, for each layer of the template image pyramid, the average pixel value of each layer is calculated in descending order of resolution.

[0156] S42: Save the average pixel value of the corresponding template image according to the corresponding layer number of each template image.

[0157] In this embodiment, by calculating the average pixel value of the template image layer by layer and saving it layer by layer, it is ensured that the parameters used for matching calculation of each layer are adapted to the resolution of the corresponding layer template image. At the same time, by saving in advance and calling quickly, the amount of repeated calculation is reduced and the matching efficiency is improved.

[0158] In one embodiment of the present invention, key pixels and integral images are used to match the top layers of the template image pyramid and the search image pyramid to determine the initial matching position, including:

[0159] S51: For the top search image of the search image pyramid, the average pixel value of each region to be matched is calculated using the integral image; the size of the region to be matched is the same as the size of the top template image of the template image pyramid.

[0160] The top-level search image is the lowest-resolution image in the search image pyramid; the integral image is an auxiliary data structure constructed by accumulating global pixel grayscale values ​​to quickly solve the pixel statistics of any rectangular region; the region to be matched is a standardized candidate comparison region defined within the top-level search image, and its size is consistent with the size of the top-level template image in the template image pyramid; the average pixel value of the search image is the arithmetic mean of all effective grayscale pixels within a single region to be matched.

[0161] Optionally, based on the pre-constructed top-level search image integral map, all compliant matching regions are delineated with the constraint that the size of the region to be matched is the same as that of the top-level template image. Utilizing the pixel and fast calculation characteristics of the integral map region, the average grayscale pixel value of each region to be matched is directly calculated without traversing pixel by pixel, thereby completing the quantization of the grayscale benchmark parameters of each candidate comparison region.

[0162] Please see Figure 2 , Figure 2 A method for calculating the average pixel value of a region based on an integral image is shown. Figure 2 In the diagram, a grid structure is used to illustrate the top-level search image. The light gray area represents the region to be matched, which is the same size as the top-level template image. A, B, C, and D are four key reference points on the integral image corresponding to the region to be matched: point A is located outside the upper left corner of the region, point B is located outside the lower right corner, point C is located outside the upper right corner, and point D is located outside the lower left corner. For this light gray region to be matched, the sum of pixels in the region to be matched can be obtained in constant time by adding the values ​​of points B and A in the integral image and then subtracting the values ​​of points C and D, without needing to accumulate pixels one by one. Then, the sum of pixels is divided by the total number of pixels in the region to be matched, and the final result is the mean pixel value (Smean) of the search image for that region.

[0163] S52: Extract the average pixel value of the template image of the top template image, and the grayscale template image pixel value corresponding to each key pixel point on the top template image.

[0164] Among them, the average pixel value of the template image is the arithmetic mean of the grayscale pixels of the top-level template image in the template image pyramid; the key pixels are the set of highly recognizable sampled pixels obtained by filtering based on image edge features; and the pixel value of the grayscale template image is the original grayscale true value corresponding to the key pixels on the top-level template image.

[0165] Optionally, retrieve the average pixel value of the top-level template image that has been calculated and stored in the preprocessing stage, and simultaneously collect the original grayscale values ​​of all key pixels on the top-level template image to form a template-side grayscale feature dataset. Only retain the edge-related key pixel features, remove invalid and redundant pixel data, and complete the extraction and collection of template-side comparison benchmark data.

[0166] S53: Extract the average value of the search image pixels of each region to be matched in the top-level search image, and the grayscale search image pixel values ​​of each region to be matched at the positions of the key pixels of the top-level template image.

[0167] Among them, the average pixel value of the search image is the arithmetic mean of the grayscale pixels of a single region to be matched; the grayscale pixel value of the search image at each key pixel position is the true grayscale value of the pixel in the region to be matched that corresponds one-to-one with the coordinates of the template key pixel; the one-to-one correspondence of coordinates is the spatial position association rule of key pixels between the template image and the search image.

[0168] Optionally, the average grayscale pixel value of each region to be matched is retrieved. Based on the coordinate alignment mapping rules, the grayscale value corresponding to the position of all key pixels on the template side is collected in each region to be matched, forming a grayscale feature dataset on the search side corresponding to each region to be matched, thus completing the extraction and alignment of the search side comparison data.

[0169] S54: For each key pixel, the overlap degree is calculated as follows:

[0170] H = (t / Tmean) / (s / Smean);

[0171] In the formula, H is the overlap value, t is the grayscale template image pixel value corresponding to the key pixel point, Tmean is the average pixel value of the template image, s is the grayscale search image pixel value at the corresponding key pixel point position, and Smean is the average pixel value of the search image.

[0172] Among them, single-point overlap is a quantitative indicator representing the degree of fit between the template and the grayscale features of the search image at a single alignment key pixel point; the calculation formula is a normalized fit calculation model constructed based on the true grayscale values ​​of the template and the search image and the mean values ​​of the corresponding regions.

[0173] Optionally, the gray values ​​of key pixels and the average gray values ​​of corresponding regions of each pair of aligned template images and search images are substituted into the above-mentioned preset standardized calculation formula. Through ratio normalization operation, the interference of illumination and substrate gray-scale offset is eliminated, and the overlap of each key pixel is quantitatively output to achieve accurate quantification of the gray-scale matching degree of a single point.

[0174] It should be noted that if Tmean=0 or Smean=0, the overlap of the region to be matched is determined to be 0 (i.e., no match), and subsequent calculations are skipped.

[0175] S55: If the overlap value is greater than 1, then the reciprocal of the overlap value is taken.

[0176] Optionally, the overlap calculation results of all key pixels are iterated, and the reciprocal operation is performed on the overlap values ​​greater than 1 to unify and constrain all single-point overlap values ​​to a preset comparable range. The purpose of taking the reciprocal is to unify and constrain the overlap of all key pixels to a comparable range of 0-1. The closer the overlap is to 1, the higher the grayscale feature fit between the template and the region to be matched; if the overlap value is greater than 1, its reciprocal is taken to ensure that all overlap values ​​are within the range of 0-1.

[0177] S56: Sum the overlap values ​​of all key pixels and take the average to obtain the matching degree of the region to be matched.

[0178] Among them, the matching degree is the statistical mean of the overlap of all key pixels in a single region to be matched, which is used to characterize the overall fit between the region to be matched and the template image; the summation and mean is a statistical method to quantify discrete single-point features into overall regional features.

[0179] Optionally, for the overlap of all normalized corrected key pixels within a single region to be matched, perform cumulative summation and arithmetic mean operations to transform the discrete single-point fitting features into a quantitative index characterizing the degree of fit between the region and the template image as a whole, thus completing the calculation of the matching degree of the region to be matched.

[0180] S57: Traverse all possible regions to be matched in the top-level search image and determine the region with the highest matching degree as the initial matching position.

[0181] The process of traversal refers to a comprehensive comparison of the matching degree of all regions to be matched and the selection of the best result; the initial matching position is the spatial coordinate of the region to be matched with the highest matching degree in the top-level search image.

[0182] Optionally, all compliant regions to be matched within the top-level search image are traversed, the matching metric results of each region are compared, the region with the highest matching degree is selected, the spatial coordinates corresponding to the region are determined, and the initial matching position is used to complete the coarse localization of the top-level matching.

[0183] In this embodiment, the lowest resolution of the top-level image is utilized to reduce the scope of traversal calculations and improve matching speed. At the same time, the average pixel value of the search image is quickly calculated using an integral image, and the overlap of key pixels is calculated, which simplifies the calculation data and improves the anti-interference ability against noise and lighting changes by calculating the ratio of pixel value to mean value. Finally, the initial matching position is determined by matching degree screening, and the pixel coordinates (x, y) that meet the requirements are recorded.

[0184] In a further embodiment of the present invention, matching is performed layer by layer in the non-top layer of the template image pyramid and the search image pyramid until the highest resolution layer of the search image pyramid is reached, including:

[0185] S61: Based on the center pixel coordinates of the initial matching position, and combined with the resolution relationship between the current image layer and the top layer, determine the reference matching region of the current image layer in the search image pyramid.

[0186] In this context, the center pixel coordinates of the initial matching position refer to the pixel coordinates corresponding to the geometric center of the initial matching region determined by the top-level matching. The current image layer refers to the layer in the template image pyramid and the search image pyramid that is between the top and highest resolution layers and is currently undergoing matching operations. The top layer refers to the layer with the lowest resolution in the image pyramid. The resolution relationship refers to the pixel scaling ratio between the current image layer and the top layer. The reference matching region refers to the candidate region in the current image layer that serves as the starting reference for the matching operation, used to limit the matching range of the current layer.

[0187] Optionally, using the center pixel coordinates of the initial matching position determined by the top layer as a reference, the corresponding reference position in the current image layer is determined through coordinate mapping operation according to the resolution scaling ratio between the current image layer and the top layer; based on this reference position, a rectangular area that conforms to the size of the template image of the current layer is delineated as the reference matching area of ​​the current image layer, thereby limiting the matching search range of the current layer and realizing the positioning and range constraint of the matching area.

[0188] For example, in non-top-level matching, the image matching area of ​​the next layer is determined based on the coordinate point (x,y) of the matching of the previous layer, such as performing fine matching in a 5*5 pixel area centered at (2*x,2*y).

[0189] S62: Based on the baseline matching region and the calculation methods of overlap and matching degree, the region with the highest matching degree in the current image layer is determined as the optimal matching region of the current image layer.

[0190] The overlap calculation method refers to the standardized computational logic used to quantify the degree of fit between the template and the grayscale features of the search image at a single aligned key pixel (i.e., the calculation method shown in step S54 of the aforementioned embodiment). The matching degree calculation method refers to the quantitative computational logic of aggregating and statistically analyzing the overlap of all key pixels within a single region to obtain the overall fitting degree of the region (i.e., the calculation method shown in steps S55-S56 of the aforementioned embodiment). The optimal matching region refers to the candidate matching region in the current image layer that has the highest degree of fit with the grayscale features of the corresponding layer template image and the largest matching degree value.

[0191] Optionally, the baseline matching region of the current image layer is used as the search range. The preset overlap and matching degree calculation method is used to calculate all compliant candidate matching regions within this range. The matching degree of each candidate matching region is calculated one by one. The candidate region with the highest matching degree is selected by quantitative comparison and determined as the optimal matching region of the current image layer, thus completing the fine matching of the current layer.

[0192] S63: Update the higher resolution layer to the current image layer layer by layer. For each layer, determine the reference matching region based on the best matching region of the previous layer and perform region matching until the highest resolution layer of the search image pyramid is matched.

[0193] In this context, a higher-resolution layer refers to an image layer in the image pyramid that is lower than the current image layer, has more pixels, and provides clearer details. Updating the current image layer means replacing the higher-resolution layer in the image pyramid with the layer currently being used for matching. The highest-resolution layer is the lowest-level image in the image pyramid with the highest resolution, the most pixels, and the clearest details.

[0194] Optionally, after determining the optimal matching region of the current image layer, the adjacent layers with higher resolution in the image pyramid are updated as the new current image layer; based on the optimal matching region of the current layer before the update, and combined with the resolution relationship between the new current layer and the previous layer, the reference matching region of the new current layer is determined; the matching operation logic of S62 is repeated to determine the optimal matching region of the new current layer; this process is iterated until the matching operation is performed up to the highest resolution layer of the search image pyramid, thus completing the fine matching of all levels.

[0195] In this embodiment, based on the upper-level matching results, the matching accuracy is gradually improved by narrowing the matching range layer by layer and increasing the image resolution. At the same time, the matching calculation method of the previous embodiment is reused to ensure the logical consistency and data correlation of the entire matching process. This avoids a large amount of computation in high-resolution traversal of the entire image and achieves accurate positioning through iterative optimization.

[0196] Based on the same inventive concept, this application also provides a grayscale-based fast template matching device for implementing the grayscale-based fast template matching method described above. The solution provided by this device is similar to the implementation described in the above method; therefore, the specific limitations in the embodiments of the grayscale-based fast template matching device provided below can be found in the limitations of the grayscale-based fast template matching method described above, and will not be repeated here.

[0197] Please see Figure 3 This invention also provides a grayscale-based fast template matching device, comprising:

[0198] The grayscale processing module is used to perform grayscale processing on the template image and the search image to obtain a grayscale template image and a grayscale search image;

[0199] The image pyramid construction module is used to construct the template image pyramid and the search image pyramid by applying an image pyramid strategy to the grayscale template image and the grayscale search image. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0200] The key pixel extraction module is used to extract key pixels based on image edge features on each layer of the template image pyramid.

[0201] The integral image construction module is used to construct integral images for each layer of the search image pyramid.

[0202] The first matching module is used to match the top layer of the template image pyramid and the search image pyramid using key pixels and integral images to determine the initial matching position.

[0203] The second matching module is used to match layer by layer down the template image pyramid and the search image pyramid, based on the initial matching position at the top layer, until the highest resolution layer.

[0204] The target location determination module is used to determine the coordinates of the region with the highest matching degree in the highest resolution layer as the target matching location.

[0205] In one embodiment, the image pyramid construction module is used to apply an image pyramid strategy to the grayscale template image and the grayscale search image to construct a template image pyramid and a search image pyramid, including:

[0206] Let the total number of layers in the pyramid be C;

[0207] An image pyramid strategy is used to generate a C-layer template image pyramid and a C-layer search image pyramid for grayscale template images and grayscale search images respectively.

[0208] In one embodiment, the key pixel extraction module is used to extract key pixels based on image edge features on each layer of the template image pyramid, including:

[0209] The Canny algorithm is used to extract image edges on each layer of the template image pyramid.

[0210] Adjust the high and low thresholds of the operators in the Canny algorithm based on the total number of pyramid levels;

[0211] Within the image edge of each template image layer, points with pixel gradients greater than the high threshold are identified as strong edge pixels, while points with pixel gradients between the high and low thresholds and connected to strong edge pixels are identified as weak edge pixels. Both strong and weak edge pixels are used as key pixels.

[0212] In one embodiment, in the key pixel extraction module, the high threshold is set to 150-Z*C, the low threshold is set to 50-Z*C, C is the total number of pyramid layers, and Z is an adjustable constant.

[0213] In one embodiment, a storage module is also included, which performs the following steps before extracting key pixels based on image edge features:

[0214] For each layer of the template image pyramid, calculate the average pixel value of the template image.

[0215] Save the average pixel value of the corresponding template image according to the corresponding layer number of each template image.

[0216] In one embodiment, the first matching module is used to match the top layers of the template image pyramid and the search image pyramid using key pixels and integral images to determine the initial matching position, including:

[0217] For the top search image of the search image pyramid, the average pixel value of each region to be matched is calculated using an integral image; the size of the region to be matched is the same as the size of the top template image of the template image pyramid.

[0218] Extract the average pixel value of the template image of the top template image, and the grayscale template image pixel value corresponding to each key pixel point on the top template image;

[0219] Extract the average pixel value of the search image for each region to be matched in the top-level search image, and the grayscale pixel value of the search image at each key pixel position of the top-level template image in each region to be matched.

[0220] For each key pixel, the overlap is calculated as follows:

[0221] H = (t / Tmean) / (s / Smean);

[0222] In the formula, H is the overlap value, t is the grayscale template image pixel value corresponding to the key pixel point, Tmean is the average pixel value of the template image, s is the grayscale search image pixel value at the corresponding key pixel point position, and Smean is the average pixel value of the search image.

[0223] If the overlap value is greater than 1, then the reciprocal of the overlap value is taken.

[0224] The matching degree of the region to be matched is obtained by summing the overlap values ​​of all key pixels and taking the average value.

[0225] Traverse all possible regions to be matched in the top-level search image, and determine the region with the highest matching degree as the initial matching position.

[0226] In one embodiment, the second matching module is configured to perform matching layer by layer down the template image pyramid and the search image pyramid, from the initial matching position at the top layer down to the highest resolution layer, including:

[0227] Based on the center pixel coordinates of the initial matching position, and combined with the resolution relationship between the current image layer and the top layer, the baseline matching region of the current image layer in the search image pyramid is determined;

[0228] Based on the baseline matching region and the calculation methods of overlap and matching degree, the region with the highest matching degree in the current image layer is determined as the optimal matching region of the current image layer.

[0229] The process involves updating the higher-resolution layers to the current image layer, with each layer using the best matching region from the previous layer to determine the baseline matching region, and then performing region matching until the highest-resolution layer of the search image pyramid is matched.

[0230] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is merely an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the system can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiments 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. Furthermore, the specific names of the functional units and modules are only for easy differentiation and are not intended to limit the scope of protection of this application. The specific working process of the units and modules in the above system can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.

[0231] Reference Figure 4 This invention also provides a computer device, including: a memory and a processor, and a computer program stored in the memory. When the computer program is executed on the processor, it performs the following steps:

[0232] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0233] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0234] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0235] For each layer of the search image pyramid, construct an integral image;

[0236] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0237] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0238] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0239] The computer device may be a desktop computer, laptop, handheld computer, or cloud server, etc. This computer device may include, but is not limited to, a processor and memory. Those skilled in the art will understand that... Figure 4 The examples of computer devices are merely examples and do not constitute a limitation on computer devices. They may include more or fewer components than shown in the illustration, or combinations of certain components, or different components. For example, they may also include input / output devices, network access devices, etc.

[0240] The processor referred to can be a Central Processing Unit (CPU), but it can also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor.

[0241] In some embodiments, the memory may be an internal storage unit of the computer device, such as a hard drive or RAM. In other embodiments, the memory may be an external storage device of the computer device, such as a plug-in hard drive, Smart Media Card (SMC), Secure Digital (SD) card, or Flash Card. Furthermore, the memory may include both internal and external storage units of the computer device. The memory is used to store the operating system, applications, boot loader, data, and other programs, such as the program code of the computer program. The memory can also be used to temporarily store data that has been output or will be output.

[0242] This invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:

[0243] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0244] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0245] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0246] For each layer of the search image pyramid, construct an integral image;

[0247] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0248] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0249] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0250] In this embodiment, 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, all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include at least: any entity or device capable of carrying computer program code to a photographing device / terminal device, a recording medium, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium. Examples include USB flash drives, portable hard drives, magnetic disks, or optical disks. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electrical carrier signals or telecommunication signals.

[0251] This invention provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:

[0252] The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image.

[0253] An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct the template image pyramid and the search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer to the bottom layer.

[0254] Key pixels are extracted based on image edge features on each layer of the template image pyramid.

[0255] For each layer of the search image pyramid, construct an integral image;

[0256] By using key pixels and integral images, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position;

[0257] Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached;

[0258] The coordinates of the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

[0259] In the above embodiments, the descriptions of each embodiment have different focuses. For parts that are not described in detail or recorded in a certain embodiment, please refer to the relevant descriptions of other embodiments.

[0260] Those skilled in the art will recognize that the units and algorithm steps of the various examples 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 implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art 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.

[0261] In the embodiments disclosed in this application, it should be understood that the disclosed devices / terminal equipment and methods can be implemented in other ways. For example, the device / terminal equipment embodiments described above are merely illustrative. For instance, the division of modules or 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 displayed or discussed mutual coupling or direct coupling or communication connection may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.

[0262] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A fast template matching method based on grayscale, characterized in that, Includes the following steps: The template image and the search image are converted to grayscale to obtain a grayscale template image and a grayscale search image. An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct a template image pyramid and a search image pyramid. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, with the image resolution increasing from the top layer downwards. Key pixels are extracted from each layer of the template image pyramid based on image edge features; For each layer of the search image pyramid, an integral image is constructed respectively; Using the key pixels and the integral image, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position; Based on the initial matching position at the top layer, matching is performed layer by layer down in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached; The coordinates corresponding to the region with the highest matching degree in the highest resolution layer are determined as the target matching position.

2. The fast template matching method based on grayscale according to claim 1, characterized in that, An image pyramid strategy is applied to the grayscale template image and the grayscale search image to construct a template image pyramid and a search image pyramid, including: Let the total number of layers in the pyramid be C; An image pyramid strategy is applied to the grayscale template image and the grayscale search image to generate a C-layer template image pyramid and a C-layer search image pyramid in sequence.

3. The fast template matching method based on grayscale according to claim 1, characterized in that, On each layer of the template image pyramid, key pixels are extracted based on image edge features, including: On each layer of the template image pyramid, the Canny algorithm is used to extract image edges; Adjust the high and low thresholds of the operators in the Canny algorithm based on the total number of pyramid levels; Within the image edge of each template image layer, points with pixel gradients greater than the high threshold are determined as strong edge pixels, and points with pixel gradients between the high threshold and the low threshold and connected to the strong edge pixels are determined as weak edge pixels. The strong edge pixels and weak edge pixels are collectively used as the key pixels.

4. The fast template matching method based on grayscale according to claim 3, characterized in that, The high threshold is set to 150-Z*C, and the low threshold is set to 50-Z*C, where C is the total number of pyramid layers and Z is an adjustable constant.

5. The fast template matching method based on grayscale according to claim 1, characterized in that, Before extracting key pixels based on image edge features, the process also includes: For each layer of the template image pyramid, calculate the average pixel value of the template image. Save the average pixel value of the template image according to the corresponding layer number of each template image.

6. The fast template matching method based on grayscale according to claim 5, characterized in that, Using the key pixels and the integral image, the top layers of the template image pyramid and the search image pyramid are matched to determine the initial matching position, including: For the top search image of the search image pyramid, the average pixel value of each region to be matched is calculated using the integral image; the size of the region to be matched is the same as the size of the top template image of the template image pyramid. Extract the average pixel value of the template image of the top-level template image, and the grayscale template image pixel value corresponding to each key pixel point on the top-level template image; Extract the average value of the search image pixels of each region to be matched in the top-level search image, and the grayscale search image pixel values ​​of each region to be matched at the positions of each key pixel point of the top-level template image. For each of the key pixels, the overlap degree is calculated as follows: H = (t / Tmean) / (s / Smean); In the formula, H is the overlap value, t is the grayscale template image pixel value corresponding to the key pixel point, Tmean is the average pixel value of the template image, s is the grayscale search image pixel value at the position of the key pixel point, and Smean is the average pixel value of the search image. If the overlap value is greater than 1, then the reciprocal of the overlap value is taken. The matching degree of the region to be matched is obtained by summing the overlap values ​​of all key pixels and taking the average value. Traverse all possible regions to be matched in the top-level search image, and determine the region with the highest matching degree as the initial matching position.

7. The fast template matching method based on grayscale according to claim 6, characterized in that, Based on the initial matching position at the top layer, matching is performed layer by layer downwards in the non-top layers of the template image pyramid and the search image pyramid until the highest resolution layer is reached, including: Based on the center pixel coordinates of the initial matching position, and combined with the resolution relationship between the current image layer and the top layer, the reference matching region of the current image layer in the search image pyramid is determined; Based on the benchmark matching region and the calculation methods of the overlap and matching degree, the region with the highest matching degree in the current image layer is determined as the optimal matching region of the current image layer. The higher resolution layer is updated to the current image layer layer by layer. Each layer determines the reference matching region based on the optimal matching region of the previous layer and performs region matching until the highest resolution layer of the search image pyramid is matched.

8. A grayscale-based rapid template matching device, characterized in that, include: The grayscale processing module is used to perform grayscale processing on the template image and the search image to obtain a grayscale template image and a grayscale search image; The image pyramid construction module is used to construct a template image pyramid and a search image pyramid by applying an image pyramid strategy to the grayscale template image and the grayscale search image. In the image pyramid strategy, the top layer is the lowest resolution layer and the bottom layer is the highest resolution layer, and the image resolution increases from the top layer down. The key pixel extraction module is used to extract key pixels based on image edge features on each layer of the template image pyramid. The integral image construction module is used to construct integral images for each layer of the search image pyramid. The first matching module is used to match the top layer of the template image pyramid and the search image pyramid using the key pixels and the integral image to determine the initial matching position. The second matching module is used to perform matching layer by layer down the template image pyramid and the search image pyramid, based on the initial matching position at the top layer, until the highest resolution layer. The target location determination module is used to determine the coordinates of the region with the highest matching degree in the highest resolution layer as the target matching location.

9. A computer device, characterized in that, The device includes a processor and a memory: The memory is used to store computer programs and send the instructions of the computer programs to the processor; The processor executes a grayscale-based fast template matching method according to the instructions of the computer program as described in any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements a grayscale-based fast template matching method as described in any one of claims 1-7.