Area calculation method and system based on image processing
Through the area calculation method based on image processing, combined with OpenCV's pixel traversal algorithm and perspective transformation algorithm, the problems of low measurement area accuracy, high cost and low efficiency in the prior art are solved, efficient and accurate area measurement is achieved, and potential harm to the structure is reduced.
Patent Information
- Application Number
- CN202510128212.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-05
- Publication Date
- 2025-03-04
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
When measuring the area of the apparent special area of the structure to be measured, the prior art has the disadvantages of low accuracy, high hardware cost, relying on special equipment, low efficiency and inability to solve the problem of horizontal and vertical axis angle.
The area calculation method based on image processing is adopted, by calculating the ratio of image pixel area to actual area, combining with the pixel traversal algorithm in OpenCV, the area calculation is realized, and image correction is performed through perspective transformation algorithm to solve the impact of shooting angle and intercept size on area calculation.
It significantly improves detection efficiency, reduces hardware costs, simplifies the detection process, improves measurement accuracy, and has no potential harm to the structure.
Smart Images

Figure CN119559242A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of general measurement, and in particular to an area calculation method and system based on image processing. Background Art
[0002] In the field of general measurement technology, the area measurement of special apparent areas of the measured structure (such as the honeycomb surface area of concrete structures, waterlogging area of roads, surface stains area of photovoltaic panels, etc.) is related to the overall quality assessment of the measured structure and subsequent maintenance and repair work, and therefore receives special attention in this technical field.
[0003] With the rapid development of computer image processing technology and artificial intelligence, area detection algorithms based on image processing technology emerge in an endless stream. The related traditional detection methods and existing algorithms have the following shortcomings: 1. The traditional on-site ruler and compass measurement method is prone to missing items, is troublesome to re-measure, and has low measurement accuracy and reliability.
[0004] 2. Related core algorithms (such as the YOLO model) use professional depth cameras or binocular cameras for non-contact measurement, which results in high hardware measurement costs, reliance on special equipment, and low efficiency.
[0005] 3. General image measurement equipment cannot solve the problem of the angle between the horizontal and vertical axes, resulting in the photographed image being smaller than the actual image. Although the shooting angle can be corrected through image processing methods such as perspective transformation algorithms, the accuracy of the correction result cannot be guaranteed.
[0006] In view of the above, this application is hereby filed. Summary of the invention
[0007] In response to the above problems, an embodiment of the present invention provides an area calculation method and system based on image processing, which adopts a standard pixel algorithm and calls the pixel traversal algorithm in OpenCV to realize the area calculation and uses non-destructive testing methods to complete most of the workload. The instruments and equipment used are lightweight and the testing process is simple, which significantly improves the testing efficiency and has no potential harm to the structure.
[0008] The present invention is achieved through the following technical solutions: First aspect The embodiment of the present invention provides an area calculation method based on image processing, which is characterized by comprising the following steps: S1: Calculate the ratio of image pixel area to actual area and obtain the actual pixel area S p ; S2: Determine the number of pixels in the detection area based on the traversal algorithm N ; S3: Calculate the actual area of the detection area S satisfy: S=S p *N .
[0009] In this solution, the area calculation method is based on image processing. By calculating the ratio of the image pixel area to the actual area, the actual pixel area is obtained. S p ; and determine the number of pixels in the detection area based on the traversal algorithm N ; By number of pixels N And the actual size of the pixel S p To calculate the actual area of the region, compared with the existing technology, this solution adopts a standard pixel algorithm and calls the pixel traversal algorithm in OpenCV to realize the area calculation. Non-destructive testing methods are used to complete most of the workload. The instruments and equipment used are lightweight and the testing process is simple, which significantly improves the testing efficiency and has no potential harm to the structure.
[0010] Furthermore, in step S1, it also includes implementing image correction based on perspective transformation.
[0011] Furthermore, the image correction comprises the following steps: S11: Obtain a square identification block image dataset, where the side length of the identification block is L ; S12: Train the identification block recognition model based on the YOLOv8 model; S13: Based on the identification block recognition model, identify a square identification block and determine the coordinates of four vertices of the identification block; S14: Based on the coordinates of the four vertices of the identification block, a perspective transformation algorithm is used to correct the identification block in the image into a rectangle, and a transformation matrix is determined. H ; S15: Based on the transformation matrix H Perform matrix transformation on the image to obtain the actual side length of the corrected image w 实 And based on the actual side length w 实 Calculate the actual pixel area size S p ,in S p =L 2 / w 实 2 .
[0012] Furthermore, in step S2, the number of pixels NThe confirmation also includes a region closing step, wherein the region closing step specifically includes: S21: Extract the contour of the selected area in the image based on findContours; S22: Determine the closedness of the area contour, based on which it is determined whether the area contour is a closed contour, an open contour or a line redundant contour, wherein the line redundant contour is an extension segment after the area contour is closed; and close the open contour or the line redundant contour.
[0013] Furthermore, in step S2, the number of pixels N The confirmation also includes the following steps: S23: Based on the closed contour space, determine the coordinate values of the highest and lowest points of the closed area and determine the traversal interval ; S24: The traversal function is the starting point, along y Axis traversal, confirm the number of intersections between the traversal function and the contour , where i is the number of traversals; If the number of intersections between the traversal function and the contour is an even number, take the number of pixels in the area on the traversal function ,in for: ; If the number of intersections between the traversal function and the contour is an odd number and not less than 3, the following steps are included: S241: Confirm the horizontal coordinate of the intersection point and create a horizontal coordinate array ; S242: Find the last time during traversal With next time The number of intersections during traversal , , and confirm its horizontal coordinate value, and establish the coordinate array respectively: , ; S243: Take out The coordinate values in the array are judged as a group of 2 coordinate values, specifically: S2431: Take the first set of coordinate values, i.e. , , ; S2432: When Value The range and exist When in range, confirm is a valid intersection point, otherwise it is a single intersection point, and pixel calculation is performed based on the valid intersection point; S2433: processing subsequent intersection points in sequence based on steps S2431 and S2433 to obtain the coordinate values of the intersection points involved in pixel calculation; S244: Calculate the number of pixels in the area , ; S25: Through the traversal function, traverse all pixels in the area to confirm the total number of pixels. The calculation formula is: .
[0014] Further, in step S22, if the area contour is an opening contour, the following steps are performed: S221: Confirmation of the final starting point and final ending point; S222: Outlining based on the determined final starting point and the final ending point; The final starting point and final ending point confirmation includes the following steps: S2211: Find the two nearest endpoints of the opening area, which are defined as the starting point B and whose coordinate values on the image are , the end point E and the coordinate value on the image ; S2212: Find n points along the area contour that are n away from the starting point B. Pixel width in pixels , and determine the pixel point Coordinates , and calculate the slope of the line connecting adjacent pixels ; Find the distance n from the end point E Pixel width in pixels , and determine the pixel point Coordinates , and calculate the slope of the line connecting adjacent pixels ; S2213: Calculate adjacent starting points The slope change between , to determine whether the change amount has changed significantly, the change threshold is , calculate the adjacent end points The slope change between , to determine whether the change amount has changed significantly, the change threshold is ; S2214: Determine the new starting point and end point, based on: Not Exceeded , then the starting point remains unchanged, such as the change There is a value greater than , then take is the new starting point; if the change Not Exceeded , then the starting point remains unchanged, such as the change There is a value greater than , then take For the new starting point.
[0015] Furthermore, the outline drawing in step S222 specifically includes the following steps: S2221: Based on the coordinates of the final starting point and the final ending point, find the midpoint position between the final starting point and the final ending point of the opening area ; S2222: Connect the final starting point and the midpoint and calculate the slope ; Connect the final end point and the midpoint and calculate the slope ; Find the final starting point and the distance from the final starting point Pixels The slope of the connecting line is the reference slope ; Find the final end point and the distance from the final starting point Pixels The slope of the connecting line is the reference slope ; S2223: Outlining the contour between the final starting point and the midpoint by changing the slope, specifically: confirming that the slope change interval is ; Confirm the minimum slope transformation value, the formula is as follows:
[0016] According to the minimum slope transformation value, find the next point outlined from the final starting point to the midpoint, where the next point x The coordinates of the axis are the distance from the starting point The value of the pixel, y The axis passes through the minimum slope transformation value and x The coordinates are confirmed and the outline is drawn. The outline width is ; Between the final end point and the midpoint, the contour is outlined by changing the slope. Specifically: confirm that the slope change interval is ; Confirm the minimum slope transformation value, the formula is as follows:
[0017] According to the minimum slope transformation value, find the next point of the final termination point toward the midpoint, where the next point xThe coordinates of the axis are the distance from the end point The pixel value, the y-axis is confirmed by the minimum slope transformation value and the x-coordinate, and the outline is outlined, and the outline width is ; S2224: Repeat step S2223 until the outline of the final starting point and the midpoint and the outline of the final ending point and the midpoint are completed.
[0018] Furthermore, in step S22, if the area contour is a line redundant contour, the following steps are performed: Repeat the operation of closing the circled area, first closing the redundant parts of the lines into small areas; Use cv2.contourArea function to find the area of each region , ,in is the area of the small region after the line is closed, is the target area.
[0019] Calculate the area ratio of 2 regions Z , determine whether to discard the small area after the line is closed, the judgment criteria are: Z <10%, discard the small area, otherwise the area to be calculated is .
[0020] Second aspect The embodiment of the present invention further provides an area calculation system based on image processing, comprising: An acquisition module, the acquisition module is used to acquire an image of the area to be measured; A processing module, wherein the processing module completes the area calculation of the regional image based on the above-mentioned area calculation method based on image processing.
[0021] The third aspect An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the above-mentioned area calculation method based on image processing are implemented.
[0022] Compared with the prior art, the present invention has the following advantages and beneficial effects: The embodiment of the present invention provides an area calculation method and system based on image processing. The area calculation method is based on image processing, and obtains the actual area size of the pixel by calculating the ratio of the image pixel area to the actual area. S p ; and determine the number of pixels in the detection area based on the traversal algorithm N ; By number of pixels N And the actual size of the pixel Sp To calculate the actual area of the region, compared with the existing technology, this solution adopts a standard pixel algorithm and calls the pixel traversal algorithm in OpenCV to realize the area calculation. Non-destructive testing methods are used to complete most of the workload. The instruments and equipment used are lightweight and the testing process is simple, which significantly improves the testing efficiency and has no potential harm to the structure. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. It should be understood that the following drawings only illustrate certain embodiments of the present invention and should not be regarded as limiting the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative work: Figure 1 A flowchart of an area calculation method based on image processing provided by an embodiment of the present invention; Figure 2 A schematic diagram of a detection frame provided by an embodiment of the present invention; Figure 3 Schematic diagram of three contour areas provided by embodiments of the present invention; Figure 4 A schematic diagram of valid intersection points provided by an embodiment of the present invention; Figure 5 A schematic diagram of confirming fitting points when outlining a contour provided by an embodiment of the present invention; Figure 6 A schematic diagram of line redundancy processing provided by an embodiment of the present invention; Figure 7 A flowchart of an area calculation method based on image processing is provided for an embodiment of the present invention to implement area calculation. DETAILED DESCRIPTION
[0024] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0025] In the following description, a large number of specific details are set forth in order to provide a thorough understanding of the present invention. However, it is apparent to one of ordinary skill in the art that these specific details are not necessarily employed to practice the present invention. In other instances, well-known structures, circuits, materials, or methods are not specifically described in order to avoid obscuring the present invention.
[0026] Throughout the specification, references to "one embodiment," "an embodiment," "an example," or "an example" mean that a particular feature, structure, or characteristic described in conjunction with the embodiment or example is included in at least one embodiment of the present invention. Therefore, the phrases "one embodiment," "an embodiment," "an example," or "an example" appearing in various places throughout the specification do not necessarily all refer to the same embodiment or example. In addition, particular features, structures, or characteristics may be combined in one or more embodiments or examples in any suitable combination and / or sub-combination. In addition, it will be appreciated by those of ordinary skill in the art that the figures provided herein are for illustrative purposes and that the figures are not necessarily drawn to scale. The term "and / or" as used herein includes any and all combinations of one or more of the associated listed items.
[0027] Example like Figure 1 As shown, an embodiment of the present invention provides an area calculation method based on image processing, which is characterized by comprising the following steps: S1: Calculate the ratio of image pixel area to actual area and obtain the actual pixel area S p ; S2: Determine the number of pixels in the detection area based on the traversal algorithm N ; S3: Calculate the actual area of the detection area S satisfy: S=S p *N .
[0028] In this solution, the area calculation method is based on image processing. By calculating the ratio of the image pixel area to the actual area, the actual pixel area is obtained. S p ; and determine the number of pixels in the detection area based on the traversal algorithm N ; By number of pixels N And the actual size of the pixel S p To calculate the actual area of the region, compared with the existing technology, this solution adopts a standard pixel algorithm and calls the pixel traversal algorithm in OpenCV to realize the area calculation. Non-destructive testing methods are used to complete most of the workload. The instruments and equipment used are lightweight and the testing process is simple, which significantly improves the testing efficiency and has no potential harm to the structure.
[0029] In some embodiments, step S1 further includes implementing image correction based on perspective transformation.
[0030] Specifically, the image can be corrected through the perspective change, thereby reducing the influence of factors such as shooting angle and interception size on the actual area size of each pixel.
[0031] In some embodiments, the image correction comprises the following steps: S11: Obtain a square identification block image dataset, where the side length of the identification block is L ; S12: Train the identification block recognition model based on the YOLOv8 model; S13: Based on the identification block recognition model, identify a square identification block and determine the coordinates of four vertices of the identification block; S14: Based on the coordinates of the four vertices of the identification block, a perspective transformation algorithm is used to correct the identification block in the image into a rectangle, and a transformation matrix is determined. H ; S15: Perform matrix transformation on the image based on the transformation matrix H to obtain the actual side length of the corrected image w 实 And based on the actual side length w 实 Calculate the actual pixel area size S p ,in S p =L 2 / w 实 2 .
[0032] Specifically, the YOLOv8 is a conventional open source model, which selects an identification block through a model frame and records relevant coordinates, and calculates the actual area size of each pixel after completing perspective transformation through the coordinates.
[0033] like Figure 2 As shown, specifically, in step S13, the identification block recognition model identifies the square identification block and provides the detection frame and coordinate information of the square identification block, including the center coordinates of the detection frame. , the width of the detection frame and height , the coordinates of the four vertices of the detection frame are: 、 、 、 .
[0034] Specifically, in step S14, those skilled in the art should know that the general transformation formula of the perspective transformation algorithm is: ; in, is the original image coordinate, take the coordinates of the four vertices of the detection box, , , , ; is the transformed coordinate, which is taken according to the actual physical size of the marker block, taking (0, 0), ( L,0 )、( L,L )、( 0,L ).
[0035] Obtain the transformation matrix through the corresponding situations of the four vertices H There are 8 unknowns in
[0036] in is the rotation amount, is the translation amount.
[0037] After obtaining H, the image is corrected by perspective transformation. H Correct the image.
[0038] In some embodiments, in step S2, the number of pixels N The confirmation also includes a region closing step, wherein the region closing step specifically includes: S21: Extract the contour of the selected area in the image based on findContours; S22: Determine the closedness of the area contour, based on which it is determined whether the area contour is a closed contour, an open contour or a line redundant contour, wherein the line redundant contour is an extension segment after the area contour is closed; and close the open contour or the line redundant contour.
[0039] Specifically, Figure 3 The following are schematic diagrams of three areas.
[0040] In some embodiments, in step S2, the number of pixels N The confirmation also includes the following steps: S23: Based on the closed contour space, determine the coordinate values of the highest and lowest points of the closed area and determine the traversal interval ; S24: The traversal function is the starting point, along y Axis traversal, confirm the number of intersections between the traversal function and the contour , where i is the number of traversals; If the number of intersections between the traversal function and the contour is an even number, take the number of pixels in the area on the traversal function ,in for: ; If the number of intersections between the traversal function and the contour is an odd number and not less than 3, the following steps are included: S241: Confirm the horizontal coordinate of the intersection point and create a horizontal coordinate array ; S242: Find the last time during traversal With next time The number of intersections during traversal , , and confirm its horizontal coordinate value, and establish the coordinate array respectively: , ; S243: Take out The coordinate values in the array are judged as a group of 2 coordinate values, specifically: S2431: Take the first set of coordinate values, i.e. , , ; S2432: When Value The range and exist When in range, confirm is a valid intersection point, such as Figure 4 As shown, otherwise it belongs to a single intersection point, and pixel calculation is performed based on the valid intersection point; S2433: processing subsequent intersection points in sequence based on steps S2431 and S2433 to obtain the coordinate values of the intersection points involved in pixel calculation; S244: Calculate the number of pixels in the area , ; S25: Through the traversal function, traverse all pixels in the area to confirm the total number of pixels. The calculation formula is: .
[0041] Among them, those skilled in the art should know that the traversal algorithm is used for calculation. y =0 function is the initial traversal function, along y Axis traversal, confirm the number of intersections between the traversal function and the area outline, find the traversal function with the first and last intersections of 1, and its function value is , .
[0042] In some embodiments, in step S22, if the area contour is an opening contour, the following steps are performed: S221: Confirmation of the final starting point and final ending point; S222: Outlining based on the determined final starting point and the final ending point; The final starting point and final ending point confirmation includes the following steps: S2211: Find the two nearest endpoints of the opening area, which are defined as the starting point B and whose coordinate values on the image are , the end point E and the coordinate value on the image ; S2212: Find n points along the area contour that are n away from the starting point B. Pixel width in pixels , and determine the pixel point Coordinates , and calculate the slope of the line connecting adjacent pixels ; Find the distance n from the end point E Pixel width in pixels , and determine the pixel point Coordinates , and calculate the slope of the line connecting adjacent pixels ; S2213: Calculate adjacent starting points The slope change between , to determine whether the change amount has changed significantly, the change threshold is , calculate the adjacent end points The slope change between , to determine whether the change amount has changed significantly, the change threshold is ; S2214: Determine the new starting point and end point, based on: Not Exceeded , then the starting point remains unchanged, such as the change There is a value greater than , then take is the new starting point; if the change Not Exceeded , then the starting point remains unchanged, such as the change There is a value greater than , then take For the new starting point.
[0043] Specifically, in step S2212, in order to facilitate the calculation and reduce the amount of calculation, 10 points are selected and recorded as ; Among them, the starting point B and , and and Slope of connection line ,based on Perform calculations.
[0044] Furthermore, in order to ensure the accuracy of the positions of the starting point and the ending point and reduce the impact of bending near the position, the above steps S2211~S2214 are repeated to ensure accuracy. In some specific embodiments, the number of repetitions is not less than 5 times.
[0045] In some embodiments, the outline drawing in step S222 specifically includes the following steps: S2221: Based on the coordinates of the final starting point and the final ending point, find the midpoint position between the final starting point and the final ending point of the opening area ; S2222: Connect the final starting point and the midpoint and calculate the slope ; Connect the final end point and the midpoint and calculate the slope ; Find the final starting point and the distance from the final starting point Pixels The slope of the connecting line is the reference slope ; Find the final end point and the distance from the final starting point Pixels The slope of the connecting line is the reference slope ; S2223: Outlining the contour between the final starting point and the midpoint by changing the slope, specifically: confirming that the slope change interval is ; Confirm the minimum slope transformation value, the formula is as follows:
[0046] According to the minimum slope transformation value, find the next point outlined from the final starting point to the midpoint, where the next point x The coordinates of the axis are the distance from the starting point The value of the pixel, y The axis passes through the minimum slope transformation value and x The coordinates are confirmed and the outline is drawn. The outline width is ; Specifically, as those skilled in the art should know, when confirming the outline point, the slope is calculated by the above formula, and the corresponding left side of the X-axis is known, and the Y-axis coordinate can be confirmed, such as Figure 5 As shown, taking the starting point as an example, the slope k after connecting the starting point and the midpoint is m1 , then the starting point is close to it The slope of the contour distance point of pixel value is k b0 , confirm the new slope through the formula, the new slope , and then determine the ordinate y using the new slope k, the starting point coordinates, and the x-axis coordinates of the outlined points.
[0047] Specifically, in some embodiments, the coordinates of the center points of the outline of the starting point and the midpoint are: Point 1: ; The second point: ; The nth point: ;
[0048] Between the final end point and the midpoint, the contour is outlined by changing the slope. Specifically: confirm that the slope change interval is ; Confirm the minimum slope transformation value, the formula is as follows:
[0049] According to the minimum slope transformation value, find the next point of the final termination point toward the midpoint, where the next point x The coordinates of the axis are the distance from the end point The pixel value, the y-axis is confirmed by the minimum slope transformation value and the x-coordinate, and the outline is outlined, and the outline width is ; Specifically, in some embodiments, the coordinates of the center points of the outline of the end point and the midpoint are: Point 1: ; The second point: ; The nth point: ; S2224: Repeat step S2223 until the outline of the final starting point and the midpoint and the outline of the final ending point and the midpoint are completed.
[0050] like Figure 6 As shown, in some embodiments, in step S22, if the area contour is a line redundant contour, the following steps are performed: Repeat the operation of closing the circled area, first closing the redundant parts of the lines into small areas; Specifically, the redundant parts of the lines are closed into small areas. The specific closing method is the same as the closing method for the opening contour mentioned above, which will not be described in detail here.
[0051] Use cv2.contourArea function to find the area of each region , ,in is the area of the small region after the line is closed, is the target area.
[0052] Calculate the area ratio of 2 regions Z , determine whether to discard the small area after the line is closed, the judgment criteria are: Z <10%, discard the small area, otherwise the area to be calculated is .
[0053] Among them, those skilled in the art should know that the region closing step is used to realize the region closing, and the area of the region to be calculated is clarified, so as to facilitate the subsequent realization of the number of pixels in the region. Calculation.
[0054] In a specific embodiment, Figure 7 As shown, when performing specific area calculations: Input the identification block recognition model to confirm the identification block coordinate information; Correct the image through perspective transformation to obtain new identification block coordinate information; Calculate the actual area of the item in the image based on the coordinate information of the new identification block ; Close the detection area; Use the traversal function to count the number of pixels in the closed area ; Calculate the actual area in the region ; The embodiment of the present invention further provides an area calculation system based on image processing, comprising: An acquisition module, the acquisition module is used to acquire an image of the area to be measured; A processing module, wherein the processing module completes the area calculation of the regional image based on the above-mentioned area calculation method based on image processing.
[0055] An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the above-mentioned area calculation method based on image processing are implemented.
[0056] In the embodiments of the present invention, the processor may be an integrated circuit chip having the ability to process signals. The processor may be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0057] The methods, steps and logic block diagrams disclosed in the embodiments of the present invention can be implemented or executed. The general processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present invention can be directly embodied as a hardware decoding processor for execution, or can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The processor reads the information in the storage medium and completes the steps of the above method in combination with its hardware.
[0058] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for calculating area based on image processing, characterized in that: The steps include: S1: Calculate the ratio of image pixel area to actual area and obtain the actual pixel area S p ; S2: Determine the number of pixels in the detection area based on the traversal algorithm N ; S3: Calculate the actual area of the detection area S satisfy: S=S p *N .
2. The area calculation method based on image processing according to claim 1, characterized in that: In step S1, it also includes implementing image correction based on perspective transformation.
3. The area calculation method based on image processing according to claim 2, characterized in that: The image correction comprises the following steps: S11: Obtain a square identification block image dataset, where the side length of the identification block is L ; S12: Train the identification block recognition model based on the YOLOv8 model; S13: Based on the identification block recognition model, identify a square identification block and determine the coordinates of four vertices of the identification block; S14: Based on the coordinates of the four vertices of the identification block, a perspective transformation algorithm is used to correct the identification block in the image into a rectangle, and a transformation matrix is determined. H ; S15: Based on the transformation matrix H Perform matrix transformation on the image to obtain the actual side length of the corrected image w 实 And based on the actual side length w 实 Calculate the actual pixel area size S p ,in S p =L 2 / w 实 2 .
4. The area calculation method based on image processing according to claim 1, characterized in that: In step S2, the number of pixels N The confirmation also includes a region closing step, wherein the region closing step specifically includes: S21: Extract the contour of the selected area in the image based on findContours; S22: Determine the closedness of the area contour, based on which it is determined whether the area contour is a closed contour, an open contour or a line redundant contour, wherein the line redundant contour is an extension segment after the area contour is closed; and close the open contour or the line redundant contour.
5. The area calculation method based on image processing according to claim 4, characterized in that: In step S2, the number of pixels N The confirmation also includes the following steps: S23: Based on the closed contour space, determine the coordinate values of the highest and lowest points of the closed area and determine the traversal interval ; S24: The traversal function is the starting point, along y Axis traversal, confirm the number of intersections between the traversal function and the contour , where i is the number of traversals; If the number of intersections between the traversal function and the contour is an even number, take the number of pixels in the area on the traversal function ,in for: ; If the number of intersections between the traversal function and the contour is an odd number and not less than 3, the following steps are included: S241: Confirm the horizontal coordinate of the intersection point and create the horizontal coordinate array ; S242: Find the last time during traversal With next time The number of intersections during traversal , , and confirm its horizontal coordinate value, and establish the coordinate array respectively: 、 ; S243: Take out The coordinate values in the array are judged as a group of 2 coordinate values, specifically: S2431: Take the first set of coordinate values, i.e. , , ; S2432: When Value The range and exist When within range, confirm is a valid intersection point, otherwise it is a single intersection point, and pixel calculation is performed based on the valid intersection point; S2433: processing subsequent intersection points in sequence based on steps S2431 and S2433 to obtain the coordinate values of the intersection points involved in pixel calculation; S244: Calculate the number of pixels in the area , ; S25: Through the traversal function, traverse all pixels in the area to confirm the total number of pixels. The calculation formula is: 。 6. The area calculation method based on image processing according to claim 4, characterized in that: In step S22, if the area contour is an opening contour, the following steps are performed: S221: Confirmation of the final starting point and final ending point; S222: Outlining based on the determined final starting point and the final ending point; The final starting point and final ending point confirmation includes the following steps: S2211: Find the two nearest endpoints of the opening area, which are defined as the starting point B and whose coordinate values on the image are , the end point E and the coordinate value on the image ; S2212: Find n points along the area contour that are n away from the starting point B. Pixel width in pixels , and determine the pixel point Coordinates , and calculate the slope of the line connecting adjacent pixels ; Find the distance n from the end point E Pixel width in pixels , and determine the pixel point Coordinates , and calculate the slope of the line connecting adjacent pixels ; S2213: Calculate adjacent starting points The slope change between , to determine whether the change amount has changed significantly, the change threshold is , calculate the adjacent end points The slope change between , to determine whether the change amount has changed significantly, the change threshold is ; S2214: Determine the new starting point and end point, based on: Not Exceeded , then the starting point remains unchanged, such as the change There is a value greater than , then take is the new starting point; if the change Not Exceeded , then the starting point remains unchanged, such as the change There is a value greater than , then take For the new starting point.
7. The area calculation method based on image processing according to claim 6, characterized in that: In step S222, the outline drawing specifically includes the following steps: S2221: Based on the coordinates of the final starting point and the final ending point, find the midpoint position between the final starting point and the final ending point of the opening area ; S2222: Connect the final starting point and the midpoint and calculate the slope ; Connect the final end point and the midpoint and calculate the slope ; Find the final starting point and the distance from the final starting point Pixels The slope of the connecting line is the reference slope ; Find the final end point and the distance from the final starting point Pixels The slope of the connecting line is the reference slope ; S2223: Outlining the contour between the final starting point and the midpoint by changing the slope, specifically: confirming that the slope change interval is ; Confirm the minimum slope transformation value, the formula is as follows: ; According to the minimum slope transformation value, find the next point outlined from the final starting point to the midpoint, where the next point x The coordinates of the axis are the distance from the starting point The value of the pixel, y The axis passes through the minimum slope transformation value and x The coordinates are confirmed and the outline is drawn. The outline width is ; Between the final end point and the midpoint, the contour is outlined by changing the slope. Specifically: confirm that the slope change interval is ; Confirm the minimum slope transformation value, the formula is as follows: ; According to the minimum slope transformation value, find the next point of the final termination point toward the midpoint, where the next point x The coordinates of the axis are the distance from the end point The value of the pixel, the y-axis is confirmed by the minimum slope transformation value and the x-coordinate, and the outline is outlined, and the outline width is ; S2224: Repeat step S2223 until the outline of the final starting point and the midpoint and the outline of the final ending point and the midpoint are completed.
8. The area calculation method based on image processing according to claim 7, characterized in that: In step S22, if the area contour is a line redundant contour, the following steps are performed: Repeat the operation of closing the circled area, first closing the redundant parts of the lines into small areas; Use cv2.contourArea function to find the area of each region , ,in is the area of the small region after the line is closed, is the target area; Calculate the area ratio of 2 regions Z , determine whether to discard the small area after the line is closed, the judgment criteria are: Z <10%, discard the small area, otherwise the area to be calculated is .
9. An area calculation system based on image processing, characterized in that: include: An acquisition module, the acquisition module is used to acquire an image of the area to be measured; A processing module, wherein the processing module completes the area calculation of the regional image based on the area calculation method based on image processing described in any one of claims 1-8.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the area calculation method based on image processing described in any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Vector image generation method, image processing apparatus, and computer-readable storage medium for computer program
US20090315892A1
Method for measuring actual area of defect, and method and apparatus for testing display panel
US20230401692A1
Algorithm for filling an image outline
US4815009A
Apparatus and method for extracting outline data and encoding image data using the outline data
US5748777A