Template matching method, system, device and medium based on template edge score
By using a template edge scoring method, the coordinates of edge pixels in the template image are determined and transformed to generate an edge image for matching. This solves the accuracy and efficiency problems of existing template matching under conditions of large grayscale value differences and environmental changes, and achieves efficient template matching.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-02
- Publication Date
- 2026-03-27
AI Technical Summary
Existing template matching methods perform poorly when the target rotates, changes size, changes grayscale value, or changes ambient light, and have low matching efficiency, making them ineffective for application scenarios with large differences in grayscale values.
By using a scoring method based on template image edges, the pixel coordinates of the middle edge, inner edge, and outer edge are determined, size transformation and matching are performed to generate an edge image, and the matching result is obtained through nested loop scoring.
It improves matching accuracy and generalization ability in application scenarios with large differences in grayscale values, while also increasing matching efficiency.
Smart Images

Figure CN115410015B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of template matching, in particular to a template matching method and system based on template edge scoring, a computer device and a storage medium. BACKGROUND
[0002] Template matching is a commonly used pattern recognition method, which is used to analyze and identify the specific location of the pattern of a specific object in a detection image, and further identify the object matching problem solving method of the specific object, and is widely used.
[0003] However, the existing template matching has its own application limitations: (1) it can only move in parallel, and when the matching target in the original image rotates, changes in size, or there is damage or change in the gray value in the matching target, the matching will fail, which will seriously affect the matching effect; (2) all pixel points in the matching template must be matched during the matching process, resulting in a large number of pixel points required for matching and low matching efficiency; (3) Because the gray value in the matching template is fixed, it cannot effectively deal with the problem of changes in the gray value of the matching target caused by changes in the brightness of the environment light, and the matching result is greatly affected by the environment light.
[0004] Therefore, it is necessary to provide a template matching method that can effectively improve the matching accuracy of application scenarios where the gray value difference between the target foreground and the background is large, and can enhance the generalization ability and improve the matching efficiency. SUMMARY
[0005] The purpose of the present application is to provide a template matching method based on template edge scoring, which solves the application defects of the existing template matching method by scoring the inside and outside of the edge of the template image. It can simply and efficiently match the template for target outlines of various shapes, not only can effectively improve the matching accuracy of application scenarios where the gray value difference between the foreground and the background is large, but also can enhance the generalization ability of template matching, and can improve the matching efficiency while ensuring the matching accuracy.
[0006] In order to achieve the above purpose, it is necessary to provide a template matching method based on template edge scoring, a system, a computer device and a storage medium in order to solve the above technical problems.
[0007] In the first aspect, the present application provides a template matching method based on template edge scoring, which comprises the following steps:
[0008] According to the detection image, a template image is made, and the middle edge, inner edge and outer edge of the template image are determined;
[0009] Obtaining pixel point coordinates corresponding to the middle edge, the inner edge and the outer edge of the template image respectively, and drawing corresponding middle edge lines, inner edge lines and outer edge lines;
[0010] According to the size of the to-be-detected image, performing size transformation on the middle edge lines, the inner edge lines and the outer edge lines respectively, and generating an edge image according to the transformed middle edge lines, the inner edge lines and the outer edge lines;
[0011] According to the edge image, performing template matching on the to-be-detected image to obtain a matching result.
[0012] Further, the step of preparing a template image from a to-be-detected image comprises:
[0013] Identifying a region of interest of the to-be-detected image, and obtaining the template image according to the outer edge of the region of interest.
[0014] Further, the step of determining the middle edge, the inner edge and the outer edge of the template image comprises:
[0015] Obtaining each corner point coordinate of the template image, and obtaining the middle edge of the template image according to each corner point coordinate;
[0016] Obtaining the inner edge and the outer edge of the template image by performing dilation and erosion processing on the template image.
[0017] Further, the step of obtaining the inner edge and the outer edge of the template image by performing dilation and erosion processing on the template image comprises:
[0018] If the template image is a black background image, the outer edge is obtained by dilation processing, and the inner edge is obtained by erosion dilation processing, otherwise, the inner edge is obtained by dilation processing, and the outer edge is obtained by erosion dilation processing.
[0019] Further, the step of performing size transformation on the middle edge lines, the inner edge lines and the outer edge lines according to the size of the to-be-detected image, and generating an edge image according to the transformed middle edge lines, the inner edge lines and the outer edge lines comprises:
[0020] Obtaining the outer edge width and the outer edge height of the outer edge line respectively, and after translating the outer edge line to a first preset position, performing proportional transformation according to a first proportional transformation formula according to the size of the to-be-detected image; the first preset position is that the left upper corner point coordinate of the outer edge line is the origin, and the right lower corner point coordinate is (the outer edge width, the outer edge height);
[0021] The first proportional transformation formula is:
[0022]
[0023] wherein (x out ,y out ) and represent the coordinates of any point on the outer edge line before and after the scaling transformation respectively; m and n represent the width and height of the image to be detected respectively; m out and n out represent the width and height of the outer edge line respectively;
[0024] The inner edge width and the inner edge height of the inner edge line are obtained respectively, and after the inner edge line is translated to a second preset position, the inner edge line is scaled according to a second scaling formula according to the size of the image to be detected, and the scaled inner edge line is translated according to a first translation formula; the second preset position is that the left upper corner point coordinate of the inner edge line is the origin, and the right lower corner point coordinate is (inner edge width, inner edge height); the second scaling formula is:
[0025]
[0026] wherein (x inner ,y inner ) and represent the coordinates of any point on the outer edge line before and after the scaling transformation respectively;
[0027] The first translation formula is:
[0028]
[0029] wherein (x inner,tran ,y inner,tran ) and represent the coordinates of any point on the outer edge line before and after the scaling transformation respectively; (a inner ,b inner ) and (a out ,b out ) represent the left upper corner point coordinates of the inner edge line and the outer edge line respectively before the size transformation;
[0030] The middle edge width and the middle edge height of the middle edge line are obtained respectively, and after the middle edge line is translated to a third preset position, the middle edge line is scaled according to a third scaling formula according to the size of the image to be detected, and the scaled middle edge line is translated according to a second translation formula; the third preset position is that the left upper corner point coordinate of the middle edge line is the origin, and the right lower corner point coordinate is (middle edge width, middle edge height); the third scaling formula is:
[0031]
[0032] wherein (x mid ,y mid ) and represent the coordinates of any point on the outer edge line before and after the scaling transformation, respectively;
[0033] The second translation formula is:
[0034]
[0035] wherein (x mid,tran ,y mid,tran ) and represent the coordinates of any point on the inner edge line before and after the scaling transformation, respectively; (a mid ,b mid ) represents the coordinates of the top-left corner point of the middle edge line before the size transformation;
[0036] The outer edge line, the middle edge line and the inner edge line are gathered into a blank image with the same size as the image to be detected, to obtain the edge image.
[0037] Further, the step of performing template matching on the image to be detected according to the edge image to obtain a matching result comprises:
[0038] According to a preset coordinate change step and a scaling transformation step, the inner edge line and the outer edge line in the edge image are subjected to nested loop changes, and at each nested loop step, a corresponding loop matching score is counted according to the pixel grayscale values of the inner edge line and the outer edge line, and the highest matching score is updated and the corresponding outer edge line change information is recorded according to the loop matching score; the outer edge change information includes the initial value of the top-left corner point coordinates of the outer edge line, the template x-axis change ratio and the template y-axis change ratio;
[0039] According to the highest matching score, the corresponding outer edge line change information is obtained, and the middle edge line of the edge image is subjected to corresponding transformation according to the outer edge line change information, to obtain a matching edge; the transformation formula of the middle edge line is:
[0040]
[0041] wherein, and represent the coordinates of the middle edge line before and after the transformation according to the outer edge change information corresponding to the highest matching score; k x and k y represent the template x-axis change ratio and the template y-axis change ratio corresponding to the highest matching score, respectively; (a s,out ,b s,out ) represents the top-left corner point coordinates corresponding to the highest matching score.
[0042] According to the matching edge, a matching result of the image to be detected is obtained.
[0043] Further, the step of counting the corresponding loop matching score according to the pixel gray value in each nested loop step comprises:
[0044] According to the pixel point coordinates of the corresponding outer edge line in each nested loop step, the corresponding gray value on the image to be detected is obtained, and an outer edge score is counted and obtained; the outer edge score is represented as:
[0045] S l,out =g l,out / (255·num l,out )
[0046] Wherein, S l,out , g l,out and num l,out represent the outer edge score, the outer edge gray value and the outer edge point number of the lth nested loop;
[0047] According to the pixel point coordinates of the corresponding inner edge line in each nested loop step, the corresponding gray value on the image to be detected is obtained, and an inner edge score is counted and obtained; the outer edge score is represented as:
[0048] S l,inner =g l,inner / (255·num l,inner )
[0049] Wherein, S l,inner , g l,inner and num l,inner represent the inner edge score, the inner edge gray value and the inner edge point number of the lth nested loop;
[0050] According to the outer edge score and the inner edge score, a loop matching score is obtained; the loop matching score is represented as:
[0051] S l =|S l,inner -S l,out |
[0052] Wherein, S l represents the loop matching score of the lth nested loop.
[0053] In a second aspect, the embodiment of the present application provides a template matching system based on template edge scoring, which comprises:
[0054] A template making module is used for making a template image according to an image to be detected, and determining the middle edge, the inner edge and the outer edge of the template image.
[0055] an edge line acquisition module, configured to acquire pixel point coordinates corresponding to a middle edge, an inner edge and an outer edge of the template image respectively, and draw corresponding middle edge lines, inner edge lines and outer edge lines;
[0056] an edge map generation module, configured to perform size transformation on the middle edge lines, the inner edge lines and the outer edge lines respectively according to a size of the to-be-detected image, and generate an edge image according to the transformed middle edge lines, the inner edge lines and the outer edge lines;
[0057] an edge matching module, configured to perform template matching on the to-be-detected image according to the edge image, and obtain a matching result.
[0058] In a third aspect, an embodiment of the present application further provides a computer device, including a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the above method when executing the computer program.
[0059] In a fourth aspect, an embodiment of the present application further provides a computer readable storage medium, which stores a computer program, and the computer program is executable on a processor to implement the steps of the above method.
[0060] The above application provides a template matching method, system, computer device and storage medium based on template edge scoring. Through the method, a template image is made according to a to-be-detected image, and a middle edge, an inner edge and an outer edge of the template image are determined. Pixel point coordinates corresponding to the middle edge, the inner edge and the outer edge are acquired respectively, and corresponding middle edge lines, inner edge lines and outer edge lines are drawn. Then, size transformation is performed on the middle edge lines, the inner edge lines and the outer edge lines according to a size of the to-be-detected image. An edge image is generated according to the transformed middle edge lines, the inner edge lines and the outer edge lines. Template matching is performed on the to-be-detected image according to the edge image, and a matching result is obtained. Compared with the prior art, the template matching method based on template edge scoring can simply and efficiently perform template matching on target contours of various shapes. The matching precision in an application scene where the foreground and background gray values differ greatly can be effectively improved. The generalization ability of template matching can be enhanced. The matching efficiency can be improved while ensuring the matching precision. BRIEF DESCRIPTION OF DRAWINGS
[0061] Figure 1 is an application scenario diagram of the template matching method based on template edge scoring in the embodiment of the present application;
[0062] Figure 2 is a flow diagram of the template matching method based on template edge scoring in the embodiment of the present application;
[0063] Figure 3 is a schematic diagram of an image to be detected in an embodiment of the present application;
[0064] Figure 4 is Figure 3 is a schematic diagram of a template image of an image to be detected in the embodiment of the present application;
[0065] Figure 5 is Figure 4 is a schematic diagram of an inner edge, a middle edge and an outer edge of a template image in the embodiment of the present application;
[0066] Figure 6 is a schematic diagram of an edge image generated in the embodiment of the present application;
[0067] Figure 7 is a schematic diagram of initial matching of an upper left corner point of an outer edge line as an initial point being an origin in the embodiment of the present application;
[0068] Figure 8 is a schematic diagram of initial matching of an upper left corner point of an outer edge line as an initial point not being an origin in the embodiment of the present application;
[0069] Figure 9 is a schematic diagram of a matching result of template matching of an image to be detected according to Figure 6 an edge image; Figure 3
[0070] Figure 10 is a schematic diagram of a matching result of another template matching in the embodiment of the present application;
[0071] Figure 11 is a schematic diagram of a structure of a template matching system based on template edge scoring in the embodiment of the present application;
[0072] Figure 12 is an internal structure diagram of a computer device in the embodiment of the present application. DETAILED DESCRIPTION
[0073] In order to make the objectives, technical solutions and beneficial effects of the present application clearer, further detailed description will be made to the present application in combination with the accompanying drawings and embodiments. Obviously, the following described embodiments are only a part of the embodiments of the present application, and are used to explain the present application, but not to limit the scope of the present application. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor, belong to the scope of protection of the present application.
[0074] The template matching method based on template edge scoring provided by the present application can be applied to, for example, Figure 1 The terminal can be, but is not limited to, various personal computers, notebook computers, smart phones, tablet computers and portable wearable devices, and the server can be implemented by an independent server or a server cluster composed of multiple servers. The server can implement matching recognition of a specific shape target image based on the template matching method based on template edge scoring of the application, and the matching recognition result can be used for subsequent application services of the server or fed back to a terminal user for viewing and use. The following embodiments will describe in detail a template matching method based on template edge scoring of the application.
[0075] In one embodiment, as shown in Figure 2 a template matching method based on template edge scoring is provided, comprising the following steps:
[0076] S11, a template image is made according to a to-be-detected image, and a middle edge, an inner edge and an outer edge of the template image are determined; wherein the template image can be understood as an image generated according to an edge of a detection target in the to-be-detected image, for example, as shown in Figure 3 the outer edge of the white area in the to-be-detected image, a template image with a white foreground and a black background as shown in Figure 4 is obtained; specifically, the step of making a template image according to a to-be-detected image comprises:
[0077] An area of interest of the to-be-detected image is identified, and the template image is obtained according to an outer edge of the area of interest; wherein the area of interest can be obtained by using a deep learning image processing method or an image pyramid method, and is not specifically limited here;
[0078] After the template image is obtained by the above method, the middle edge, the inner edge and the outer edge used for template matching of the application can be obtained according to the edge of the template image itself and the edge obtained by dilating and eroding the template image; specifically, the step of determining the middle edge, the inner edge and the outer edge of the template image comprises:
[0079] Each corner point coordinate of the template image is obtained, and the middle edge of the template image is obtained according to each corner point coordinate; wherein each corner point coordinate of the template image can be obtained by using the find_contours function of the opencv library, and the corresponding edge can also be drawn by using the draw_contours function according to the obtained corner point coordinate;
[0080] The inner edge and the outer edge of the template image are obtained by performing dilation and erosion on the template image; wherein the corresponding relationship between the inner edge and the outer edge and the dilation and erosion processing is related to the foreground and background gray values of the template image; specifically, the step of obtaining the inner edge and the outer edge of the template image by performing dilation and erosion on the template image comprises:
[0081] If the template image is a black background image, the outer edge is obtained by dilation processing, and the inner edge is obtained by erosion dilation processing, otherwise, the inner edge is obtained by dilation processing, and the outer edge is obtained by erosion dilation processing; that is, for the black background template shown in Figure 4 The outer edge (white line), the inner edge (black line) and the middle edge (the edge of the white foreground area in the template image) located between the outer edge and the inner edge can be obtained as shown in Figure 5
[0082] The method of the present application matches the gray values corresponding to all pixel point coordinates of the edge, therefore, the corner point coordinates of the obtained inner edge, middle edge and outer edge are used to extend and record point by point to obtain all pixel point coordinates of the inner edge, middle edge and outer edge.
[0083] S12, the pixel point coordinates corresponding to the middle edge, the inner edge and the outer edge of the template image are obtained respectively, and the corresponding middle edge line, inner edge line and outer edge line are drawn; wherein the pixel point coordinates corresponding to the middle edge, the inner edge and the outer edge are obtained in the same way, and the following will be replaced by edge instead of middle edge, inner edge and outer edge to explain the obtaining step in detail:
[0084] 1) In order to make the draw_contours function draw the edge according to all pixel points consistent with the edge drawn according to the corner point coordinates, an empty vector with the same format as the vector storing the corner point coordinates of the edge is created to store all pixel point coordinates of the edge, which will be described as a new vector in the following;
[0085] 2) using for loop to get each pixel coordinate of each pair of adjacent corner points one by one, and record all the pixel coordinates between each pair of adjacent corner points to a new vector after getting all the pixel coordinates; for example, get (1, 1) to (1, 10) (these two points are only used to illustrate how to loop, not represent that these two points are corner points in the above template image), then get (1, 1), (1, 2), (1, 3),..., (1, 10) in turn; it should be noted that the edge corner points obtained by using the find_contours function for the first time will not have adjacent corner points with large slant edges, that is, (2, 2), (10, 10) will not appear directly, and according to the function mechanism, (2, 2), (3, 3), (4, 4),..., (10, 10) are finally obtained; therefore, even if there are slant edges, circular arcs, etc., all pixel coordinates can be correctly obtained;
[0086] 3) after step 2) is executed, the vector recording all the pixel points of the edge is obtained, and the edge drawn by using the draw_contours function is consistent with the edge drawn by using the corner points; all the steps below use the new vector recording all the pixel coordinates of the edge and the edge line drawn by using the new vector.
[0087] The above method obtains the middle edge line, the inner edge line and the outer edge line, that is, the following method is used to generate an edge image that can be used for edge matching by using size transformation and translation transformation.
[0088] S13, according to the size of the to-be-detected image, respectively performing size transformation on the middle edge line, the inner edge line and the outer edge line, and generating an edge image according to the transformed middle edge line, inner edge line and outer edge line; wherein the size transformation of the middle edge line and the inner edge line is consistent with the size transformation of the outer edge line, which effectively ensures the effectiveness of the edge image; specifically, the step of performing size transformation on the middle edge line, the inner edge line and the outer edge line according to the size of the to-be-detected image, and generating an edge image according to the transformed middle edge line, inner edge line and outer edge line comprises:
[0089] respectively obtaining the outer edge width and the outer edge height of the outer edge line, and translating the outer edge line to a first preset position, and then performing proportional transformation according to a first proportional transformation formula according to the size of the to-be-detected image; the first preset position is that the top left corner point coordinate of the outer edge line is the origin, and the bottom right corner point coordinate is (outer edge width, outer edge height);
[0090] The first proportional transformation formula is:
[0091]
[0092] wherein, (xout ,y out ) and respectively represent the pre-proportional transformation coordinates and the post-proportional transformation coordinates of any point on the outer edge line; m and n respectively represent the width and the height of the image to be detected; m out and n out respectively represent the width and the height of the outer edge line;
[0093] The process of size transformation of the outer edge line can be understood as follows: 1) obtaining the minimum value of x and the minimum value of y in all point coordinates of the outer edge line as the left upper corner point coordinates of the outer edge, and obtaining the maximum value of x and the maximum value of y in all point coordinates of the outer edge line as the right lower corner point coordinates of the outer edge; 2) subtracting the x coordinate value of the left upper corner point coordinates from the x coordinate value of the right lower corner point coordinates of the outer edge to obtain the width of the outer edge, and subtracting the y coordinate value of the left upper corner point coordinates from the y coordinate value of the right lower corner point coordinates to obtain the height of the outer edge; 3) performing translation on the outer edge line: (x coordinate value of all points of the outer edge - x coordinate value of the left upper corner point coordinates of the outer edge), (y coordinate value of all points of the outer edge - y coordinate value of the left upper corner point coordinates of the outer edge), so that the left upper corner point coordinates of the outer edge become the origin (0, 0) and the right lower corner point coordinates become (the width of the outer edge, the height of the outer edge); 4) performing proportional transformation on the translated outer edge line: x coordinate value of all points of the outer edge * (the width of the image to be detected / the width of the outer edge), y coordinate value of all points of the outer edge * (the height of the image to be detected / the height of the outer edge);
[0094] respectively obtaining the inner edge width and the inner edge height of the inner edge line, and performing proportional transformation according to a second proportional transformation formula after the inner edge line is translated to a second preset position according to the size of the image to be detected, and performing translation on the transformed inner edge line according to a first translation formula; the second preset position is that the left upper corner point coordinates of the inner edge line are the origin and the right lower corner point coordinates are (the inner edge width, the inner edge height); the second proportional transformation formula is:
[0095]
[0096] wherein, (x inner ,y inner ) and respectively represent the pre-proportional transformation coordinates and the post-proportional transformation coordinates of any point on the outer edge line;
[0097] The first translation formula is:
[0098]
[0099] wherein, (x inner,tran ,y inner,tran ) and respectively represent the pre-translation coordinates and the post-translation coordinates of any point on the inner edge line after the proportional transformation; (a inner ,b inner ) and (a out ,b out ) respectively represent the left-top corner point coordinates of the inner edge line and the outer edge line before the size transformation;
[0100] The size transformation process of the inner edge line can be understood as follows: first, repeat steps 1) and 2) of the size transformation of the outer edge line to obtain the inner edge width and the inner edge height respectively; then, repeat step 3) to translate the left-top corner point of the inner edge to the origin and the right-bottom corner point to the corresponding (inner edge width, inner edge height); then, make a proportional transformation of the inner edge line in the same proportion as the outer edge line: multiply the x coordinate value of all points of the inner edge line by (width of the image to be detected / outer edge width) and multiply the y coordinate value of all points of the inner edge by (height of the image to be detected / outer edge height); finally, make a translation of all pixel points of the inner edge line so that the inner edge line is in the middle of the outer edge line, and the translation value of the x coordinate of all points of the inner edge line is (x coordinate value of the left-top corner point of the inner edge line of the template image-x coordinate value of the left-top corner point of the outer edge line of the template image)*(height of the image to be detected / outer edge height), and the translation value of the y coordinate of all points of the inner edge line is (y coordinate value of the left-top corner point of the inner edge line of the template image-y coordinate value of the left-top corner point of the outer edge line of the template image)*(height of the image to be detected / outer edge height);
[0101] After obtaining the middle edge width and the middle edge height of the middle edge line respectively and translating the middle edge line to a third preset position, according to the size of the image to be detected, a third proportional transformation formula is used for proportional transformation, and a second translation formula is used for translation of the transformed middle edge line; the third preset position is that the left-top corner point coordinate of the middle edge line is the origin and the right-bottom corner point coordinate is (middle edge width, middle edge height); the third proportional transformation formula is:
[0102]
[0103] wherein (x mid ,y mid ) and represent the pre-proportional transformation coordinates and the post-proportional transformation coordinates of any point on the outer edge line respectively;
[0104] The second translation formula is:
[0105]
[0106] wherein (x mid,tran ,y mid,tran ) and respectively represent the pre-translation coordinates and post-translation coordinates of any point on the inner edge line after the proportional transformation; (a mid ,b mid ) represents the left-top corner point coordinate of the middle edge line before the size transformation;
[0107] The size transformation process of the above middle edge line can be understood as follows: first, repeat steps 1) and 2) of the size transformation of the outer edge line to obtain the middle edge width and the middle edge height respectively; then, repeat step 3) to translate the left-top corner point of the middle edge to the origin and the right-bottom corner point to the corresponding (middle edge width, middle edge height); then, make a proportional transformation of the middle edge line with the same proportion as the outer edge line: the x coordinate value of all points of the middle edge line*(width of the image to be detected / outer edge width), the y coordinate value of all points of the middle edge*(height of the image to be detected / outer edge height); finally, make a translation change to all pixel points of the middle edge line so that the middle edge line is in the middle of the outer edge line, the translation value of the x coordinate of all pixel points of the middle edge line=(x coordinate value of the left-top corner point of the middle edge line in the template image-x coordinate value of the left-top corner point of the outer edge line in the template image)*(height of the image to be detected / outer edge height), the translation value of the y coordinate of all points of the middle edge line=(y coordinate value of the left-top corner point of the middle edge line in the template image-y coordinate value of the left-top corner point of the outer edge line in the template image)*(height of the image to be detected / outer edge height);
[0108] The outer edge line, the middle edge line and the inner edge line are gathered to a blank image with the same size as the image to be detected to obtain the edge image; as shown in the figure, the three white closed lines in the edge image correspond to the inner edge line, the middle edge line and the outer edge line from inside to outside, and the left-top corner point coordinate of the outer edge line is (0, 0), and the size of the edge image is equal to the size of the image to be detected. Figure 6
[0109] S14, template matching is performed on the to-be-detected image according to the edge image, and a matching result is obtained; wherein, the template matching process is: based on the coordinates of each pixel point of the inner and outer edge lines in the edge image, the parameters (including: initial value of the coordinates of the top-left corner point of the outer edge line, maximum value of the x-coordinate value of the top-left corner point of the outer edge line, x-coordinate change step, maximum value of the y-coordinate value of the top-left corner point of the outer edge line, y-coordinate change step, maximum and minimum values of the x-axis proportion change of the template, x-axis proportion change step, maximum and minimum values of the y-axis proportion change of the template, and y-axis proportion change step) are changed according to the self-set parameters; four double-type variables with initial values of 0 are set respectively, wherein two variables record the gray value accumulations of the inner and outer edge line coordinates of the to-be-detected image, and the remaining two variables are respectively used to count the pixel points of the inner and outer edge lines; then, three variables with initial values of 0 are set to record the scores of the inner and outer edge lines and the loop matching score; then, the parameters are changed and matched in a loop according to the self-set parameters, until the highest matching score is obtained after the loop ends, the middle edge line is changed in consistency according to the same translation and proportion change of the inner and outer edge lines corresponding to the highest matching score, and the edge of the matched target image is obtained.
[0110] When the coordinates of the top-left corner point of the outer edge line in the edge image used for template matching are the origin (0, 0), the initial matching position relationship of the to-be-detected image is as shown in Figure 7 The outer edge line is just on the edge of the to-be-detected image, and for the initial coordinates (30, 20) of the outer edge line as shown in Figure 8 When the initial coordinates (30, 20) of the outer edge line are not the origin, the right lower part of the inner and outer edge lines of the edge image may exceed the range of the matching image, and the out-of-boundary problem occurs, at this time, the following transformation formula is used for proportion transformation to make the coordinates of the top-left corner point of the outer edge line of the edge image be the initial coordinates set by the user, and the coordinates of the right lower corner point be the coordinate value of the right lower corner of the to-be-detected image (all the outer edge lines below represent the outer edge line with the initial coordinates of the top-left corner point at the origin):
[0111] The x-coordinate value of each pixel point of the outer edge line = [(x-coordinate value of each pixel point of the outer edge line / width of the to-be-detected image) * (width of the to-be-detected image - initial x-coordinate value set by the user) + initial x-coordinate value set by the user];
[0112] The y-coordinate value of each pixel point of the outer edge line = [(y-coordinate value of each pixel point of the outer edge line / height of the to-be-detected image) * (height of the to-be-detected image - initial y-coordinate value set by the user) + initial y-coordinate value set by the user];
[0113] The inner edge line changes in the same way and uses the same parameters as the outer edge line. You only need to replace the x and y coordinates of each pixel of the outer edge line in the above formula with the x and y coordinates of each pixel of the inner edge line. The outer edge line obtained is called the initial point adaptive outer edge line, and the inner edge line obtained is called the initial point adaptive inner edge line.
[0114] After ensuring that the coordinates of the upper left corner of the outer edge line of the edge image are at the origin using the above method, nested loop matching can be performed; specifically, the step of performing template matching on the image to be detected based on the edge image to obtain the matching result includes:
[0115] Based on preset coordinate change step size and scale transformation step size, nested cyclic changes are performed on the inner and outer edge lines in the edge image. At each nested cyclic step, a corresponding cyclic matching score is calculated based on the pixel grayscale values of the inner and outer edge lines. The highest matching score is updated based on the cyclic matching score, and the corresponding outer edge line change information is recorded. The outer edge change information includes the initial coordinates of the upper left corner of the outer edge line, the template x-axis change scale, and the template y-axis change scale. The coordinate change step size includes the x-axis change step size and y-axis change step size of the outer edge line. The scale transformation step size includes the x-axis transformation scale step size and y-axis transformation scale step size of the template image. Specifically, the step of calculating the corresponding cyclic matching score based on pixel grayscale values at each nested cyclic step includes:
[0116] Based on the pixel coordinates of each point on the outer edge line corresponding to each nested loop step, the corresponding grayscale value on the image to be detected is obtained, and the outer edge score is calculated; the outer edge score is expressed as:
[0117] S l,out =g l,out / (255·num l,out )
[0118] Among them, S l,out g l,out and num l,out This represents the outer edge score, the cumulative gray value of the outer edge, and the number of outer edge points in the l-th nested loop;
[0119] Based on the pixel coordinates of each inner edge line corresponding to each nested loop step, the corresponding grayscale value on the image to be detected is obtained, and the inner edge score is calculated; the outer edge score is represented as:
[0120] S l,inner =g l,inner / (255·num l,inner )
[0121] Among them, Sl,inner , g l,inner and num l,inner respectively represent the inner edge score, the inner edge gray value accumulation and the inner edge point number of the lth nested loop;
[0122] According to the outer edge score and the inner edge score, the cycle matching score is obtained; the cycle matching score is represented as:
[0123] S l = |S l,inner -S l,out |
[0124] Wherein, S l represents the cycle matching score of the lth nested loop, the minimum value is 0, and the maximum value is 1; the greater the difference between the average gray value of the inner edge line and the outer edge line, the higher the cycle matching score, and the higher the possibility of the middle edge line being the target edge;
[0125] According to the highest matching score, the corresponding outer edge line change information is obtained, and the middle edge line of the edge image is transformed according to the outer edge line change information, so that the matching edge is obtained; the transformation formula of the middle edge line is:
[0126]
[0127] Wherein, and respectively represent the coordinates of the middle edge line before and after the transformation according to the outer edge change information corresponding to the highest matching score; k x and k y respectively represent the template x-axis change ratio and the template y-axis change ratio corresponding to the highest matching score; (a s,out ,b s,out ) represents the top-left corner point coordinates corresponding to the highest matching score;
[0128] According to the matching edge, the matching result of the image to be detected is obtained;
[0129] The above nested loop matching process can be understood as:
[0130] 1) According to the coordinates of all pixel points of the initial inner and outer edge lines on the edge image, the corresponding gray value of each coordinate is directly obtained on the image to be detected and accumulated in the set variable; for example, when the gray value of each pixel point corresponding to the coordinate of the inner edge line is obtained using a for loop, the total number of inner edge pixel points is increased by 1 and the total value of the inner edge gray value is added to the obtained pixel gray value (for example, assuming that the gray value of the starting coordinate (2, 2) of the outer edge is 10 and the gray value of the next coordinate (2, 3) is 20, the gray value of the starting coordinate is first obtained, then the total number of inner edge pixel points is increased by 1 to 1 and the total value of the inner edge gray value is increased by 10 to 10; then the next coordinate is entered using a loop, the total number of inner edge pixel points is increased by 1 to 2 and the total value of the inner edge gray value is increased by 20 to 30; the rule is followed to record the gray value and the number of pixel points corresponding to all pixel points on the inner edge line); the steps of the outer edge are the same, which will not be described here. After the inner and outer edge lines are recorded, the final score of each nested loop can be obtained according to the above-mentioned outer edge score, inner edge score and loop matching score calculation formula, if the calculated score is higher than the existing highest score (the initial value of the final score is 0, this time is the first time to obtain the score, so the existing highest score is 0), the latest score, the latest top-left corner point coordinate and the latest template change ratio (the ratio is greater than 0 and the maximum is 1 to prevent the template edge coordinate from exceeding the range of the matching image) are recorded; after each edge change scoring, the current highest score is compared, and as long as a new highest score is obtained, the record is immediately updated, and then the next edge change scoring is performed;
[0131] 2) According to the step, the change variable (including: the x coordinate value of the leftmost top corner point of the outer edge line, the y coordinate value of the leftmost top corner point of the outer edge line, the template x axis change ratio, the template y axis change ratio) is changed in a nested loop: for example, the template x axis change ratio is changed and scored first, the x axis change ratio is repeatedly changed and scored according to the step until the limit is reached, then the y axis change ratio is changed and the x change ratio is set back to the initial value, then the x change ratio change and scoring are continued as described above, and similarly when the y axis change ratio reaches the limit, the x coordinate value of the leftmost top corner point of the outer edge is changed and the y axis change ratio and the x axis change ratio are set back to the initial value, then the y axis change ratio and the x axis change ratio are adjusted and scored as described above; the outer edge change formula in the corresponding nested loop is:
[0132] The x coordinate value of each pixel point of the outer edge line = (the x coordinate value of each pixel point of the outer edge line / the width of the image to be detected) * (the width of the image to be detected - the x coordinate value of the leftmost top starting point of the outer edge line) * the x axis change ratio + the x coordinate value of the leftmost top starting point of the outer edge line;
[0133] The y coordinate value of each pixel point of the outer edge = (the y coordinate value of each pixel point of the outer edge line / the height of the image to be detected) * (the height of the image to be detected-the y coordinate value of the starting point of the upper left corner of the outer edge line) * the y axis change ratio + the y coordinate value of the starting point of the upper left corner of the outer edge;
[0134] The change of the inner edge line change formula is consistent with the change of the outer edge line change formula. According to the outer edge line change formula, the inner edge line change formula can be obtained by replacing the x coordinate value and the y coordinate value of each pixel point of the outer edge line change formula with the x coordinate value and the y coordinate value of each pixel point of the inner edge line change formula.
[0135] It should be noted that the essence of the above inner and outer edge line change formula is to perform multi-scale transformation on the initial outer edge line and the initial inner edge line corresponding to the obtained initial point adaptive change. The score is evaluated once in each loop, and the score is compared with the current highest score. If it is greater than the current highest score, the outer edge change information (including: the initial point coordinate of the left upper corner of the outer edge, the x axis change ratio and the y axis change ratio) is recorded:
[0136] After the multi-layer nested loop is executed, the highest matching score and the recorded outer edge change information when the highest matching score is obtained are obtained, and the middle edge line of the edge image is changed according to the outer edge change information when the highest matching score is obtained. The change mode and parameters are consistent with the change of the inner and outer edge lines when the highest matching score is obtained, that is, the middle edge line change formula can be obtained according to the inner and outer edge line change formula:
[0137] The x coordinate value of each pixel point of the middle edge = (the x coordinate value of each pixel point of the middle edge / the width of the image to be detected) * (the width of the image to be detected-the x coordinate value of the starting point of the upper left corner of the outer edge line corresponding to the highest matching score) * the x axis change ratio corresponding to the highest matching score + the x coordinate value of the starting point of the upper left corner of the outer edge corresponding to the highest matching score;
[0138] The y coordinate value of each pixel point of the middle edge = (the y coordinate value of each pixel point of the middle edge / the height of the image to be detected) * (the height of the image to be detected-the y coordinate value of the starting point of the upper left corner of the outer edge line corresponding to the highest matching score) * the y axis change ratio corresponding to the highest matching score + the y coordinate value of the starting point of the upper left corner of the outer edge corresponding to the highest matching score.
[0139] The final matching result is as follows: Figure 9As shown, the to-be-detected target is in the region selected by the gray line frame, and accurate matching is achieved. The corresponding parameters are: the initial coordinates of the left upper corner of the outer edge line = (0, 0), the maximum value of the x coordinate value = the width of the to-be-detected image / 2, the maximum value of the y coordinate value = the height of the to-be-detected image / 2, the x coordinate value change step = 2, the y coordinate value change step = 2, the maximum value of the change ratio of the x axis and the y axis = 1, the minimum value of the ratio = 0.8, and the ratio change step = 0.05. Similarly, the real object image detection process is as shown in Figure 10 As shown, it is further verified that the method of the application can achieve accurate matching of various regular or irregular to-be-detected targets.
[0140] The embodiment of the application can generate an edge image based on a template image, perform traversal matching scoring on a to-be-detected image based on a preset nested loop rule of the inner and outer edge lines of the edge image, and obtain a middle edge line of the edge image corresponding to the highest matching score as the edge of the to-be-recognized image to obtain a matching result. The template matching method can simply and efficiently perform template matching on target outlines of various shapes, can effectively improve the matching accuracy in application scenarios where the foreground and background gray values differ greatly, can enhance the generalization ability of template matching, and can improve the matching efficiency while ensuring the matching accuracy.
[0141] It should be noted that although each step in the above flowchart is displayed in sequence according to the arrow, these steps are not necessarily executed in the order indicated by the arrow. Unless otherwise specified in this document, the execution of these steps has no strict order limitation, and these steps can be executed in other orders.
[0142] In one embodiment, as Figure 11 As shown, a template matching system based on template edge scoring is provided, and the system comprises:
[0143] A template making module 1 is configured to make a template image according to a to-be-detected image, and determine a middle edge, an inner edge and an outer edge of the template image.
[0144] An edge line acquisition module 2 is configured to acquire pixel point coordinates corresponding to the middle edge, the inner edge and the outer edge of the template image respectively, and draw corresponding middle edge lines, inner edge lines and outer edge lines.
[0145] An edge image generation module 3 is configured to perform size transformation on the middle edge lines, the inner edge lines and the outer edge lines respectively according to the size of the to-be-detected image, and generate an edge image according to the transformed middle edge lines, inner edge lines and outer edge lines.
[0146] An edge matching module 4 is configured to perform template matching on the to-be-detected image according to the edge image, and obtain a matching result.
[0147] The specific limitation of the template matching system based on template edge scoring can refer to the limitation of the template matching method based on template edge scoring, which is not repeated here. Each module in the template matching system based on template edge scoring can be realized by software, hardware and combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in hardware form, or can be stored in the memory in the computer device in software form, so that the processor calls and executes the operation corresponding to each module.
[0148] Figure 12 The internal structure of the computer device in an embodiment is shown, which can be a terminal or a server. As shown in the figure, the computer device includes a processor, a memory, a network interface, a display and an input device connected by a system bus. Among them, the processor of the computer device is used to provide computing and control capability. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with the external terminal through the network connection. The computer program is executed by the processor to implement a template matching method based on template edge scoring. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer overlaid on the display screen, or a key, trackball or touchpad arranged on the shell of the computer device, or an external keyboard, touchpad or mouse, etc. Figure 12
[0149] Those skilled in the art can understand that the structure shown in the figure is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied. The specific computer device can include more or fewer components than those shown in the figure, or combine certain components, or have the same component arrangement. Figure 12
[0150] In one embodiment, a computer device is provided, which includes a memory, a processor and a computer program stored on the memory and executable on the processor, and the processor executes the computer program to implement the steps of the above-mentioned method.
[0151] In one embodiment, a computer readable storage medium is provided, which stores a computer program, and the computer program is executed by the processor to implement the steps of the above-mentioned method.
[0152] In summary, the template matching method and system based on template edge scoring provided by the embodiment of the present application realizes the technical scheme of making a template image according to a to-be-detected image, determining the middle edge, inner edge and outer edge of the template image, respectively acquiring the pixel point coordinates corresponding to the middle edge, inner edge and outer edge, and drawing the corresponding middle edge line, inner edge line and outer edge line, respectively performing size transformation on the middle edge line, inner edge line and outer edge line according to the size of the to-be-detected image, and generating an edge image according to the transformed middle edge line, inner edge line and outer edge line, and then performing template matching on the to-be-detected image according to the edge image to obtain a matching result. The template matching method based on template edge scoring can simply and efficiently perform template matching on target contours of various shapes, can effectively improve the matching accuracy in application scenarios where the foreground and background gray value difference is large, can enhance the generalization ability of template matching, and can improve the matching efficiency while ensuring the matching accuracy.
[0153] Each of the embodiments in the specification is described in a progressive manner, and the same or similar parts of each of the embodiments can be referred to each other. Each of the embodiments focuses on the difference from other embodiments. In particular, the system embodiments are described simply because they are basically similar to the method embodiments. The relevant parts can be referred to the part of the description of the method embodiments. It should be noted that each of the technical features of the above embodiments can be combined arbitrarily, and in order to make the description simple, each of the technical features of the above embodiments is not described in all possible combinations, but as long as the combination of the technical features does not exist contradictory, it should be considered as the scope of the description.
[0154] The above-described embodiments only express several preferred embodiments of the present application, and the description is more specific and detailed, but it should not be understood as a limitation on the scope of the patent. It should be noted that for ordinary skilled in the art, without departing from the technical principles of the present application, a number of improvements and replacements can be made, and these improvements and replacements should be considered as the protection scope of the present application. Therefore, the protection scope of the patent of the present application should be subject to the protection scope of the claims.
Claims
1. A template matching method based on template edge scoring, characterized by, The method comprises the following steps: According to the image to be detected, a template image is made, and the middle edge, inner edge and outer edge of the template image are determined; The pixel point coordinates corresponding to the middle edge, inner edge and outer edge of the template image are obtained respectively, and the corresponding middle edge line, inner edge line and outer edge line are drawn; According to the size of the image to be detected, the middle edge line, inner edge line and outer edge line are respectively subjected to size transformation, and an edge image is generated according to the transformed middle edge line, inner edge line and outer edge line; According to the edge image, template matching is performed on the image to be detected, and a matching result is obtained; The step of transforming the size of the middle edge line, inner edge line and outer edge line according to the size of the image to be detected, and generating an edge image according to the transformed middle edge line, inner edge line and outer edge line comprises: The outer edge width and outer edge height of the outer edge line are obtained respectively, and after the outer edge line is translated to a first preset position, the size of the image to be detected is transformed according to a first proportional transformation formula; the first preset position is that the left upper corner point coordinate of the outer edge line is the origin, and the right lower corner point coordinate is (outer edge width, outer edge height); The first proportional transformation formula is: wherein, and respectively represent the pre-proportional transformation coordinates and the post-proportional transformation coordinates of any point on the outer edge line; m and n respectively represent the width and height of the image to be detected; m out and n out respectively represent the width and height of the outer edge line; The inner edge width and inner edge height of the inner edge line are obtained respectively, and after the inner edge line is translated to a second preset position, the size of the image to be detected is transformed according to a second proportional transformation formula, and the transformed inner edge line is translated according to a first translation formula; the second preset position is that the left upper corner point coordinate of the inner edge line is the origin, and the right lower corner point coordinate is (inner edge width, inner edge height); the second proportional transformation formula is: wherein and respectively denote the pre-scaling and post-scaling coordinates of any point on the outer edge line. The first translation formula is: wherein, and respectively represent the pre-translation coordinates and the post-translation coordinates of any point on the inner edge line after the proportional transformation; and respectively represent the left-top corner point coordinates of the inner edge line and the outer edge line before the dimensional transformation; The middle edge width and middle edge height of the middle edge line are obtained respectively, and after the middle edge line is translated to a third preset position, the size of the image to be detected is transformed according to a third proportional transformation formula, and the transformed middle edge line is translated according to a second translation formula; the third preset position is that the left upper corner point coordinate of the middle edge line is the origin, and the right lower corner point coordinate is (middle edge width, middle edge height); the third proportional transformation formula is: wherein and respectively denote the pre-scaling and post-scaling coordinates of any point on the outer edge line. The second translation formula is: wherein, and respectively represent the pre-translation coordinate and the post-translation coordinate of any point on the inner edge line after the proportional transformation; represents the left-top corner coordinate of the middle edge line before the size transformation; The outer edge line, middle edge line and inner edge line are collected to a blank image with the same size as the image to be detected to obtain the edge image.
2. The template matching method based on template edge score as claimed in claim 1, wherein, The step of making a template image according to the image to be detected comprises: The region of interest of the image to be detected is identified, and the region of interest is intercepted to obtain the template image.
3. The template matching method based on template edge score as claimed in claim 1, wherein, The step of determining the middle edge, inner edge and outer edge of the template image comprises: The corner point coordinates of the template image are obtained, and the middle edge of the template image is obtained according to the corner point coordinates; The inner edge and outer edge of the template image are obtained by dilating and eroding the template image.
4. The template matching method based on template edge score as claimed in claim 3, wherein, The step of obtaining the inner edge and outer edge of the template image by dilating and eroding the template image comprises: If the template image is a black background image, the outer edge is obtained by expansion processing, and the inner edge is obtained by erosion expansion processing, and vice versa, the inner edge is obtained by expansion processing, and the outer edge is obtained by erosion expansion processing.
5. The template matching method based on template edge score as claimed in claim 1, wherein, The step of performing template matching on the image to be detected according to the edge image to obtain a matching result comprises: According to the preset coordinate change step and the scale transformation step, the inner edge line and the outer edge line in the edge image are changed in nested loops, and at each nested loop step, the corresponding loop matching score is counted according to the pixel grayscale value of the inner edge line and the outer edge line, and the highest matching score is updated and the corresponding outer edge line change information is recorded; the outer edge change information includes the initial value of the upper left corner point coordinate of the outer edge line, the x-axis change scale and the y-axis change scale; According to the highest matching score, the corresponding outer edge line change information is obtained, and the edge line in the edge image is transformed according to the outer edge line change information to obtain a matching edge; the transformation formula of the middle edge line is: wherein, and respectively represent the coordinates of the middle edge line before and after the change information of the outer edge corresponding to the highest matching score is transformed; k x and k y respectively represent the x-axis change ratio and the y-axis change ratio corresponding to the highest matching score; represents the top-left corner coordinate corresponding to the highest matching score; According to the matching edge, the matching result of the image to be detected is obtained.
6. The template matching method based on template edge score as claimed in claim 5, wherein, The step of counting the corresponding loop matching score according to the pixel grayscale value at each nested loop step comprises: According to the pixel point coordinates of each pixel point of the corresponding outer edge line at each nested loop step, the corresponding grayscale value on the image to be detected is obtained, and the outer edge score is counted; the outer edge score is represented as: wherein, , and denote the outer edge fraction, the outer edge gray value accumulation and the outer edge point number of the lth nested loop. According to the pixel point coordinates of each pixel point of the corresponding inner edge line at each nested loop step, the corresponding grayscale value on the image to be detected is obtained, and the inner edge score is counted; the outer edge score is represented as: wherein, , and respectively represent the inner edge score, the inner edge gray value accumulation and the inner edge point number of the lth round of nested loop. According to the outer edge score and the inner edge score, the loop matching score is obtained; the loop matching score is represented as: wherein, represents the cycle match score of the lth nested loop.
7. A template matching system based on template edge scoring, characterized by, The system comprises the template matching method based on template edge score according to claim 1. A template making module is configured to make a template image according to an image to be detected, and determine a middle edge, an inner edge and an outer edge of the template image. An edge line acquisition module is configured to acquire pixel point coordinates corresponding to the middle edge, the inner edge and the outer edge of the template image respectively, and draw corresponding middle edge lines, inner edge lines and outer edge lines. An edge image generation module is configured to perform size transformation on the middle edge lines, the inner edge lines and the outer edge lines respectively according to the size of the image to be detected, and generate an edge image according to the transformed middle edge lines, inner edge lines and outer edge lines. An edge matching module is configured to perform template matching on the image to be detected according to the edge image to obtain a matching result.
8. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the computer program to realize the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to realize the steps of the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Mobile phone casing defect detecting method based on depth learning
CN106875381A
Image detection method and device, electronic equipment and computer readable storage medium
CN114170613A