Image processing-based parallel line distance calculation method, terminal and medium

By using threshold segmentation and weighted least squares method to fit straight lines, the problem of large error, complex calibration and insufficient robustness in the calculation of parallel line spacing in the prior art is solved, realizing high-precision and simple parallel line spacing measurement, which is suitable for automated detection in a variety of scenarios.

CN122335945APending Publication Date: 2026-07-03WUHAN TEXTILE UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
WUHAN TEXTILE UNIV
Filing Date
2026-05-12
Publication Date
2026-07-03

AI Technical Summary

Technical Problem

Existing methods for calculating the spacing between parallel lines based on image processing suffer from problems such as large distance calculation errors due to perspective distortion, complex calibration and poor versatility, insufficient robustness, and difficulty in balancing accuracy and real-time performance, which cannot meet the needs of industrial online inspection and vehicle-mounted dynamic measurement.

Method used

The method employs threshold segmentation to obtain parallel line regions, fits straight lines using weighted least squares, and calculates the spacing between parallel lines by combining slope screening and the shortest distance principle. This simplifies the calibration process and improves the robustness and accuracy of the method.

Benefits of technology

It achieves stable, accurate, and simple parallel line spacing calculation, and is suitable for high-precision and automated measurement in scenarios such as industrial precision inspection, intelligent transportation, surveying and mapping, security and robot vision.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122335945A_ABST
    Figure CN122335945A_ABST
Patent Text Reader

Abstract

This invention discloses a method, terminal, and medium for calculating the distance between parallel lines based on image processing. The method includes: S1, performing threshold segmentation on the image to obtain the regions corresponding to the two parallel lines; S2, representing the regions corresponding to the two parallel lines using two point sets; S3, calculating the variance of the two point sets respectively, with the point set with the smaller variance called the first point set and the point set with the larger variance called the second point set; S4, for the first point set, performing line fitting using weighted least squares, and the fitted line is called the first line; S5, based on the slope of the first line, filtering the points in the second point set, and the resulting point set is called the third point set; S6, based on the third point set, fitting a second line, the second line having the same slope as the first line, and satisfying that the sum of the distances from all points in the third point set to the second line is the shortest; S7, calculating the distance between the first line and the second line as the distance between the two parallel lines.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of image processing technology, and more specifically to a method for calculating the distance between parallel lines based on image processing. Background Technology

[0002] In fields such as industrial production, intelligent transportation, architectural surveying, and precision assembly, the spacing between parallel lines is a key geometric parameter that directly determines product quality, operational safety, and measurement accuracy. For example, the width of grooves in industrial parts, track spacing, parallelism of sheet metal, lane line width, and PCB trace spacing all require rapid and accurate measurement. Traditional contact-based measurement relies on tools such as calipers, micrometers, and plug gauges, which suffer from drawbacks such as low efficiency, easy scratching of workpieces, inability to perform online dynamic measurement, and difficulty in adapting to small or large components. Physical ranging devices based on lasers and ultrasound are costly, complex to install, and have weak anti-interference capabilities.

[0003] Image processing-based visual measurement methods have become the mainstream technology due to their advantages such as being non-contact, fast, accurate, highly automated, and adaptable to complex scenarios. Existing technologies typically follow a process of: image acquisition → edge detection → line extraction → pixel distance calculation → physical distance conversion. Current mainstream methods include: line detection methods based on Hough transform / least square fitting, accuracy optimization methods based on sub-pixel edge localization, scale conversion methods based on camera calibration (checkerboard, calibration board), and 3D ranging methods based on binocular vision. However, these methods may have the following drawbacks:

[0004] Perspective distortion leads to large distance calculation errors: Monocular camera imaging has a perspective effect where near objects appear larger and distant objects appear smaller. Parallel lines in three-dimensional space tend to converge in the image. Directly converting physical distances according to a fixed pixel ratio will produce significant systematic errors and cannot truly reflect the actual distance.

[0005] The calibration is complex and the versatility is poor: high-precision measurement relies on the calibration of camera intrinsic and extrinsic parameters, requires a special calibration board, multiple shooting and offline calculation. After the camera moves or the angle changes, it needs to be recalibrated, which makes it difficult to meet the needs of online, mobile and calibration-free scenarios.

[0006] Insufficient robustness and susceptible to environmental interference: Under conditions such as uneven lighting, shadows, occlusion, blurring, and noise, edge extraction and line fitting are prone to failure, leading to line positioning offset and unstable spacing calculation.

