Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth, such as particular system configurations, techniques, etc. in order to provide a thorough understanding of the embodiments of the present application. It will be apparent, however, to one skilled in the art that the present application may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present application with unnecessary detail.
It should be understood that the terms "comprises" and/or "comprising," when used in this specification and the appended claims, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
It should also be understood that the term "and/or" as used in this specification and the appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes such combinations.
As used in this specification and the appended claims, the term "if" may be interpreted as "when..once" or "in response to a determination" or "in response to detection" depending on the context. Similarly, the phrase "if a determination" or "if a [ described condition or event ] is detected" may be interpreted in the context of meaning "upon determination" or "in response to determination" or "upon detection of a [ described condition or event ]" or "in response to detection of a [ described condition or event ]".
In addition, in the description of the present application and the appended claims, the terms "first," "second," "third," and the like are used merely to distinguish between descriptions and are not to be construed as indicating or implying relative importance.
Reference in the specification to "one embodiment" or "some embodiments" or the like means that a particular feature, structure, or characteristic described in connection with the embodiment is included in one or more embodiments of the application. Thus, appearances of the phrases "in one embodiment," "in some embodiments," "in other embodiments," and the like in the specification are not necessarily all referring to the same embodiment, but mean "one or more but not all embodiments" unless expressly specified otherwise. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless expressly specified otherwise.
It should be understood that the sequence numbers of the steps in the following embodiments do not mean the order of execution, and the execution order of the processes should be determined by the functions and the internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
In order to illustrate the technical solution of the present application, the following description is made by specific examples.
The template matching method for the image provided by the embodiment of the invention can be applied to an application environment as shown in fig. 1, wherein a client communicates with a server to form a cloud edge combined system. The client includes, but is not limited to, a palm computer, a desktop computer, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a server computer device, a personal digital assistant (personal digital assistant, PDA), and other computer devices, and the client may also be a data processing device of an electrocardiograph, an electroencephalograph, and other measuring devices. The server may be implemented by a stand-alone server or a server cluster formed by a plurality of servers.
Referring to fig. 2, a flow chart of a template matching method of an image according to a second embodiment of the present application is shown, where the template matching method of an image is applied to a server in fig. 1, and a computer device corresponding to the server is connected to a corresponding database to obtain a corresponding target image and a template image in the database, and of course, the template image may also be directly stored in the server. The computer equipment can be connected with corresponding clients, the clients are operated by users, and the users can acquire corresponding target images through the clients and provide the corresponding target images to the server. As shown in fig. 2, the template matching method of the image may include the steps of:
step S201, for any region to be matched in the target image, determining a registration region corresponding to the region to be matched in the template image, obtaining first target information composed of pixel information of the region to be matched and its neighborhood, and forming second target information composed of pixel information of the registration region and its neighborhood.
In the application, the target image may be an image acquired by using a certain image acquisition device, and in a precise industrial scene, the target image may be an image acquired aiming at details of a workpiece and a part, so that characteristics of the workpiece and the like can be determined.
The region to be matched can be a region defined by a user according to requirements, specifically, a bounding region can be defined from an image by adopting a bounding box with a preset size, the bounding region can be used as the region to be matched, in general, all regions in the whole image can be required to be matched, at this time, the whole image can be divided into different small regions, and the region to be matched is any small region in the whole image.
The template image is a preset image meeting the requirement of a user, and can be an image with the same attribute as the acquisition range of the target image, such as the size of the acquisition range, or an image with the acquisition range smaller than the acquisition range of the target image. If the template image and the target image are images with the same attribute, the position of the registration area in the template image and the position of the area to be matched in the target image are the same, and of course, if the acquisition range of the template image is smaller than the acquisition range of the target image, the shape registration and other operations can be performed on the target image and the template image, so that after the target image and the template image are registered, the registration area is determined in the template image according to the position of the area to be matched in the target image.
The field of the region to be matched can refer to a region adjacent to the region to be matched, specifically can be a 4 neighborhood or an 8 neighborhood, the size of the neighborhood is the same as that of the corresponding surrounding region, and the neighborhood of the registration region is selected in the same manner as that of the region to be matched, namely the region to be matched and the neighborhood thereof are registered with the registration region and the neighborhood thereof. The target information may be pixel information of an image such as a pixel value, a luminance value, and a gradation value.
Optionally, determining the registration area corresponding to the area to be matched in the template image includes:
and performing shape registration on the template image and the target image by using singular value decomposition and principal component extraction, and determining a registration area corresponding to the area to be matched from the registered template image.
Wherein the registration adopts the following formula:
wherein the formula is a singular value decomposition process, the matrix A of the image is decomposed into U, E, V matrix representations, m and n respectively represent the number of rows and the number of columns of the image, and the columns of U form a set of basis vectors for orthogonal input of A, namely AA
T The columns of V form a set of basis vectors for the quadrature outputs of A, i.e. A
T The eigenvectors of A, the elements on the diagonal of E being singular values, can be considered as scalar expansion control between input and output, i.e., AA
T A is a
T The singular value of A corresponds to the column vector of U and V, k is a preset approximate value, and is considered in an approximate mode
The characteristic information of the first 90% is retained for E, and the percentage can be changed according to the use scene. And (3) downsampling by using U and V, wherein only principal component information is reserved, and the sampling standard is maximum sampling, namely only the maximum value of a sampling interval is reserved.
The target image and the template image are subjected to shape registration by using singular value decomposition and principal component extraction, and restored according to a fixed size when the feature map is reconstructed by using a decomposition matrix, wherein the fixed size is smaller than that of the template map and the target map, for example, a size of 64 x 32 can be adopted in a wafer detection scene.
Step S202, determining first gray scale characteristic information and first structural characteristic information on a region to be matched and a neighborhood thereof according to first target information, and determining second gray scale characteristic information and second structural characteristic information on a registration region and the neighborhood thereof according to second target information.
In this application, the gray characteristic information may refer to an image gray data value representing a distribution of gray, contrast, etc., for example, a gray value, a gray variance, a gray mean value, a brightness contrast value, etc. The structural feature information may refer to data values characterizing morphological distributions such as texture in an image, e.g., local binary pattern (Local Binary Patterns, LBP) encoded values, for describing the texture of the image.
Optionally, determining the first gray scale feature information and the first structural feature information on the region to be matched and the neighborhood thereof according to the first target information includes:
carrying out mean value calculation, variance calculation, LBP value calculation and LBP coding calculation on the first target information, and determining a first mean value, a first variance, a first LBP value and a first LBP coding value of a region to be matched and a neighborhood thereof;
taking the first mean value, the first variance and the first LBP value as first gray scale characteristic information;
determining pixel difference values of the region to be matched and the neighborhood thereof according to the first target information, and normalizing all the pixel difference values to obtain a normalization value corresponding to each neighborhood;
multiplying the normalized value of each neighborhood with the first LBP coding value of the corresponding neighborhood to obtain a first feature vector representing the corresponding neighborhood;
and adding the first feature vectors of all the neighborhoods to obtain a first addition result, calculating first included angles of all the first feature vectors and the first addition result, and taking the calculation result of the first included angle of each neighborhood as first structural feature information.
The mean value calculation, variance calculation, LBP value calculation, and LPP value encoding calculation may be calculated based on pixel values of the image, or may be calculated based on gray values.
Taking pixel values as an example, the mean value and the variance of the region to be matched are calculated according to the pixel values of all the pixel points in the region to be matched, and the mean value and the variance of the neighborhood are calculated according to the pixel values of all the pixel points in the corresponding neighborhood.
The LBP value of the region to be matched may refer to the LBP value of each pixel in the region to be matched, and then the average value is calculated, where the average value is the LBP value of the region to be matched, the LBP value of the neighborhood may refer to the LBP value of each pixel in the corresponding neighborhood, and then the average value is calculated, where the average value is the LBP value of the neighborhood, and the LBP value of one pixel is calculated based on the pixel values of the surrounding pixels. If the region to be matched is only one pixel, the corresponding neighborhood is also one pixel, and the LBP value of the neighborhood needs to be calculated through the neighborhood of the corresponding neighborhood.
The region to be matched and the first mean value, the first variance and the first LBP value of the neighborhood form mean distribution, variance distribution and LBP distribution, and the distribution is the first gray characteristic information representing the gray characteristic.
If the region to be matched comprises at least two pixel points, the neighborhood also comprises at least two pixel points, the average value of all the pixel points can be adopted when the pixel difference value is calculated, the LBP coding is carried out according to the average value of all the pixel points when the LBP coding value is calculated, if the region to be matched comprises one pixel point, the neighborhood comprises one pixel point, and the pixel difference value calculation and the LBP coding can be directly carried out by using the pixel value of the corresponding pixel point.
Since the LBP code value is 0 or 1, the purpose of normalization is to normalize the information characterizing the structural feature for subsequent weighted summation with the gray scale feature.
The first feature vector is a vector representing a corresponding neighborhood, the magnitude of the vector is expressed by the multiplied product, the vector direction points to the center of the corresponding neighborhood from the center of the region to be matched, and the first addition result is a main vector representing the structural change trend of the region to be matched, namely, the first feature vector is obtained by vector addition.
And calculating a first included angle between all the first feature vectors and the first addition result, namely calculating an included angle between the two vectors, so that each neighborhood corresponds to an included angle value, and the difference between each neighborhood and the structural change trend can be represented.
Optionally, determining the second gray scale feature information and the second structural feature information on the registration area and the neighborhood thereof according to the second target information includes:
performing mean value calculation, variance calculation, LBP value calculation and LBP coding calculation on the second target information, and determining a second mean value, a second variance, a second LBP value and a second LBP coding value of the registration area and the neighborhood thereof;
taking the second mean value, the second variance and the second LBP value as second gray characteristic information;
Determining pixel difference values of the registration area and the neighborhood thereof according to the second target information, and normalizing all the pixel difference values to obtain a normalization value corresponding to each neighborhood;
multiplying the normalized value of each neighborhood with the second LBP coding value of the corresponding neighborhood to obtain a second feature vector representing the corresponding neighborhood;
and adding the second feature vectors of all the neighborhoods to obtain a second addition result, calculating second included angles of all the second feature vectors and the second addition result, and taking the calculation result of the second included angle of each neighborhood as second structural feature information.
In one embodiment, after the gray feature information and the structural feature information of the template image are obtained, the gray feature information and the structural feature information obtained by the calculation may be directly used in the subsequent use of the template image.
Step S203, comparing the first gray scale characteristic information with the second gray scale characteristic information to obtain gray scale difference information, and comparing the first structural characteristic information with the second structural characteristic information to obtain structural difference information.
In the method, gray characteristic information and structural characteristic information of the target image and the template image are compared to obtain differences, and the differences are used for calculating the subsequent matching degree or similarity.
Optionally, comparing the first gray scale feature information with the second gray scale feature information to obtain gray scale difference information includes:
calculating a first KL divergence of the region to be matched and the registration region according to the first mean value and the second mean value;
calculating a second KL divergence of the region to be matched and the registration region according to the first variance and the second variance;
and calculating third KL divergences of the region to be matched and the registration region according to the first LBP value and the second LBP value, and determining the first KL divergences, the second KL divergences and the third KL divergences as gray difference information.
And calculating KL divergences of the target image and the template image respectively according to the mean value dimension, the variance dimension and the LBP value dimension, and using the KL divergences to represent the gray level difference. The value intervals of the KL divergence are 0 and 1, and the larger the corresponding value is, the more matching is indicated.
Optionally, comparing the first structural feature information with the second structural feature information to obtain structural difference information includes:
comparing the calculation result of the first included angle of each neighborhood in the first structural feature information with the second included angle of the corresponding neighborhood in the second structural feature information to determine the difference value of the included angles of each neighborhood;
and calculating a corresponding difference mean value and a difference variance according to the included angle difference value of each neighborhood, normalizing the half-width of normal distribution formed by the difference mean value and the difference variance, and determining the normalized value of the half-width as the structural difference information.
And calculating the difference value between the first included angle of the neighborhood of the region to be matched and the second included angle of the neighborhood corresponding to the registration region to obtain the difference value of the included angles of each neighborhood, and calculating the mean value and the variance of the difference values of all the included angles. And forming normal distribution by the mean value and the variance to obtain half-width of the normal distribution, wherein the half-width is divided by 180 to normalize the half-width because the angle is 0-180 degrees.
And S204, carrying out weighted summation on the gray level difference information and the structure difference information to obtain a weighted summation result, and determining that the target image is matched with the template image in the region to be matched when the weighted summation result meets the preset condition.
In the present application, the weights of the two pieces of information in the weighted summation may be set according to the requirements or the test results. The preset condition may be that a threshold value or a threshold value range is set, when the weighted sum result is greater than the threshold value or belongs to the threshold value range, the target image and the template image are determined to be matched, otherwise, the target image and the template image are not matched.
Optionally, the weighted summation of the gray scale difference information and the structure difference information is performed, and obtaining a weighted summation result includes:
averaging the first KL divergence, the second KL divergence and the third KL divergence to obtain a divergence average value;
And carrying out weighted summation on the divergence mean value and the normalized value of the half-width to obtain a weighted summation result.
And carrying out weighted summation on the average value of the three KL divergences and the normalized value of the half-width so that the weighted summation result is controlled between [0,1], wherein the larger the value of the weighted summation result is, the higher the matching degree is. The weight of both is 0.5.
In an embodiment, when the accuracy requirement is reduced, only one of the three KL divergences may be used to perform weighted summation with the normalized value of the half-width.
According to any region to be matched in the target image, a registration region corresponding to the region to be matched is determined in the template image, first target information formed by pixel information of the region to be matched and the neighborhood thereof is obtained, and second target information formed by pixel information of the registration region and the neighborhood thereof is obtained.
Fig. 3 shows a block diagram of a template matching device of an image according to a third embodiment of the present application, where the template matching device is applied to a server in fig. 1, and a computer device corresponding to the server is connected to a corresponding database to obtain a corresponding target image and a template image in the database, and of course, the template image may also be directly stored in the server. The computer equipment can be connected with corresponding clients, the clients are operated by users, and the users can acquire corresponding target images through the clients and provide the corresponding target images to the server. For convenience of explanation, only portions relevant to the embodiments of the present application are shown.
Referring to fig. 3, the template matching apparatus includes:
the information obtaining module 31 is configured to determine, for any region to be matched in the target image, a registration region corresponding to the region to be matched in the template image, obtain first target information formed by pixel information of the region to be matched and a neighborhood thereof, and obtain second target information formed by pixel information of the registration region and the neighborhood thereof;
the feature determining module 32 is configured to determine, according to the first target information, first gray scale feature information and first structural feature information on the region to be matched and the neighborhood thereof, and determine, according to the second target information, second gray scale feature information and second structural feature information on the registration region and the neighborhood thereof;
A difference comparison module 33, configured to compare the first gray scale feature information and the second gray scale feature information to obtain gray scale difference information, and compare the first structural feature information and the second structural feature information to obtain structural difference information;
the matching result determining module 34 is configured to perform weighted summation on the gray scale difference information and the structure difference information to obtain a weighted summation result, and determine that the target image matches the template image in the region to be matched when the weighted summation result meets a preset condition.
Optionally, the feature determining module 32 includes:
the first calculating unit is used for carrying out mean value calculation, variance calculation, LBP value calculation and LBP coding calculation on the first target information, and determining a first mean value, a first variance, a first LBP value and a first LBP coding value of the region to be matched and the neighborhood thereof;
a first gray feature determining unit configured to take the first mean, the first variance, and the first LBP value as first gray feature information;
the first difference normalization unit is used for determining pixel differences between the region to be matched and the neighborhood thereof according to first target information, and normalizing all the pixel differences to obtain normalization values corresponding to each neighborhood;
the first feature vector determining unit is used for multiplying the normalized value of each neighborhood with the first LBP coding value of the corresponding neighborhood to obtain a first feature vector representing the corresponding neighborhood;
The first structural feature determining unit is used for adding the first feature vectors of all the neighborhoods to obtain a first addition result, calculating first included angles of all the first feature vectors and the first addition result, and taking the calculation result of the first included angle of each neighborhood as first structural feature information.
Optionally, the feature determining module 32 includes:
the second calculating unit is used for carrying out mean value calculation, variance calculation, LBP value calculation and LBP coding calculation on the second target information and determining a second mean value, a second variance, a second LBP value and a second LBP coding value of the registration area and the neighborhood thereof;
a second gray feature determining unit, configured to use the second mean, the second variance, and the second LBP value as second gray feature information;
the second difference normalization unit is used for determining pixel differences between the registration area and the neighborhood thereof according to second target information, and normalizing all the pixel differences to obtain a normalization value corresponding to each neighborhood;
the second feature vector determining unit is used for multiplying the normalized value of each neighborhood with the second LBP coding value of the corresponding neighborhood to obtain a second feature vector representing the corresponding neighborhood;
and the second structural feature determining unit is used for adding the second feature vectors of all the neighborhoods to obtain a second addition result, calculating second included angles of all the second feature vectors and the second addition result, and taking the calculation result of the second included angle of each neighborhood as second structural feature information.
Optionally, the difference comparison module 33 includes:
the first divergence calculating unit is used for calculating a first KL divergence of the region to be matched and the registration region according to the first mean value and the second mean value;
a second divergence calculating unit, configured to calculate a second KL divergence of the region to be matched and the registration region according to the first variance and the second variance;
and the third divergence calculating unit is used for calculating the third KL divergence of the region to be matched and the registration region according to the first LBP value and the second LBP value, and determining the first KL divergence, the second KL divergence and the third KL divergence as gray difference information.
Optionally, the difference comparison module 33 includes:
the included angle difference value determining unit is used for comparing the calculation result of the first included angle of each neighborhood in the first structural feature information with the second included angle of the corresponding neighborhood in the second structural feature information to determine the included angle difference value of each neighborhood;
the structure difference determining unit is used for calculating a corresponding difference mean value and a difference variance according to the included angle difference value of each neighborhood, normalizing the half-width of normal distribution formed by the difference mean value and the difference variance, and determining the normalized value of the half-width as the structure difference information.
Optionally, the matching result determining module 34 includes:
The divergence average value calculation unit is used for averaging the first KL divergence, the second KL divergence and the third KL divergence to obtain a divergence average value;
and the matching value calculation unit is used for carrying out weighted summation on the divergence mean value and the half-width normalized value to obtain a weighted summation result.
Optionally, the information obtaining module 31 includes:
and the region registration unit is used for carrying out shape registration on the template image and the target image by using singular value decomposition and principal component extraction, and determining a registration region corresponding to the region to be matched from the registered template image.
It should be noted that, because the content of information interaction and execution process between the modules is based on the same concept as the method embodiment of the present application, specific functions and technical effects thereof may be referred to in the method embodiment section, and details are not repeated herein.
Fig. 4 is a schematic structural diagram of a computer device according to a fourth embodiment of the present application. As shown in fig. 4, the computer device of this embodiment includes: at least one processor (only one shown in fig. 4), a memory, and a computer program stored in the memory and executable on the at least one processor, the processor, when executing the computer program, performing the steps of the template matching method embodiments of any of the respective images described above.
The computer device may include, but is not limited to, a processor, a memory. It will be appreciated by those skilled in the art that fig. 4 is merely an example of a computer device and is not intended to limit the computer device, and that a computer device may include more or fewer components than shown, or may combine certain components, or different components, such as may also include a network interface, a display screen, an input device, and the like.
The processor may be a CPU, but may also be other general purpose processors, digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), off-the-shelf programmable gate arrays (Field-Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
The memory includes a readable storage medium, an internal memory, etc., where the internal memory may be the memory of the computer device, the internal memory providing an environment for the execution of an operating system and computer-readable instructions in the readable storage medium. The readable storage medium may be a hard disk of a computer device, and in other embodiments may be an external storage device of the computer device, for example, a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash memory Card (Flash Card), etc. that are provided on the computer device. Further, the memory may also include both internal storage units and external storage devices of the computer device. The memory is used to store an operating system, application programs, boot loader (BootLoader), data, and other programs such as program codes of computer programs, and the like. The memory may also be used to temporarily store data that has been output or is to be output.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-described division of the functional units and modules is illustrated, and in practical application, the above-described functional distribution may be performed by different functional units and modules according to needs, i.e. the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-described functions. The functional units and modules in the embodiment may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit, where the integrated units may be implemented in a form of hardware or a form of a software functional unit. In addition, specific names of the functional units and modules are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the present application. The specific working process of the units and modules in the above device may refer to the corresponding process in the foregoing method embodiment, which is not described herein again. The integrated units, if implemented in the form of software functional units and sold or used as stand-alone products, may be stored in a computer readable storage medium. Based on such understanding, the present application implements all or part of the flow of the method of the above-described embodiments, and may be implemented by a computer program to instruct related hardware, where the computer program may be stored in a computer readable storage medium, where the computer program, when executed by a processor, may implement the steps of the method embodiments described above. Wherein the computer program comprises computer program code, which may be in the form of source code, object code, executable files or in some intermediate form, etc. The computer readable medium may include at least: any entity or device capable of carrying computer program code, a recording medium, a computer Memory, a Read-Only Memory (ROM), a random access Memory (Random Access Memory, RAM), an electrical carrier signal, a telecommunications signal, and a software distribution medium. Such as a U-disk, removable hard disk, magnetic or optical disk, etc. In some jurisdictions, computer readable media may not be electrical carrier signals and telecommunications signals in accordance with legislation and patent practice.
The present application implementing all or part of the flow of the method of the above embodiment may also be implemented by a computer program product, which when run on a computer device causes the computer device to execute the steps of the method embodiment described above.
In the foregoing embodiments, the descriptions of the embodiments are emphasized, and in part, not described or illustrated in any particular embodiment, reference is made to the related descriptions of other embodiments.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
In the embodiments provided in this application, it should be understood that the disclosed apparatus/computer device and method may be implemented in other ways. For example, the apparatus/computer device embodiments described above are merely illustrative, e.g., the division of modules or units is merely a logical functional division, and there may be additional divisions when actually implemented, e.g., multiple units or components may be combined or integrated into another system, or some features may be omitted or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection via interfaces, devices or units, which may be in electrical, mechanical or other forms.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed over a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
The above embodiments are only for illustrating the technical solution of the present application, and are not limiting thereof; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present application, and are intended to be included in the scope of the present application.