Method, device, equipment and medium for calculating effective area of recognition region
Patent Information
- Application Number
- CN202410029166.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-05
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2044-01-05
AI Technical Summary
[0005]本发明的主要目的为提供计算识别区域有效面积的方法、装置、设备和介质,旨在解决现有方法的识别区域存在误差的问题
本申请整体的复杂度只与多边形的个数、顶点个数相关,与多边形区域的大小无关,且顶点位置可以是浮点数甚至精度更高的双精度浮点数,即本发明可以调整EPSILON的大小使得算法可以应用于精度要求更高的场景。此外,本发明避免了三角函数和反三角函数的调用,一般的多边形求交并算法都会涉及这两类函数,然而这两类函数无论使用何种手段实现,复杂度都高于向量叉乘,实现高频率、高重合度多识别框快速融合,且可以处理非凸复杂区域的布尔运算,如瑕疵识别区域融合这一类应用场景,具有较高的可靠性及较低的时间、空间复杂度。
Smart Images

Figure CN118096860B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to methods, apparatus, devices and media for calculating the effective area of a recognition region. Background Technology
[0002] Polygon Boolean operations are one of the most fundamental algorithms in computer graphics and computational geometry, widely used in geometric solid modeling, geographic information systems (GIS), and other fields. In computer vision, particularly in deep learning object recognition applications, models may identify the same target multiple times, or the recognition results from multiple images may need to be merged, ultimately generating multiple overlapping recognition regions. Because practical production applications demand high efficiency and accuracy from the algorithm, a highly efficient algorithm is needed to merge the model's recognition results and calculate the effective area of the regions.
[0003] Currently, there are many feasible and efficient algorithms for intersection and union operations of two polygons. Among them, Weiler's algorithm, which starts from the intersection points and their categories, has a complexity of O(N*M) only in the intersection point calculation step, and O(M+N) in the solution phase, exhibiting extremely high computational efficiency and capable of handling Boolean operations between non-convex polygons. However, many algorithms, including Weiler's, are not designed for merging multiple polygon regions. The problem can be decomposed into Boolean operations between two polygons. This requires a set of logic to determine which polygons need to be merged, and to avoid regions participating in the calculation repeatedly; otherwise, the final generated regions may still have intersections. Secondly, when considering the problem as a whole, there will be considerable redundant calculations, such as the intersection point calculation when determining region intersections being repeated with the final intersection point calculation. Finally, some polygon Boolean operation algorithms do not consider the error caused by floating-point numbers, and will output incorrect calculation results when dealing with degenerate cases, such as overlapping polygon edges or intersection points at vertices. The recognition regions output by deep learning models are very prone to overlap and intersection at vertices.
[0004] To address these technical problems, we propose a method, apparatus, device, and medium for calculating the effective area of the identified region. Summary of the Invention
[0005] The main objective of this invention is to provide a method, apparatus, device, and medium for calculating the effective area of an identification region, aiming to solve the problem of errors in the identification region of existing methods.
[0006] To achieve the aforementioned objective, the first aspect of this invention provides a method for calculating the effective area of an identified region, comprising: Step 1: Obtain the vertex data and defect types of the recognition region set output by the neural network model; Step 2: The order of the vertices in the unified region is counterclockwise; Step 3: Quickly calculate the intersection between each pair of regions and generate an intersection map of the regions; Step 4: Based on Weiler Atherton's polygon Boolean operation algorithm, make targeted improvements by quickly merging non-overlapping regions through depth-first traversal, and merge vertices of temporarily generated regions during the merging process. Step 5: Calculate the clockwise or counterclockwise rotation of the boundary vertices of the generated region set, and calculate the in-depth area of the region using the Shoelace Theorem; Step 6: Combine the clockwise and counterclockwise rotation of the region boundary vertices with the area of the region inside the boundary to calculate the defect area ratio of the overall image.
[0007] Furthermore, in step one, when inputting the position data of vertices in the image and the type of defects, the maximum number of rows (maxR), maximum number of columns (maxC), minimum number of rows (minR), and minimum number of columns (minC) of each region in the image are calculated to quickly determine the correlation between regions.
[0008] Furthermore, in step two, curve integrals are calculated along the edges of the polygon. If the integral is greater than 0, the vertex order of that region is counterclockwise, pointing to the enclosed region; otherwise, it is clockwise, pointing to the outside of the enclosed region. In this step, all regions are internal regions enclosed by their boundaries. If the vertex order of a certain region is clockwise, it needs to be corrected in this step by reversing the vertex order to ensure that the subsequent region merging calculation is executed correctly.
[0009] Furthermore, step three is performed as follows: A1. Quickly determine whether two regions are completely non-overlapping by using maxR, maxC, minR, and minC for each region in step one; A2. Set the regions corresponding to maxR1, maxC1, minR1 and minC1 as region 1, and set the regions corresponding to maxR2, maxC2, minR2 and minC2 as region 2. A3. Two regions must be separate if any of the following conditions are true: maxR1 <minR2-EPSILON; maxR2 <minR1-EPSILON; maxC1 <minC2-EPSILON; maxC2 <minC1-EPSILON; EPSILON is the set error tolerance value. When none of the above conditions are true, the minimum non-rotation external rectangles of the two regions coincide. A4. Calculate and store the valid intersection points of the ray generated from the first vertex of region 1 to the second vertex and each edge of region 2. If the intersection point falls on the first edge, the two regions intersect. Otherwise, proceed to step A5. Determine whether the first vertex of region 1 is inside region 2 by the parity of the number of valid intersection points. Similarly, determine whether the first vertex of region 2 is inside region 1. A5. Calculate whether there are valid intersections between the remaining edge and all edges of the other region and record whether each edge of the region has a valid intersection. When there is a valid intersection, it is determined that the two regions intersect. When there is no valid intersection, combine the positional relationship between the first vertex of the two regions and the other region to determine whether the two regions are separate or have an inclusion relationship. An inclusion relationship is considered as an intersection. When a region has no intersecting regions, it is directly stored in the result region set.
[0010] Furthermore, step four is performed as follows: B1. Traverse all polygon regions. If the current region has already participated in the region operation, skip it. Otherwise, use the current region as the core to generate a complete connected region until all regions connected to the current region participate in the operation. B2. Perform polygon union operation; B3. After completing the intersection calculation and sorting, perform the region union operation.
[0011] Furthermore, step B1 is performed as follows: B1.1, Traverse all polygonal regions; B1.2 Determine if there are any regions that were not involved in the calculation. If not, output the calculation result set S. If yes, proceed to step B1.3. B1.3 When there are regions that are not involved in the calculation, obtain the next polygon region that is not involved in the calculation; B1.4. Using the polygon as the parent node, record that the polygon has participated in the calculation, and denote the polygon as P; B1.5 Query the related polygonal child nodes of the parent node; B1.6. Traverse all child node polygon regions pi; B1.7 Determine if there are any child node polygons that are not involved in the calculation. If yes, obtain the polygon pi that is not involved in the calculation and proceed to step B1.12. If no, proceed to step B1.8. B1.8 Return to the parent node; B1.9 Determine whether to return to the initial parent node. If yes, save the result to the calculation result set S, and enter the next polygon region, then proceed to step B1.2. If no, proceed to step B1.10. B1.10, Query the next child node pi that has not participated in the calculation; B1.11. Determine if there is a child node pi that did not participate in the operation. If yes, proceed to step B1.12; otherwise, return to step B1.8. B1.12. Record that the polygon has been used in the calculation; B1.13. Denote pi as the new parent node and proceed to step B1.5.
[0012] Furthermore, step B2 is performed as follows: B2.1 Obtain the vertex position data and pointing region declaration of simple polygons p1 and p2 with holes; B2.2 Remove vertices that are too close to the next point or lie on the line connecting the two points; B2.3 Correct the vertex order to match the declared region; B2.4 Calculate the intersection points, classify the intersection points, and add the valid intersection points to the intersection point set S1; B2.5. Traverse the points in the intersection set S1 and generate the calculation results according to the specified rules; B2.6 Calculate the top-end order of the polygons in the calculation result, determine their pointing areas, and output the calculation result.
[0013] Furthermore, step B3 is performed as follows: B3.1. Obtain an unvisited intersection point D0 from the intersection point set S1, denoted as D=D0, and record that D0 has been visited. Add an empty polygon P to the result set S, and add the first vertex D0 to P. B3.2 Determine if point D0 exists. If yes, proceed to step B3.3; otherwise, output the result set. B3.3, Query the intersection type of D; B3.4 When the intersection type of D is an outgoing point, proceed to step B3.5; B3.5. Let D1 be the next right polygon point after D; B3.6 Determine if D is D0. If yes, return to step B3.1; otherwise, proceed to step B3.7. B3.7 Record that D has been visited, add D to the end of P, and then return to step B3.3; B3.8 When the intersection type of D is non-intersection, determine whether D is in the left polygon. If yes, proceed to step B3.9; otherwise, proceed to step B3.5. B3.9. Let D2 be the next left polygon point of D, and proceed to step B3.6; B3.10 When the intersection type of D is an in-point, proceed to step B3.9.
[0014] Secondly, embodiments of this application provide an apparatus for calculating the effective area of an identified region, the apparatus comprising: The acquisition module is used to acquire vertex data and defect types of the recognition region set output by the neural network model; The first calculation module is used to calculate the intersection between pairs of regions and generate an intersection map of the regions; The fusion module is used to quickly fuse and generate a set of non-overlapping regions. During the fusion process, vertex merging is performed on temporarily generated regions. The second calculation module is used to calculate the clockwise or counterclockwise direction of the boundary vertices of the generated region set; The statistics module is used to calculate the percentage of defective areas in the overall image.
[0015] Thirdly, embodiments of this application provide a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the method for calculating the effective area of an identification region as described in any of the above claims.
[0016] Fourthly, embodiments of this application provide a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps of a method for calculating the effective area of an identification region as described in any one of the claims.
[0017] Beneficial effects: The overall complexity of this application depends only on the number of polygons and vertices, and is independent of the size of the polygon region. Furthermore, the vertex positions can be floating-point numbers or even higher-precision double-precision floating-point numbers. This means the size of the EPSILON can be adjusted to allow the algorithm to be applied to scenarios with higher precision requirements. In addition, this invention avoids the use of trigonometric and inverse trigonometric functions. General polygon intersection and union algorithms involve these two types of functions; however, regardless of the implementation method, their complexity is higher than vector cross products. This enables rapid fusion of high-frequency, high-overlapping multi-box recognition, and can handle Boolean operations in non-convex complex regions, such as defect recognition region fusion applications, exhibiting high reliability and low time and space complexity. Attached Figure Description
[0018] Figure 1 This is a schematic diagram of the intersecting relationships of the regions in this invention; Figure 2 This is a schematic diagram of the overall logic for multi-region union in this invention; Figure 3 This is a schematic diagram illustrating the basic process of merging two simple, hole-free polygonal regions according to the present invention. Figure 4 This is a schematic diagram of the solution process after the intersection point classification in this invention; Figure 5 This is a schematic diagram illustrating the vertex position of the region declaration in this invention; Figure 6 This is a schematic diagram showing that the vertex order of the present invention is counterclockwise; Figure 7 For the present invention Figure 6 A schematic diagram of the external area; Figure 8 This is a schematic diagram of the entry point relationship of the present invention; Figure 9 This is a schematic diagram of the output point relationship of the present invention; Figure 10 This is a schematic diagram of invalid intersection relationships in this invention; Figure 11 This is a schematic diagram of the fan-shaped radiation region S of the present invention; Figure 12 This is a schematic diagram illustrating the intersection point category determination method of the present invention. Figure 13 This is a schematic diagram of the intersection state before sorting in this invention; Figure 14 This is a schematic diagram of the intersection state after sorting according to the present invention; Figure 15 This is a schematic diagram of the basic logic of intersection sorting in this invention; Figure 16 This is a schematic diagram of the vertex topology of the region in this invention; Figure 17 For the present invention Figure 16 Element meaning diagram; Figure 18 This is a schematic diagram of the simple holeless counterclockwise polygonal intersection relationship tree of the present invention; Figure 19 This is a schematic diagram of the steps of the present invention. The realization of the objective, functional characteristics, and advantages of the present invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0020] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this specification means the presence of features, integers, steps, operations, elements, modules, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, modules, components, and / or groups thereof. It should be understood that when we say an element is “connected” or “coupled” to another element, it can be directly connected or coupled to the other element, or there may be intermediate elements. Furthermore, “connected” or “coupled” as used herein can include wireless connections or wireless coupling. The term “and / or” as used herein includes all or any modules and all combinations of one or more associated listed items.
[0021] It will be understood by those skilled in the art that, unless otherwise defined, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. It should also be understood that terms such as those defined in general dictionaries should be understood to have the same meaning as in the context of the prior art, and should not be interpreted in an idealized or overly formal sense unless specifically defined as herein.
[0022] Reference Figure 19 This invention provides a method for calculating the effective area of an identification region, comprising: Step 1: Obtain the vertex data and defect types of the recognition region set output by the neural network model; Step 2: The order of the vertices in the unified region is counterclockwise; Step 3: Quickly calculate the intersection between each pair of regions and generate an intersection map of the regions; Step 4: Based on Weiler Atherton's polygon Boolean operation algorithm, make targeted improvements by quickly merging non-overlapping regions through depth-first traversal, and merge vertices of temporarily generated regions during the merging process. Step 5: Calculate the clockwise or counterclockwise rotation of the boundary vertices of the generated region set, and calculate the in-depth area of the region using the Shoelace Theorem; Step 6: Combine the clockwise and counterclockwise rotation of the region boundary vertices with the area of the region inside the boundary to calculate the defect area ratio of the overall image.
[0023] In step one, when inputting the position data of vertices in the image and the type of defects in the input region, the maximum number of rows maxR, the maximum number of columns maxC, the minimum number of rows minR, and the minimum number of columns minC of each region in the image are counted. These four indicators will be used to quickly determine the correlation between regions. For step two, the effective area of the region is calculated using Green's integral formula. If the integral is greater than 0, the vertex order of the region is counterclockwise, pointing to the enclosed region; otherwise, it is clockwise, pointing to the outside of the enclosed region. In this step, all regions are internal regions enclosed by their boundaries. If the vertex order of a certain region is clockwise, it needs to be corrected in this step by reversing the vertex order to ensure that the subsequent region merging calculation is executed correctly. Please see Figure 1 For step three, first use the maxR, maxC, minR, and minC values of each region in step one to quickly determine whether the two regions are completely non-overlapping. For example, for regions 1 and 2, the corresponding values are maxR1, maxC1, minR1, and minC1, and maxR2, maxC2, minR2, and minC2. If any of the following conditions are true, the two regions must be separate: maxR1 <minR2-EPSILON; maxR2 <minR1-EPSILON; maxC1 <minC2-EPSILON; maxC2 <minC1-EPSILON; EPSILON is the set error tolerance value. If none of the above conditions are true, it means that the minimum non-rotation external rectangles of the two regions overlap to some extent. Further judgment requires considering the relationship between the region boundaries and vertices.
[0024] Calculate and store the valid intersection points of the ray generated from the first vertex of region 1 to the second vertex and each edge of region 2. If an intersection point is found to fall on the first edge, it means that the two regions intersect. Otherwise, continue with the subsequent operations. Determine whether the first vertex of region 1 is inside region 2 by the parity of the number of valid intersection points. Similarly, determine whether the first vertex of region 2 is inside region 1.
[0025] The previous operations have calculated the intersection of the first edge of one region with all edges of the other region. The next step is to calculate whether the remaining edges have valid intersections with all edges of the other region and record whether each edge of a region has a valid intersection. If a valid intersection is found, the two regions are considered to intersect. If no valid intersection is found, the positional relationship between the first vertex of one region and the other region can be used to determine whether the two regions are disjoint or contain each other. Containment relationships are still considered intersections. If a region has no intersecting regions, it is directly stored in the result region set. Recording the intersection relationships between regions generates... Figure 1 .
[0026] Please see Figure 2For step four, traverse all regions. If the current region has already participated in region merging, skip it; otherwise, use that region as the core to generate a complete connected region until all regions connected to that region participate in the calculation. The basic process of the polygon union algorithm is as follows: Figure 3 After completing the intersection calculation and sorting, the operation logic for region union is as follows: Figure 4 .
[0027] In this invention, the algorithm itself consists of multiple modules, including the following: 1. Intersection and union operations between two simple polygons; 2. Union operation between two polygons with holes; 3. Merge a finite number of identified regions and calculate the total area.
[0028] Each module depends on the implementation of the previous module. The implementation of each module is explained here: 1. Intersection and union operations between two simple polygons Some conventions that need to be declared in advance in this invention: constants This constant is used to control the allowable floating-point error; the cross product of two-dimensional vectors is regarded as the cross product of three-dimensional vectors with a z-value of 0, and the result is the z-value of the calculated result, such as (a1,b1)×(a2,b2)=a1*b2-a2*b1; for each vertex and intersection, its structure design in the algorithm includes the following members: `loc` is an array of two vertex positions. An array of length two containing the distances between alpha and the vertices of the next left and right polygons; Next left vertex of the polygon; Next right polygon vertex; The upper left vertex of the polygon; The upper right vertex of the polygon; Intersection type; Has it been included in the calculation?
[0029] When receiving vertex data for two polygons, the system also receives declarations of the regions they point to. For example, the received left polygon P1 is declared to point to an internal region, with its vertex positions M1(x11,y11), M2(x12,y12), M3(x13,y13), and M4(x14,y14); the right polygon P2 is declared to point to an internal region, with its vertex positions N1(x21,y21), N2(x22,y22), N3(x23,y23), and N4(x24,y24). An example diagram is shown below. Figure 5 As shown, N2 is on the M1-M2 side of the left polygon, and N1 is on the M1-M4 side.
[0030] The execution of this module's algorithm includes the following steps: (1) Remove redundant vertices Because the boundaries of the recognition regions generated by the neural network model have a high probability of overlapping, the intermediate polygons generated in subsequent calculations often have similar vertices, or a vertex is almost on the line connecting its preceding and following points. These types of vertices are removed in this invention to reduce unnecessary computation. The detection principle is as follows: It checks whether the L1 norm is less than... Determine if two points are too close; for two adjacent edges of a point. , forming a vector Calculate the cross product The L1 norm, when the result is less than When the time is right, it means that the point is almost on the line connecting the two points before and after.
[0031] (2) Correcting the vertex order In this invention, when the order of the vertices is counterclockwise, such as... Figure 6 As shown, only the area pointed to by the polygon is the area enclosed by the polygon; otherwise, it is the outer area. Figure 7 As shown.
[0032] To ensure the correctness of subsequent calculations, after removing unnecessary vertices, it is also necessary to calculate the order of the vertices. Taking P1 as an example, the calculation method is as follows:
[0033] When greater than 10* When the vertex order is counterclockwise; less than The time should be counter-clockwise; otherwise, it indicates that the effective area of P1 is too small, and the precision limitations of floating-point numbers during calculation may lead to calculation errors. This polygon cannot participate in the calculation. The above calculation formula and principle are modified from Green's theorem, and the absolute value of the integral result is the area of the polygon's interior region. When the order of the polygon vertices is inconsistent with the declaration, simply reverse the order of the vertices.
[0034] During this stage, for each point of the left polygon, the pointers to the vertices of the preceding and following left polygons are recorded, and the same process is applied to the right polygon. If both polygons are counterclockwise polygons, the maximum and minimum x and y values of the polygons are simultaneously calculated.
[0035] (3) Calculate the intersection point If both polygons are counterclockwise, you can quickly determine whether the two polygons are necessarily separated by comparing the maximum and minimum x and y values.
[0036] This step calculates the intersection point between the two polygons. Taking M1 and N1 as examples, the corresponding first points are (x14, y14) and (x24, y24) respectively, and the corresponding second points are (x1n, y1n) and (x2n, y2n) respectively. The edges S1 corresponding to (x14, y14) and (x11, y11) are the same, and the edges S2 corresponding to (x24, y24) and (x21, y21) are the same. The intersection point of S1 and S2 is calculated. To avoid adding duplicate intersection points, if the intersection point is at the starting point of an edge, it is considered non-intersecting. Let: S1 starts from the vector V pointing to the end point. MN,MF = ; S2 starting point to ending point vector V SN,SF = ; The vector V pointing from the end point of S1 to the starting point of S2 MN,SF = ; The vector V pointing from the end point of S2 to the starting point of S1 SN,MF =
[0037] The vector V pointing from the end point of S2 to the end point of S1 SN,MN =
[0038] The vector V pointing from the starting point of S2 to the starting point of S1 SF,MF =
[0039] This is used to determine the positional relationship between the starting point of S2 and S1; This is used to determine the positional relationship between the end point of S2 and S1. This is used to determine the positional relationship between the end point of S1 and S2; This is used to determine the positional relationship between the starting point of S1 and S2; This is used to determine the positional relationship between the lines containing S2 and S1; This is used to determine the positional relationship between the lines containing S1 and S2; It should be mentioned that the cross product "×" is defined as (a1,b1)×(a2,b2)=a1*b2-a2*b1.
[0040] The following cases will be discussed: a. If The L1 norm is less than EPSILON. the two points are regarded as the same point, that is, S1 and S2 intersect at the end point, denoted as C=(x11,y11), the next point of the left polygon after this intersection is Cmn=(x1n,y1n), and the next point of the right polygon is Csn=(x2n,y2n); b. If , , the L1 norm of any one is less than EPSILON, it indicates that S1 and S2 intersect at the starting point of a certain edge, such a case is regarded as no intersection, and the process directly proceeds to the intersection calculation of the next pair of edges c. If > EPSILON, or > EPSILON, there must be no intersection between the two edges, and the process proceeds to the intersection calculation of the next pair of edges; d. If < -EPSILON, and < -EPSILON, there must be an intersection between the two edges S1 and S2, and the intersection is not on the original vertices. The intersection is denoted as C, the accurate coordinates of C are obtained, and denoted as Cmn=(x1n,y1n) and Csn=(x2n,y2n); e. If | | EPSILON, and | | EPSILON, the two edges are parallel and the straight lines where they are located coincide, which is a degenerate case. In this case, if * > 0, (x11,y11) is the intersection; if * < 0, (x21,y21) is the intersection, these two cases may exist simultaneously; in other cases, the two edges do not intersect.
[0041] f. If | | < EPSILON, and < -EPSILON, then S2 intersects S1 at (x11,y11), the intersection is denoted as C=(x11,y11), and denoted as Cmn=(x1n,y1n) and Csn=(x21,y21); g. If | | < EPSILON, and < -EPSILON, Then S2 intersects S1 at Let C = (x21, y21), Cmn = (x11, y11), and Csn = (x2n, y2n).
[0042] (4) Intersection classification: After calculating the intersection points, their positions are obtained. However, some types of intersection points are not practically meaningful. This step involves classifying the obtained intersection points, adding the valid intersection points to the intersection point set of the corresponding edge, so that the intersection points on the same edge can be sorted in the next step, such as... Figure 8-10 As shown.
[0043] Figure 8-10 The precise definitions of the three types of intersection points are as follows: like Figure 11 As shown, let S be the fan-shaped radiating region centered at the intersection point, extending counterclockwise from the left polygon point after the intersection point to the left polygon point in front of the intersection point; Outgoing points: Before the intersection, the right polygon point is inside S; after the intersection, the right polygon point is outside S. Entry point: Before the intersection, the right polygon point is outside S; after the intersection, the right polygon point is inside S. Invalid intersection: The points on the right side of the polygon before and after the intersection are both inside or outside S.
[0044] If a point falls on the boundary, it is still considered to fall within S.
[0045] In calculating the intersection point, the intersection point position C and its next points Cmn and Csn on the left and right polygons have been obtained. Let the vector be: Vtmf = (x14, y14) - C; Vtsf = (x24, y24) - C; Vtmn =Cmn-C; Vtsn =Csn-C.
[0046] The key to determining the type of intersection point lies in whether the two points Csn and (x24, y24) on the right polygon are within the sector centered at C and pointed to by Cmn at (x14, y14). The determination process is illustrated using Csn as an example: like Figure 12 As shown, when ∠CmnCDm∈[0°,180°], Csn can only be considered as within the sector if ∠CmnCCsn∈[0°,180°] and ∠CsnCDm∈[0°,180°]; otherwise, it is outside the sector. [0°, 180°], Csn can be considered outside the sector only if ∠DmCCsn∈[0°, 180°] and ∠CsnCCmn∈[0°, 180°], otherwise it is inside the sector. This is only a geometric discussion; the following will translate it into specific algebraic operations.
[0047] When Vtmn×Vtmf>-EPSILON, Csn can only be considered as within the sector if Vtmn×Vtsn>-EPSILON and Vtsn×Vtmf>-EPSILON; otherwise, it is outside the sector. When Vtmn×Vtmf is less than-EPSILON, Csn can only be considered as outside the sector if Vtmn×Vtsn<=-EPSILON and Vtsn×Vtmf<=-EPSILON.
[0048] If both points Csn and (x24, y24) are within or outside the region, the intersection point is invalid. If Csn is within the region and (x24, y24) is outside the region, the intersection point is an in-point; otherwise, it is an out-point. Invalid intersection points will not be added to the intersection point set and will not be discussed further.
[0049] (5) Intersection sorting like Figure 13 As shown, when two polygons intersect, there may be multiple intersection points on one side of a polygon, forming a set of intersection points. D1 and D2 are original vertices and not intersection points, while C1, C2, C3, C4, and C5 are intersection points. Before sorting, only the data structures of D1 and D2 are connected; the relevant members in the intersection point structures are still null. After calculating the valid intersection points on this side, these intersection points need to be included in the connections to generate the correct topology.
[0050] Reconnect within the vertex structure to ensure the correct vertex order. An example of the sorting result is shown below. Figure 14 As shown.
[0051] For an edge L of the left polygon, with a preceding vertex D1 and a following vertex D2, the set of their upper intersection points is Sc. For all points C within Sc, perform the following... Figure 15 As shown, this completes the sorting of Sc on L.
[0052] (6) Union operation After the preceding operations, a topological graph of the vertex relationships of the two polygons was generated, for example... Figure 16 As shown, the meaning is as follows Figure 17 As shown.
[0053] Based on an existing topological graph, the general logic for merging two simple, holeless polygons is as follows: Figure 4 As shown.
[0054] The following is based on Figure 16 For example, let's illustrate the solution process for this diagram: Starting from the first intersection point C1, which is the entry point, the next point moves to point M3 on the left polygon. M3 is not an intersection point, so the next point moves to point C2 on the left polygon. C2 is the exit point, and the next point moves to point C1 on the right polygon. The intersection point C1 is the starting point of this round, and the current round of calculation ends. Save the result of this round of calculation: C1-M3-C2. The calculated order is clockwise.
[0055] If the second intersection point has already been used in the calculation, then skip that intersection point.
[0056] Query the third intersection point C3. C3 is the exit point. Move to the next point N3 on the right polygon. N3 is not an intersection point. Move to the next point N4 on the right polygon. N4 is not an intersection point. Move to the next point C4 on the right polygon. C4 is the entry point. Move to the next point M4 on the left polygon. M4 is not an intersection point. Move to the next point M1 on the polygon. M1 is not an intersection point. Move to the next point M2 on the polygon. M2 is not an intersection point. Move to the next point C3 on the polygon. Return to the starting point, end the original calculation, and save the result of this round of calculation: C3-N3-N4-C4-M4-M1-M2. The calculated order is counterclockwise.
[0057] Query the fourth intersection point C4. C4 has already been used in the calculation, so skip this intersection point.
[0058] Therefore, the result of the union is: Outer contour: polygon C3-N3-N4-C4-M4-M1-M2, hole: polygon C1-M3-C2.
[0059] 2. Union operation between simple polygons with holes and simple non-hole counterclockwise polygons. The data structure for simple polygons with holes includes an outer contour polygon arranged counter-clockwise, and a finite number of holes represented by multiple clockwise polygons. The following example illustrates the union operation between simple polygons with holes and simple non-hole counter-clockwise polygons.
[0060] Simple polygon with a hole: P1=(Pc,Ph1,Ph2,Ph3), where Pc is a counterclockwise polygon, and Ph1, Ph2, and Ph3 are clockwise polygons; Simple polygon: P2, which is a counterclockwise polygon.
[0061]
[0062] Each union term in the above formula is a union of two simple polygons, which is done by directly using the intersection and union operation between two simple polygons.
[0063] remember It should be noted that the union of a simple polygon with a hole and a simple polygon without a hole is still a simple polygon with a hole. Therefore, in this invention, only the four union terms mentioned above need to be calculated, without further proof. That is, the union of a simple polygon with a hole and a simple polygon without a hole can be achieved by simply calling the intersection and union operation between two simple polygons.
[0064] 3. Merging of a limited number of identification regions and calculation of the total area (1) Assume the simple, holeless counterclockwise polygons corresponding to the recognition regions output by the neural network are: P1, P2, P3, P4, P5, ..., P30. By obtaining the intersection relationships between each pair of regions through 1), a polygon is generated as shown in Figure 1. Figure 18 The intersecting relationship tree shown includes containment relationships, which are also considered intersecting.
[0065] (2) Perform the following operations on each region: First, check if the region has already participated in the calculation. If it has, skip it; otherwise, denote the simple perforated region P as the current region. Recursively perform a depth-first traversal of simple non-perforated regions that intersect or indirectly intersect with the current region. For each region obtained, calculate its union with P, assign the result to P, and record that the region has participated in the calculation. After traversing all relevant regions, store P in the result set.
[0066] The calculation process is illustrated using regions P1 and P5 in the diagram as examples: For P1, let P=P1, indicating that P1 has participated in the operation. Query for related polygons P3 that have participated in the operation, and let P=P1∪P3. This is considered as the union of a simple counterclockwise polygon with a hole and a simple counterclockwise polygon without a hole. Calling 2) completes the operation, and we record that P3 has participated in the operation. Query for P2 under P3 that has participated in the operation and intersects with it, and let P=P∪P2, recording that P2 has participated in the operation. Query for all intersecting regions of P2 that have participated in the operation, and return to the parent node P3. All intersecting regions of P3 have participated in the operation. Then return to the parent node P1 and perform similar operations on all nodes of P1 to obtain the connected region P generated by P1 as the core. P is the union of P1 and all regions that intersect with P1 indirectly or directly. Add P to the result set.
[0067] P2 has already been involved in the operation of P1, so it is skipped.
[0068] After performing the above operations on all regions, the result set is a collection of non-intersecting regions. Each element in the region set is a simple, hole-covered, counter-clockwise polygon. For each simple, hole-covered polygon, its effective area can be calculated using the Shoelace Theorem formula, thus obtaining the total effective area of the identified regions.
[0069] Although the complete solution process of this algorithm is logically complex and appears quite lengthy, the time and space complexity of each step is actually quite low, and the functions called are simply combinations of four arithmetic operations. The overall complexity of this invention is only related to the number of polygons and vertices, and is independent of the size of the polygon region. Furthermore, the vertex positions can be floating-point numbers or even higher-precision double-precision floating-point numbers, meaning that the size of the EPSILON can be adjusted to allow the algorithm to be applied to scenarios with higher precision requirements. In addition, this invention avoids the use of trigonometric and inverse trigonometric functions. General polygon intersection and union algorithms involve these two types of functions; however, regardless of the implementation method, the complexity of these two types of functions is higher than that of vector cross products. Polygon union algorithms based on scan lines or connected regions cannot meet this requirement due to their finite number of traversal steps. The complexity of these two types of algorithms is equivalent to the size of the region containing all polygons. In actual operation, images recognized by neural networks may reach hundreds of millions of pixels. In production applications where overhead is critical, neither of these two types of algorithms can meet the requirements.
[0070] This application embodiment also provides an apparatus for calculating the effective area of an identification region, the apparatus comprising: The acquisition module is used to acquire vertex data and defect types of the recognition region set output by the neural network model; The first calculation module is used to calculate the intersection between pairs of regions and generate an intersection map of the regions; The fusion module is used to quickly fuse and generate a set of non-overlapping regions. During the fusion process, vertex merging is performed on temporarily generated regions. The second calculation module is used to calculate the clockwise or counterclockwise direction of the boundary vertices of the generated region set; The statistics module is used to calculate the percentage of defective areas in the overall image.
[0071] This invention also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the method for calculating the effective area of an identification region as described in any of the preceding claims.
[0072] This invention also provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the steps of a method for calculating the effective area of an identification region as described in any one of the claims.
[0073] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. Any references to memory, storage, databases, or other media used in this application and in the embodiments can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0074] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, apparatus, article, or method that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, apparatus, article, or method. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, apparatus, article, or method that includes that element.
[0075] The above description is merely a preferred embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A method for calculating the effective area of an identification region, characterized in that, Includes the following steps: Step 1: Obtain the vertex data and defect types of the recognition region set output by the neural network model; Step 2: The order of the vertices in the unified region is counterclockwise; Step 3: Quickly calculate the intersection between each pair of regions and generate an intersection map of the regions; Step 4: Based on Weiler Atherton's polygon Boolean operation algorithm, make targeted improvements by using depth-first traversal to generate a set of non-overlapping regions, and merge the vertices of the temporarily generated regions during the fusion process. Step 5: Calculate the clockwise or counterclockwise rotation of the boundary vertices of the generated region set, and calculate the area contained within the region using the Shoelace Theorem; Step 6: Combine the clockwise and counterclockwise rotation of the region boundary vertices with the area of the region inside the boundary to calculate the defect area ratio of the overall image. Step three is performed as follows: A1. Quickly determine whether two regions are completely non-overlapping by using maxR, maxC, minR, and minC for each region in step one; A2. Set the regions corresponding to maxR1, maxC1, minR1 and minC1 as region 1, and set the regions corresponding to maxR2, maxC2, minR2 and minC2 as region 2. A3. Two regions must be separate if any of the following conditions are true: maxR1 <minR2-EPSILON; maxR2 <minR1-EPSILON; maxC1 <minC2-EPSILON; maxC2 <minC1-EPSILON; EPSILON is the set error tolerance value. When none of the above conditions are true, the minimum non-rotation external rectangles of the two regions coincide. A4. Calculate and store the valid intersection points of the ray generated from the first vertex of region 1 to the second vertex and each edge of region 2. If the intersection point falls on the first edge, the two regions intersect; otherwise, proceed to step A5. Determine whether the first vertex of region 1 is inside region 2 by the parity of the number of valid intersection points, and similarly determine whether the first vertex of region 2 is inside region 1. A5. Calculate whether there are valid intersections between the remaining edge and all edges of the other region and record whether each edge of the region has a valid intersection. When there is a valid intersection, it is determined that the two regions intersect. When there is no valid intersection, combine the positional relationship between the first vertex of the two regions and the other region to determine whether the two regions are separate or have an inclusion relationship. An inclusion relationship is considered as an intersection. When a region has no intersecting regions, it is directly stored in the result region set.
2. The method for calculating the effective area of the identification region according to claim 1, characterized in that, In step one, when inputting the position data of vertices in the image and the type of defects in the input region, the maximum number of rows (maxR), maximum number of columns (maxC), minimum number of rows (minR), and minimum number of columns (minC) of each region in the image are calculated to quickly determine the correlation between regions.
3. The method for calculating the effective area of the identification region according to claim 2, characterized in that, In step two, the curve integral is calculated along the edges of the polygon. If the integral is greater than 0, the vertex order of the region is counterclockwise, pointing to the enclosed region; otherwise, it is clockwise, pointing to the outside of the enclosed region.
4. The method for calculating the effective area of the identification region according to claim 1, characterized in that, Step four is performed as follows: B1. Traverse all polygon regions. If the current region has already participated in the region operation, skip it. Otherwise, use the current region as the core to generate a complete connected region until all regions connected to the current region participate in the operation. B2. Perform polygon union operation; B3. After completing the intersection calculation and sorting, perform the region union operation.
5. The method for calculating the effective area of the identification region according to claim 4, characterized in that, Step B1 is performed as follows: B1.1, Traverse all polygonal regions; B1.2 Determine if there are any regions that were not involved in the calculation. If not, output the calculation result set S. If yes, proceed to step B1.
3. B1.3 When there are regions that are not involved in the calculation, obtain the next polygon region that is not involved in the calculation; B1.
4. Using the polygon as the parent node, record that the polygon has participated in the calculation, and denote the polygon as P; B1.5 Query the related polygonal child nodes of the parent node; B1.
6. Traverse all child node polygon regions pi; B1.7 Determine if there are any child node polygons that are not involved in the calculation. If yes, obtain the polygon pi that is not involved in the calculation and proceed to step B1.
12. If no, proceed to step B1.
8. B1.8 Return to the parent node; B1.9 Determine whether to return to the initial parent node. If yes, save the result to the calculation result set S, and enter the next polygon region, then proceed to step B1.
2. If no, proceed to step B1.
10. B1.10, Query the next child node pi that has not participated in the calculation; B1.
11. Determine if there is a child node pi that did not participate in the operation. If yes, proceed to step B1.12; otherwise, return to step B1.
8. B1.
12. Record that the polygon has been used in the calculation; B1.
13. Denote pi as the new parent node and proceed to step B1.
5.
6. The method for calculating the effective area of the identification region according to claim 5, characterized in that, Step B2 is performed as follows: B2.1 Obtain the vertex position data and pointing region declaration of simple polygons p1 and p2 with holes; B2.2 Remove vertices that are too close to the next point or lie on the line connecting the two points; B2.3 Correct the vertex order to match the declared region; B2.4 Calculate the intersection points, classify the intersection points, and add the valid intersection points to the intersection point set S1; B2.
5. Traverse the points in the intersection set S1 and generate the calculation results according to the following rules: Step B3 is performed as follows: B3.
1. Obtain an unvisited intersection point D0 from the intersection point set S1, denoted as D=D0, and record that D0 has been visited. Add an empty polygon P to the result set S, and add the first vertex D0 to P. B3.2 Determine if point D0 exists. If yes, proceed to step B3.3; otherwise, output the result set. B3.3, Query the intersection type of D; B3.4 When the intersection type of D is an outgoing point, proceed to step B3.5; B3.
5. Let D1 be the next right polygon point after D; B3.6 Determine if D is D0. If yes, return to step B3.1; otherwise, proceed to step B3.
7. B3.7 Record that D has been visited, add D to the end of P, and then return to step B3.3; B3.8 When the intersection type of D is non-intersection, determine whether D is in the left polygon. If yes, proceed to step B3.9; otherwise, proceed to step B3.
5. B3.
9. Let D2 be the next left polygon point of D, and proceed to step B3.6; B3.10 When the intersection type of D is an in-point, proceed to step B3.9; B2.6 Calculate the top-end order of the polygons in the calculation result, determine their pointing areas, and output the calculation result.
7. A device for calculating the effective area of a recognition region, characterized in that, For implementing a method for calculating the effective area of an identified region as described in any one of claims 1 to 6, the apparatus comprises: The acquisition module is used to acquire vertex data and defect types of the recognition region set output by the neural network model; The first calculation module is used to calculate the intersection between pairs of regions and generate an intersection map of the regions; The fusion module is used to quickly fuse and generate a set of non-overlapping regions. During the fusion process, vertex merging is performed on temporarily generated regions. The second calculation module is used to calculate the clockwise or counterclockwise direction of the boundary vertices of the generated region set; The statistics module is used to calculate the percentage of defective areas in the overall image.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of a method for calculating the effective area of an identification region as described in any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of a method for calculating the effective area of an identification region as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Method for detecting airplane skin pit defects under different scales based on point cloud data
CN116934678A
Method and device for calculating effective area of identification region, equipment and medium
CN118096860A