[0007] Accuracy and real-time performance are difficult to balance: traditional sub-pixel algorithms and Hough transforms have high computational loads and cannot meet the requirements of high-speed detection; while simplified algorithms will lead to insufficient positioning accuracy.

[0008] In summary, existing methods for calculating parallel line spacing based on image processing cannot simultaneously achieve high precision, anti-interference, calibration-free operation, real-time performance, and versatility, and therefore cannot meet the actual needs of scenarios such as industrial online inspection, vehicle-mounted dynamic measurement, and field-based unsupported measurement. Summary of the Invention

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

[0010] A method for calculating the distance between parallel lines based on image processing includes the following steps:

[0011] S1. Perform threshold segmentation on the image to obtain the regions corresponding to the two parallel lines respectively;

[0012] S2. Represent the regions corresponding to two parallel lines using two point sets;

[0013] S3. Calculate the variance of the two point sets respectively. The point set with the smaller variance is called the first point set, and the point set with the larger variance is called the second point set.

[0014] S4. For the first set of points, use the weighted least squares method to fit a straight line. The fitted line is called the first straight line.

[0015] S5. Based on the slope of the first line, the points in the second point set are filtered, and the resulting point set is called the third point set.

[0016] S6. Based on the third point set, fit the second line. The slope of the second line is the same as that of the first line, and the sum of the distances from all points in the third point set to the second line is the shortest.

[0017] S7. Calculate the distance between the first line and the second line to determine the distance between the two parallel lines.

[0018] Furthermore, in step S1, the region corresponding to either of the two parallel lines is obtained as a strip region;

[0019] In step S2, for any strip-shaped region, the coordinates of the midpoints in the width direction are obtained along its length direction, and a corresponding point set is established based on these midpoint coordinates.

[0020] Further, in step S2, the two point sets are represented as S1 respectively: With S2: ;

[0021] In step S3, the mean of the two point sets is calculated using the following formula. and :

[0022] ;

[0023] ;

[0024] Then calculate the variance of each of the two point sets separately. and :

[0025] ;

[0026] .

[0027] Furthermore, in step S5, when filtering the points in the second point set, specifically:

[0028] The slope of the first straight line is used This means that the points in the second set are traversed, and for any point in the second set... If there exists another arbitrary point in the second point set The slope of the line formed by the two points. ,satisfy , If the preset slope threshold is used, then it is considered as a valid data retention point. and Otherwise delete point .

[0029] Furthermore, in step S6, when fitting the second straight line, the specific steps include the following:

[0030] S61. The slope of the second line to be fitted is the same as the slope of the first line. Same, therefore use The second line represents the undetermined intercept determined by the coordinates of the points in the third set of points. minimum value With the maximum value ,by As the initial value for the undetermined intercept, and set the step size s;

[0031] S62. Generate a line to be determined using the current value of the undetermined intercept, calculate the distance from each point in the third point set to the line to be determined, and then calculate the sum of all distances.

[0032] S63. Increase the value of the undetermined intercept by a step size. If the current value of the undetermined intercept is less than... Otherwise, proceed to step S64;

[0033] S64. Select the undetermined intercept corresponding to the minimum value of the sum of distances as the intercept of the second line, thereby determining the second line.

[0034] In another aspect, the present invention provides an electronic terminal, including a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the electronic terminal performs the above-described method for calculating the distance between parallel lines based on image processing.

[0035] In another aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, causes the processor to perform the above-described method for calculating the distance between parallel lines based on image processing.

[0036] Compared with existing technologies, the method for calculating the distance between parallel lines based on image processing provided by this invention can stably, accurately and easily calculate the distance between parallel lines. It is suitable for non-contact, high-precision and automated measurement of the distance between parallel lines in scenarios such as industrial precision inspection, intelligent transportation, surveying and mapping, security, and robot vision. Attached Figure Description

[0037] Figure 1 A flowchart illustrating a method for calculating the distance between parallel lines based on image processing, provided by the present invention;

[0038] Figure 2 This is a schematic diagram of the regions corresponding to the two parallel lines obtained in a specific embodiment;

[0039] Figure 3 The coordinate graphs corresponding to the two point sets obtained in the specific embodiment;

[0040] Figure 4 This is a schematic diagram of two parallel lines obtained through fitting in a specific embodiment. Detailed Implementation

[0041] To make the technical means, creative features, objectives and effects of this invention easier to understand, the following description, in conjunction with the accompanying drawings and specific embodiments, further explains how this invention is implemented.

[0042] Reference Figure 1 As shown, this invention provides a method for calculating the distance between parallel lines based on image processing, comprising the following steps:

[0043] S1. Perform thresholding on the image to obtain the regions corresponding to the two parallel lines. It's understandable that for either of the two parallel lines, the resulting image region after thresholding is not an ideal straight line, but rather a strip-shaped region. (Refer to...) Figure 2 As shown, in a specific embodiment, the regions corresponding to the two parallel lines may even overlap.

[0044] S2. Represent the regions corresponding to the two parallel lines using two point sets. Specifically, for any strip-shaped region, obtain the coordinates of multiple midpoints along its length and width, and establish corresponding point sets based on these midpoint coordinates. The two point sets are represented as S1: With S2: In a specific embodiment, the coordinate graphs corresponding to the two point sets are as follows: Figure 3 As shown.

[0045] S3. Calculate the variance of the two point sets respectively. The point set with the smaller variance is called the first point set, and the point set with the larger variance is called the second point set.

[0046] Specifically, the mean of the two point sets is first calculated using the following formula. and :

[0047] ;

[0048] ;

[0049] Then calculate the variance of each of the two point sets separately. and :

[0050] ;

[0051] .

[0052] S4. For the first set of points, use weighted least squares to fit a straight line. The fitted line is called the first straight line, and the slope of the first straight line is... express.

[0053] S5. Based on the slope of the first straight line, the points in the second point set are filtered, and the resulting point set is called the third point set. Specifically, the filtering of points in the second point set involves:

[0054] Traverse the points in the second point set. For any point in the second point set... If there exists another arbitrary point in the second point set The slope of the line formed by the two points. ,satisfy , If the preset slope threshold is used, then it is considered as a valid data retention point. and Otherwise delete point .

[0055] For example, when the specific algorithm is running, all n points in the second point set can be numbered first. During the algorithm process, the state of each point is divided into three types: valid point (indicating that the point has been added to the third point set through the selection), pending point (indicating that the state is undetermined), and deleted point (indicating that the point has not been added to the third point set due to failure to pass the selection).

[0056] Initially, all points are undetermined. During traversal, the points are traversed in numerical order, starting from the first undetermined point. Begin by judging in sequence. , … respectively with Whether the slope of the formed straight line meets the above requirements is determined in two cases. If there exists a point that meets the requirements, then it is determined. Points that meet the requirements are considered valid points; points that do not meet the requirements remain unchanged and are still undetermined. If other points are respectively... If the slopes of the lines formed do not meet the above requirements, then... Become a deletion point. These points will no longer participate in subsequent processing, while the status of other points remains unchanged and remains pending.

[0057] Next, let's start with the next undetermined point, which has the earliest number. Start by sequentially determining whether the number is greater than... Other points , … respectively with Does the slope of the formed straight line satisfy the above requirements? Similarly, if there exist points that satisfy the requirements, then determine... Points that meet the requirements are considered valid points; points that do not meet the requirements remain unchanged (they are either undetermined points or valid points already determined in previous calculations); if other points are respectively... If the slopes of the lines formed do not meet the above requirements, then... Become a deletion point. It will no longer participate in subsequent processing, while the status of other points remains unchanged.

[0058] This process continues until no more points remain to be determined, at which point the filtering is complete. All points in the second set have been divided into valid points and deleted points, and all valid points form the third set.

[0059] S6. Based on the third point set, fit a second straight line. The second straight line has the same slope as the first straight line, and the sum of the distances from all points in the third point set to the second straight line is minimized. The specific steps for fitting the second straight line are as follows:

[0060] S61. The slope of the second line to be fitted is the same as the slope of the first line. Same, therefore use The second line represents the undetermined intercept determined by the coordinates of the points in the third set of points. minimum value With the maximum value This process can be completed in advance in step S5, that is, in step S5, each time a valid point is determined, the coordinates of that point are substituted into the system. Calculate the corresponding intercept Once the value of the intercept is determined and all valid points are identified, the calculation of all corresponding intercepts is complete, thus determining the intercept. The minimum and maximum values. Use this as the initial value for the undetermined intercept and set the step size s. The step size s is a positive constant and can be set according to the accuracy requirements; the smaller the value of s, the higher the accuracy.

[0061] S62. Generate a line to be determined using the current value of the undetermined intercept, calculate the distance from each point in the third point set to the line to be determined, and then calculate the sum of all distances.

[0062] S63. Increase the value of the undetermined intercept by a step size (i.e., the new undetermined intercept equals the original undetermined intercept plus the step size). If the current value of the undetermined intercept is less than... If the condition is not met, return to step S62; otherwise, proceed to step S64.

[0063] S64. Select the undetermined intercept corresponding to the minimum sum of distances as the intercept of the second line, thereby determining the second line. In a specific embodiment, the two fitted lines are as follows: Figure 4 As shown.

[0064] S7. Calculate the distance between the first and second lines to determine the distance between the two parallel lines. It can be understood that after fitting two parallel lines, based on the principle that the distance between parallel lines is equal everywhere, take any point on one line and calculate its distance to the other line; this distance can then be used as the distance between the parallel lines.

[0065] In summary, the image processing-based method for calculating the distance between parallel lines provided by this invention can stably, accurately, and easily calculate the distance between parallel lines. It is suitable for non-contact, high-precision, and automated measurement of the distance between parallel lines in scenarios such as industrial precision inspection, intelligent transportation, surveying and mapping, security, and robot vision.

[0066] In another aspect, the present invention provides an electronic terminal, including a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the electronic terminal performs the above-described method for calculating the distance between parallel lines based on image processing.

[0067] In another aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, causes the processor to perform the above-described method for calculating the distance between parallel lines based on image processing.

[0068] Finally, it should be noted that the above description is only an 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 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 distance between parallel lines based on image processing, characterized in that, Includes the following steps: S1. Perform threshold segmentation on the image to obtain the regions corresponding to the two parallel lines respectively; S2. Represent the regions corresponding to two parallel lines using two point sets; S3. Calculate the variance of the two point sets respectively. The point set with the smaller variance is called the first point set, and the point set with the larger variance is called the second point set. S4. For the first set of points, use the weighted least squares method to fit a straight line. The fitted line is called the first straight line. S5. Based on the slope of the first line, the points in the second point set are filtered, and the resulting point set is called the third point set. S6. Based on the third point set, fit the second line. The slope of the second line is the same as that of the first line, and the sum of the distances from all points in the third point set to the second line is the shortest. S7. Calculate the distance between the first line and the second line to determine the distance between the two parallel lines.

2. The method for calculating the distance between parallel lines based on image processing according to claim 1, characterized in that, In step S1, the region corresponding to either of the two parallel lines is obtained as a strip region; In step S2, for any strip-shaped region, the coordinates of the midpoints in the width direction are obtained along its length direction, and a corresponding point set is established based on these midpoint coordinates.

3. The method for calculating the distance between parallel lines based on image processing according to claim 1, characterized in that, In step S2, the two point sets are represented as S1: With S2: ; In step S3, the mean of the two point sets is calculated using the following formula. and : ; ; Then calculate the variance of each of the two point sets separately. and : ; 。 4. The method for calculating the distance between parallel lines based on image processing according to claim 1, characterized in that, In step S5, when filtering the points in the second point set, specifically: The slope of the first straight line is used This means that the points in the second set are traversed, and for any point in the second set... If there exists another arbitrary point in the second point set The slope of the line formed by the two points. ,satisfy , If the preset slope threshold is used, then it is considered as a valid data retention point. and Otherwise delete point .

5. The method for calculating the distance between parallel lines based on image processing according to claim 4, characterized in that, In step S6, when fitting the second straight line, the specific steps include the following: S61. The slope of the second line to be fitted is the same as the slope of the first line. Same, therefore use The second line represents the undetermined intercept determined by the coordinates of the points in the third set of points. minimum value With the maximum value ,by As the initial value for the undetermined intercept, and set the step size s; S62. Generate a line to be determined using the current value of the undetermined intercept, calculate the distance from each point in the third point set to the line to be determined, and then calculate the sum of all distances. S63. Increase the value of the undetermined intercept by a step size. If the current value of the undetermined intercept is less than... Otherwise, proceed to step S64; S64. Select the undetermined intercept corresponding to the minimum value of the sum of distances as the intercept of the second line, thereby determining the second line.

6. An electronic terminal, characterized in that, It includes a processor and a memory, wherein the memory stores a computer program, which, when executed by the processor, causes the electronic terminal to perform the method for calculating the distance between parallel lines based on image processing as described in any one of claims 1 to 5.

7. A computer-readable storage medium having a computer program stored thereon, the computer program, when executed by a processor, causing the processor to perform the method for calculating the distance between parallel lines based on image processing as described in any one of claims 1 to 5.