A workpiece edge automatic tracking method and system
By fitting the workpiece edge motion trajectory using the least squares method, automatic edge tracking is achieved, solving the problems of visual fatigue and inconsistent accuracy caused by manual operation, and improving measurement efficiency and accuracy.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING SHANG FANG YUN SHUI SOFTWARE TECH CO LTD
- Filing Date
- 2025-07-17
- Publication Date
- 2026-06-02
AI Technical Summary
In existing technologies, edge contour measurement relies on manual operation, which leads to visual and muscle fatigue, inconsistent measurement accuracy, low efficiency, and high error rate.
The least squares method is used to fit the motion trajectory of the workpiece edge, and the edge points are automatically tracked through the image acquisition window to calculate the tangent direction vector, thereby realizing automatic edge tracking.
It reduces manual operations, improves measurement accuracy and efficiency, lowers the error rate, and saves human and financial costs.
Smart Images

Figure CN120846242B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of image measurement, and in particular relates to a method and system for automatic tracking of workpiece edges. Background Technology
[0002] Two-dimensional image measurement is an important measurement method in modern industry. It captures images using a high-definition camera and adjusts image clarity by modifying parameters such as color and saturation, or binarizes the image by setting a threshold, before magnification and measurement using image algorithms. Contour measurement is a crucial component of image measurement and is widely used in assembly line operations such as mobile phone modules and electronic device components. Current edge measurement methods still rely on traditional external devices like hand markers and keyboards. Frequent operation leads to operator visual and muscle fatigue, potentially causing accidents. Furthermore, human operation relies on visual identification to control equipment, resulting in inconsistent measurement accuracy. Efficient, fast, and accurate contour measurement would significantly reduce labor and financial costs in this process, and automatic edge tracking algorithms and devices perfectly solve this problem.
[0003] In the industrial manufacturing sector, image measurement has been widely applied. For example, mobile phone module manufacturers produce mobile phone modules, and electronic component manufacturers produce electronic cards. The specification inspection of these high-precision and sophisticated components is a crucial part of the entire production line. Every day, the inspection of tens of thousands of identical components is completed manually by workers using image inspection equipment such as image measuring instruments.
[0004] Currently, the most common method for measuring edge contours is the coordinate measuring machine (CMM), which uses a mouse to control the machine's movement and collect data. Workers face tens or hundreds of thousands of components to inspect daily; prolonged repetitive operations lead to visual and muscle fatigue, resulting in a continuous decrease in work efficiency and a persistent increase in error rates over time. Summary of the Invention
[0005] To address the aforementioned technical problems, this invention proposes a technical solution for an automatic workpiece edge tracking method.
[0006] The first aspect of this invention discloses an automatic workpiece edge tracking method, the method comprising:
[0007] Step S1: Acquire images of predefined parts of the workpiece and their edge points through the image capture window; input parameter step size based on the edge points; select a preset number of edge points as a local point set at the end of the motion trajectory of the image.
[0008] Step S2: Using the least squares method, fit the curve at the end of the motion trajectory to the set of local points; calculate the tangent direction vector of the curve based on the polynomial of the obtained curve.
[0009] Step S3: Based on the normalized tangent direction vector and the step size, apply the current coordinates of the window capturing the image to calculate the coordinates of the next movement of the window;
[0010] Step S4: The window moves to the next calculated coordinate, and steps S1 to S3 are repeated until the end marker is encountered.
[0011] According to the method of the first aspect of the present invention, in step S1, the preset quantity is 3 to 15.
[0012] According to the method of the first aspect of the present invention, in step S2, before fitting the curve at the end of the motion trajectory, the method further includes:
[0013] A quadratic polynomial is chosen as the fitting formula for the curve.
[0014] According to the method of the first aspect of the present invention, in step S2, before selecting a quadratic polynomial as the curve fitting formula and fitting the curve at the end of the motion trajectory, the method further includes:
[0015] Perform a perpendicularity check on the points in the local point set. If it is determined that the data points are distributed within the predefined range of the vertical line, then the curve is considered to be a vertical line.
[0016] According to the method of the first aspect of the present invention, in step S3, calculating the coordinates of the next movement of the window based on the current coordinates of the window capturing the image, using the normalized tangent direction vector and the step size, includes:
[0017] The compensation amounts in the x and y directions are calculated based on the normalized tangent direction vector and the step size.
[0018] The offset in the x-direction is calculated based on the width of the window, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction.
[0019] The offset in the y-direction is calculated based on the height of the window, the vector in the y-direction of the tangent direction, and the compensation amount in the y-direction.
[0020] The coordinates of the window's next movement are calculated based on the current coordinates of the captured image window, the offset in the x-direction, and the offset in the y-direction.
[0021] According to the method of the first aspect of the present invention, in step S3, calculating the compensation amounts in the x and y directions based on the normalized tangent direction vector and the step size includes:
[0022]
[0023] in, This represents the compensation amount in the x-direction; Indicates the step size; A vector representing the x-direction of the tangent; This represents the compensation amount in the y-direction; The vector representing the y-direction of the tangent.
[0024] According to the method of the first aspect of the present invention, in step S3, calculating the offset in the x-direction based on the width of the window, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction includes:
[0025]
[0026] in, This represents the offset in the x-direction; Indicates the width of the window;
[0027] The step of calculating the offset in the y-direction based on the window height, the y-direction vector of the tangent direction, and the y-direction compensation amount includes:
[0028]
[0029] in, This represents the offset in the y-direction; Indicates the height of the window.
[0030] A second aspect of the present invention discloses an automatic workpiece edge tracking system, the system comprising:
[0031] The first processing module is configured to: acquire images of predefined parts of the workpiece and their edge points through an image capture window; input parameter step size based on the edge points; and select a preset number of edge points as a local point set at the end of the motion trajectory of the image.
[0032] The second processing module is configured to use the least squares method to fit the curve at the end of the motion trajectory using the set of local points; and to calculate the tangent direction vector of the curve based on the polynomial of the obtained curve.
[0033] The third processing module is configured to calculate the coordinates of the next movement of the window by applying the current coordinates of the window capturing the image, based on the normalized tangent direction vector and the step size.
[0034] The fourth processing module is configured to move the window to the next calculated coordinate and repeat steps S1 to S3 until an end marker is encountered.
[0035] A third aspect of this invention discloses an electronic device. The electronic device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the automatic workpiece edge tracking method according to any one of the first aspects of this disclosure.
[0036] A fourth aspect of this invention discloses a computer-readable storage medium. The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of a workpiece edge automatic tracking method according to any one of the first aspects of this disclosure.
[0037] In summary, the solution proposed in this invention can automatically complete edge measurement tasks. For large-scale such tasks, it can be performed without human intervention, reducing detection errors caused by visual or muscle fatigue. This is particularly significant for improving efficiency in assembly line operations. Consequently, it greatly improves work efficiency and reduces error rates, thereby reducing costs for enterprises. Attached Figure Description
[0038] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0039] Figure 1 This is a flowchart of an automatic workpiece edge tracking method according to an embodiment of the present invention;
[0040] Figure 2 This is a schematic diagram illustrating the existence of the next image window at the inflection point of the image according to an embodiment of the present invention.
[0041] Figure 3 A schematic diagram showing the next moving position of the window, based on an embodiment of the present invention, where the next image window is located at an inflection point of the image.
[0042] Figure 4 This is a schematic diagram of the next movement position of the window after compensation according to an embodiment of the present invention;
[0043] Figure 5 This is a schematic diagram illustrating the principles of starting and stopping according to an embodiment of the present invention;
[0044] Figure 6 This is a structural diagram of an automatic workpiece edge tracking system according to an embodiment of the present invention;
[0045] Figure 7This is a structural diagram of an electronic device according to an embodiment of the present invention;
[0046] Figure 8 This is a schematic diagram of a tablet computer module as an example according to an embodiment of the present invention;
[0047] Figure 9 This is a schematic diagram of bottom lighting of a tablet computer module, as shown in an embodiment of the present invention.
[0048] Figure 10 This is a schematic diagram of obtaining a binarized image of a tablet computer module, as an example, according to an embodiment of the present invention.
[0049] Figure 11 To illustrate this invention, a tablet computer module is used as an example, where the red box represents a schematic diagram of the camera's capture portion. Detailed Implementation
[0050] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0051] The first aspect of this invention discloses an automatic workpiece edge tracking method. Figure 1 This is a flowchart of an automatic workpiece edge tracking method according to an embodiment of the present invention, as follows: Figure 1 As shown, the method includes:
[0052] Step S1: Acquire images of predefined parts of the workpiece and their edge points through the image capture window; input parameter step size based on the edge points; select a preset number of edge points as a local point set at the end of the motion trajectory of the image.
[0053] Step S2: Using the least squares method, fit the curve at the end of the motion trajectory to the set of local points; calculate the tangent direction vector of the curve based on the polynomial of the obtained curve.
[0054] Step S3: Based on the normalized tangent direction vector and the step size, apply the current coordinates of the window capturing the image to calculate the coordinates of the next movement of the window;
[0055] Step S4: The window moves to the next calculated coordinate, and steps S1 to S3 are repeated until the end marker is encountered.
[0056] In step S1, the image of a predefined part of the workpiece and its edge points are acquired through the image capture window; the parameter step size is entered according to the edge points; and a preset number of edge points are selected as a local point set at the end of the motion trajectory of the image.
[0057] In some embodiments, in step S1, the preset quantity is 3 to 15.
[0058] Specifically, edge point data is obtained through other methods, such as OpenVC. However, this embodiment uses the step size parameter for sampling points, which is the distance between every two points when sampling points. It is generally determined according to the shape of the workpiece to be measured. The more complex and varied the shape of the workpiece, the smaller this value should be. For example, if there are many inflection points or sharp corners, the step size should be reduced to ensure the continuity of the data. If it is a regular tool or the shape changes little, such as a rectangular workpiece, the value should be larger. The range is generally [5-15] pixels.
[0059] At the end of the motion trajectory, select n edge points as the test data group (local domain), forming a local point set {P1, P2, P3, ..., P...} n The algorithm uses n to fit curves. The range of n is generally between 3 and 15. This is because the core of this embodiment is to calculate the tangent line at the last point of the curve. Each moving curve can be understood as being composed of many arcs of different sizes. Therefore, we only need to find the last arc at the end of the curve to find the tangent line at the last point of the curve. The direction of this tangent line is the direction of the curve movement that needs to be calculated. Therefore, n cannot be too large, as it will affect the true shape of the fitted curve, nor can it be too small, as it will affect the calculation accuracy. The algorithm uses n of 5, which can ensure the accuracy of the fitted curve and avoid the possibility of complex curves affecting the calculation accuracy.
[0060] In step S2, the least squares method is used to fit the curve at the end of the motion trajectory using the local point set; the tangent direction vector of the curve is calculated based on the polynomial of the obtained curve.
[0061] In some embodiments, in step S2, before fitting the curve at the end of the motion trajectory, the method further includes:
[0062] A quadratic polynomial is chosen as the fitting formula for the curve.
[0063] Before fitting the curve at the end of the motion trajectory using a quadratic polynomial as the fitting formula, the process further includes:
[0064] Perform a perpendicularity check on the points in the local point set. If it is determined that the data points are distributed within the predefined range of the vertical line, then the curve is considered to be a vertical line.
[0065] Specifically, curve fitting methods include least squares and interpolation (the classic example being Newton's interpolation). Since this embodiment only calculates the next movement trajectory of the machine, ensuring the image acquisition window covers the curve's extension, only the tangent direction at the last point of the curve needs to be calculated. Therefore, the computational load is small, and the accuracy of individual points is not pursued; only that the acquisition points infinitely approximate the curve is sufficient. In summary, this embodiment uses the least squares method to fit the curve.
[0066] Fitting function The calculation, that is, based on the given n points ( Using these as sample points, calculate the curve fitting function. .
[0067] The curve fitted by the least squares method does not require By using all the data collection points, the goal is only to approximate the data as closely as possible, i.e., to minimize the deviation. It approaches 0 infinitely, but because It can be positive or negative, therefore the total deviation cannot be considered as positive or negative. hour, This effectively reflects the relationship between variables because the absolute value of each deviation can be quite large. Therefore, this embodiment ultimately chooses to use... To measure the total deviation.
[0068] Since minimizing the sum of squared deviations ensures that each deviation will not be too large, the problem is reduced to a deterministic approach. ,make It is the smallest.
[0069] Fitting function The fitting function is an nth-degree polynomial (n>=1). In manufacturing, the workpiece contour can be an irregular curve, a regular arc, or a straight line. When the fitted curve infinitely approaches a straight line, n equals 1, i.e. When the curve is complex, n>=2. Since we only need to calculate the trajectory at the end of the curve and not the shape of the entire curve, a quadratic polynomial is used in this embodiment:
[0070] y=ax 2 +bx+c
[0071] It can guarantee the fitting of specific curves (straight lines, where the polynomial is a straight line equation when a approaches 0), and it can also guarantee the fitting of curves.
[0072] Substitute n sample points into the data to obtain
[0073]
[0074] To find the coefficients a, b, and c that minimize the error function, we take the partial derivatives of the error function with respect to a, b, and c, respectively, and set the partial derivatives equal to 0, resulting in a system of three equations:
[0075]
[0076]
[0077]
[0078] Solving this system of equations allows us to calculate the values of the coefficients a, b, and c that minimize the error.
[0079] Once the values of coefficients a, b, and c are found, the quadratic polynomial function can be determined.
[0080]
[0081] This function is a curve obtained by fitting edge points within a local region. It can approximately describe the curve shape of the region where these points are located, providing a continuous functional basis for subsequent calculation of the tangent direction of points within that region.
[0082] However, since the least squares method can only fit curves that are not perpendicular to a straight line (a straight line is also a type of curve), for perpendicular straight lines: This is because, mathematically speaking, if the slope of the line is infinitely large, the objective function of the least squares method will tend to diverge and will not converge to a stable solution.
[0083] To address the above situation, before using the quadratic polynomial in the least squares method to fit the curve, we need to perform a perpendicularity check on all points. If it is determined that the data points are approximately distributed near a vertical line, then the object to be fitted is considered to be a vertical line. In the context of 'c', all values can be directly retrieved. Mean: This is equivalent to minimizing data points to execution. Sum of squared horizontal distances It follows the core idea of the least squares method.
[0084] The tangent direction is calculated because it determines the machine's next movement position and maintains continuity with the curve of the current window; therefore, the last point is calculated. The tangent direction is the direction of the machine's next movement.
[0085] For point The tangent direction is calculated as follows:
[0086] Differentiate, for Taking the derivative, we get: ;
[0087] Calculate the slope, and place the point x-coordinate Substituting the derivative, we obtain the slope of the tangent line. ;
[0088] Determine the tangent direction vector, V= The normalized vector is V= .
[0089] In step S3, based on the normalized tangent direction vector and the step size, the coordinates of the next movement of the window are calculated using the current coordinates of the window capturing the image.
[0090] In some embodiments, in step S3, calculating the coordinates of the next movement of the window by applying the current coordinates of the window capturing the image based on the normalized tangent direction vector and the step size includes:
[0091] The compensation amounts in the x and y directions are calculated based on the normalized tangent direction vector and the step size.
[0092] The offset in the x-direction is calculated based on the width of the window, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction.
[0093] The offset in the y-direction is calculated based on the height of the window, the vector in the y-direction of the tangent direction, and the compensation amount in the y-direction.
[0094] The coordinates of the window's next movement are calculated based on the current coordinates of the captured image window, the offset in the x-direction, and the offset in the y-direction.
[0095] The step of calculating the compensation amounts in the x and y directions based on the normalized tangent direction vector and the step size includes:
[0096]
[0097] in, This represents the compensation amount in the x-direction; Indicates the step size; A vector representing the x-direction of the tangent; This represents the compensation amount in the y-direction; The vector representing the y-direction of the tangent.
[0098] The step of calculating the offset in the x-direction based on the window width, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction includes:
[0099]
[0100] in, This represents the offset in the x-direction; Indicates the width of the window;
[0101] The step of calculating the offset in the y-direction based on the window height, the y-direction vector of the tangent direction, and the y-direction compensation amount includes:
[0102]
[0103] in, This represents the offset in the y-direction; Indicates the height of the window.
[0104] Specifically, calculate the next movement position of the window. The window here is the window for capturing the image, that is, the current coordinates of the window. The algorithm for determining the movement position is as follows:
[0105] The size of the image window is determined by the lens's focal length and angle of view; this parameter remains constant. Width: ,high: ;
[0106] Calculate the offset of the current coordinates
[0107]
[0108] Calculate the next position
[0109]
[0110]
[0111] However, for regular rectangular workpieces, a situation may arise where, when measuring on a vertical or horizontal line, if the calculation... There exists a next image window that is exactly at the inflection point of the image, such as... Figure 2 As shown.
[0112] Based on the above algorithm, the position of the next movement in the acquisition window is as follows: Figure 3 As shown.
[0113] At this point, it is impossible to collect edge point data.
[0114] To ensure the continuity of edge points, a special process is needed: the last position captured in the previous window, i.e., the position of the tangent point, must be included in the window. This requires... We need to compensate for the errors in the calculation, but we cannot allow duplicate points to affect the actual curve calculation results. Therefore, we use the step size of the sampling points as the compensation coefficient. The optimized algorithm is as follows:
[0115] Calculate the compensation in the x and y directions respectively:
[0116]
[0117] Calculate the offset of the current coordinates
[0118]
[0119] or:
[0120]
[0121] Calculate the next position
[0122]
[0123]
[0124] After compensation, the calculation results show that the next movement position of the window in the above figure is as follows: Figure 4 .
[0125] The above compensation algorithm is also applicable to the handling of sharp corners.
[0126] In some embodiments, a curve with a complete, continuous, closed edge is detected, but this is not applicable to cases where there are breakpoints.
[0127] The start and stop functions belong to the data acquisition logic. This embodiment only provides a motion trajectory calculation for automatic acquisition. However, for the completeness of this embodiment, the principle of start and stop is introduced here.
[0128] Starting point: The user manually moves the image window to ensure the curve is within the window, selects the curve to be measured, and determines the starting and direction points. Then, the user manually starts automatic edge tracking in the software. The starting and direction points define the curve to be measured. Because multiple edges may exist within an image window, the image acquisition algorithm may have acquired multiple curves. The starting and ending points are used to find the data of the curve to be measured among these multiple curves. The image acquisition algorithm is then passed in, confirmed, and the edge to be measured is selected. Dragging the mouse will draw a starting direction of movement in the image, such as... Figure 5 As shown.
[0129] Stop flag: The data collected using the above method is continuous. When the window moves to the specified position to collect a new curve data, it will compare the data from beginning to end with the beginning of the previously measured curve data. If the distance is less than step, the current point is the last closed point, and automatic measurement will stop at this point.
[0130] In some specific embodiments,
[0131] 1) Taking a tablet computer module as an example, such as Figure 8 As shown;
[0132] 2) Lighting is applied from the bottom of the device, such as... Figure 9 As shown;
[0133] 3) Obtain the binarized image of the module, such as... Figure 10 As shown;
[0134] 4) The edge of the camera's local magnification module is processed using high-magnification, high-definition image processing;
[0135] 5) such as Figure 11 As shown, the red box indicates the camera's capture portion; the above describes the device's operation process.
[0136] In summary, the difference between automatic and manual measurement lies in the movement of the image acquisition window. Manual measurement requires moving the image window via a manual remote sensor or manual button controlled by a PLC or control system. The precision of manually operating buttons or a mouse cannot reach the pixel level. Automatic measurement, on the other hand, calculates based on the acquired data, achieving pixel-level precision, making it more accurate than manual measurement.
[0137] Manually moving the image window requires visual aiming at the image position and manual operation of the controller, while automatic inspection only requires manually determining the starting point and direction point and clicking the start button. The following experiment, conducted by a skilled worker inspecting 20 6.5-inch mobile phone casings, illustrates this. The 6.5-inch mobile phone modules, after magnification, require 15 windows to complete the data acquisition. Automatic inspection data is shown in Table 1 (unit: seconds), and manual inspection data is shown in Table 2 (unit: seconds).
[0138] Table 1
[0139]
[0140] Table 2
[0141]
[0142] Without considering fatigue caused by long hours of work, manual inspection takes nearly 6 times longer per inspection window than automatic inspection. This means that the larger the workpiece, the more significant the efficiency gap in automatic inspection becomes.
[0143] The second aspect of the present invention discloses an automatic workpiece edge tracking system. Figure 6 This is a structural diagram of an automatic workpiece edge tracking system according to an embodiment of the present invention; as shown. Figure 6 As shown, the system 100 includes:
[0144] The first processing module 101 is configured to: acquire images of predefined parts of the workpiece and their edge points through an image capture window; input parameter step size based on the edge points; and select a preset number of edge points as a local point set at the end of the motion trajectory of the image.
[0145] The second processing module 102 is configured to use the least squares method to fit the curve at the end of the motion trajectory using the set of local points; and to calculate the tangent direction vector of the curve based on the polynomial of the obtained curve.
[0146] The third processing module 103 is configured to calculate the coordinates of the next movement of the window based on the normalized tangent direction vector and the step size, using the current coordinates of the window capturing the image.
[0147] The fourth processing module 104 is configured to move the window to the next calculated coordinate and repeat the first to third processing modules until an end marker is encountered.
[0148] According to the system of the second aspect of the present invention, the first processing module 101 is specifically configured such that the preset number is 3 to 15.
[0149] Specifically, edge point data is obtained through other methods, such as OpenVC. However, this embodiment uses the step size parameter for sampling points, which is the distance between every two points when sampling points. It is generally determined according to the shape of the workpiece to be measured. The more complex and varied the shape of the workpiece, the smaller this value should be. For example, if there are many inflection points or sharp corners, the step size should be reduced to ensure the continuity of the data. If it is a regular tool or the shape changes little, such as a rectangular workpiece, the value should be larger. The range is generally [5-15] pixels.
[0150] At the end of the motion trajectory, select n edge points as the test data group (local domain), forming a local point set {P1, P2, P3, ..., P...} n The algorithm uses n to fit curves. The range of n is generally between 3 and 15. This is because the core of this embodiment is to calculate the tangent line at the last point of the curve. Each moving curve can be understood as being composed of many arcs of different sizes. Therefore, we only need to find the last arc at the end of the curve to find the tangent line at the last point of the curve. The direction of this tangent line is the direction of the curve movement that needs to be calculated. Therefore, n cannot be too large, as it will affect the true shape of the fitted curve, nor can it be too small, as it will affect the calculation accuracy. The algorithm uses n of 5, which can ensure the accuracy of the fitted curve and avoid the possibility of complex curves affecting the calculation accuracy.
[0151] According to a system of a second aspect of the present invention, the second processing module 102 is specifically configured to, before fitting the curve at the end of the motion trajectory, further include:
[0152] A quadratic polynomial is chosen as the fitting formula for the curve.
[0153] Before fitting the curve at the end of the motion trajectory using a quadratic polynomial as the fitting formula, the process further includes:
[0154] Perform a perpendicularity check on the points in the local point set. If it is determined that the data points are distributed within the predefined range of the vertical line, then the curve is considered to be a vertical line.
[0155] Specifically, curve fitting methods include least squares and interpolation (the classic example being Newton's interpolation). Since this embodiment only calculates the next movement trajectory of the machine, ensuring the image acquisition window covers the curve's extension, only the tangent direction at the last point of the curve needs to be calculated. Therefore, the computational load is small, and the accuracy of individual points is not pursued; only that the acquisition points infinitely approximate the curve is sufficient. In summary, this embodiment uses the least squares method to fit the curve.
[0156] Fitting function The calculation, that is, based on the given n points ( Using these as sample points, calculate the curve fitting function. .
[0157] The curve fitted by the least squares method does not require By using all the data collection points, the goal is only to approximate the data as closely as possible, i.e., to minimize the deviation. It approaches 0 infinitely, but because It can be positive or negative, therefore the total deviation cannot be considered as positive or negative. hour, This effectively reflects the relationship between variables because the absolute value of each deviation can be quite large. Therefore, this embodiment ultimately chooses to use... To measure the total deviation.
[0158] Since minimizing the sum of squared deviations ensures that each deviation will not be too large, the problem is reduced to a deterministic approach. ,make It is the smallest.
[0159] Fitting function The fitting function is an nth-degree polynomial (n>=1). In manufacturing, the workpiece contour can be an irregular curve, a regular arc, or a straight line. When the fitted curve infinitely approaches a straight line, n equals 1, i.e. When the curve is complex, n>=2. Since we only need to calculate the trajectory at the end of the curve and not the shape of the entire curve, a quadratic polynomial is used in this embodiment:
[0160] y=ax 2 +bx+c
[0161] It can guarantee the fitting of specific curves (straight lines, where the polynomial is a straight line equation when a approaches 0), and it can also guarantee the fitting of curves.
[0162] Substitute n sample points into the data to obtain
[0163]
[0164] To find the coefficients a, b, and c that minimize the error function, we take the partial derivatives of the error function with respect to a, b, and c, respectively, and set the partial derivatives equal to 0, resulting in a system of three equations:
[0165]
[0166]
[0167]
[0168] Solving this system of equations allows us to calculate the values of the coefficients a, b, and c that minimize the error.
[0169] Once the values of coefficients a, b, and c are found, the quadratic polynomial function can be determined.
[0170]
[0171] This function is a curve obtained by fitting edge points within a local region. It can approximately describe the curve shape of the region where these points are located, providing a continuous functional basis for subsequent calculation of the tangent direction of points within that region.
[0172] However, since the least squares method can only fit curves that are not perpendicular to a straight line (a straight line is also a type of curve), for perpendicular straight lines: This is because, mathematically speaking, if the slope of the line is infinitely large, the objective function of the least squares method will tend to diverge and will not converge to a stable solution.
[0173] To address the above situation, before using the quadratic polynomial in the least squares method to fit the curve, we need to perform a perpendicularity check on all points. If it is determined that the data points are approximately distributed near a vertical line, then the object to be fitted is considered to be a vertical line. In the context of 'c', all values can be directly retrieved. Mean: This is equivalent to minimizing data points to execution. Sum of squared horizontal distances It follows the core idea of the least squares method.
[0174] The tangent direction is calculated because it determines the machine's next movement position and maintains continuity with the curve of the current window; therefore, the last point is calculated. The tangent direction is the direction of the machine's next movement.
[0175] For point The tangent direction is calculated as follows:
[0176] Differentiate, for Taking the derivative, we get: ;
[0177] Calculate the slope, and place the point x-coordinate Substituting the derivative, we obtain the slope of the tangent line. ;
[0178] Determine the tangent direction vector, V= The normalized vector is V= .
[0179] According to the system of the second aspect of the present invention, the third processing module 103 is specifically configured to calculate the coordinates of the next movement of the window based on the normalized tangent direction vector and the step size, using the current coordinates of the window capturing the image.
[0180] The compensation amounts in the x and y directions are calculated based on the normalized tangent direction vector and the step size.
[0181] The offset in the x-direction is calculated based on the width of the window, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction.
[0182] The offset in the y-direction is calculated based on the height of the window, the vector in the y-direction of the tangent direction, and the compensation amount in the y-direction.
[0183] The coordinates of the window's next movement are calculated based on the current coordinates of the captured image window, the offset in the x-direction, and the offset in the y-direction.
[0184] The step of calculating the compensation amounts in the x and y directions based on the normalized tangent direction vector and the step size includes:
[0185]
[0186] in, This represents the compensation amount in the x-direction; Indicates the step size; A vector representing the x-direction of the tangent; This represents the compensation amount in the y-direction; The vector representing the y-direction of the tangent.
[0187] The step of calculating the offset in the x-direction based on the window width, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction includes:
[0188]
[0189] in, This represents the offset in the x-direction; Indicates the width of the window;
[0190] The step of calculating the offset in the y-direction based on the window height, the y-direction vector of the tangent direction, and the y-direction compensation amount includes:
[0191]
[0192] in, This represents the offset in the y-direction; Indicates the height of the window.
[0193] Specifically, calculate the next movement position of the window. The window here is the window for capturing the image, that is, the current coordinates of the window. The algorithm for determining the movement position is as follows:
[0194] The size of the image window is determined by the lens's focal length and angle of view; this parameter remains constant. Width: ,high: ;
[0195] Calculate the offset of the current coordinates
[0196]
[0197] Calculate the next position
[0198]
[0199]
[0200] However, for regular rectangular workpieces, a situation may arise: when measuring on a vertical or horizontal line, if the calculated... There exists a next image window that is exactly at the inflection point of the image, such as... Figure 2 As shown.
[0201] Based on the above algorithm, the position of the next movement in the acquisition window is as follows: Figure 3 As shown.
[0202] At this point, it is impossible to collect edge point data.
[0203] To ensure the continuity of edge points, a special process is needed: the last position captured in the previous window, i.e., the position of the tangent point, must be included in the window. This requires... We need to compensate for the errors in the calculation, but we cannot allow duplicate points to affect the actual curve calculation results. Therefore, we use the step size of the sampling points as the compensation coefficient. The optimized algorithm is as follows:
[0204] Calculate the compensation in the x and y directions respectively:
[0205]
[0206] Calculate the offset of the current coordinates
[0207]
[0208] or:
[0209]
[0210] Calculate the next position
[0211]
[0212]
[0213] After compensation, the calculation results show that the next movement position of the window in the above figure is as follows: Figure 4 .
[0214] The above compensation algorithm is also applicable to the handling of sharp corners.
[0215] A third aspect of this invention discloses an electronic device. The electronic device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the automatic workpiece edge tracking method according to any one of the first aspects of this invention.
[0216] Figure 7 This is a structural diagram of an electronic device according to an embodiment of the present invention, such as... Figure 7As shown, the electronic device includes a processor, memory, communication interface, display screen, and input device connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, carrier networks, Near Field Communication (NFC), or other technologies. The display screen can be an LCD screen or an e-ink screen. The input device can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the device's casing, or an external keyboard, touchpad, or mouse.
[0217] Those skilled in the art will understand that Figure 7 The structure shown is merely a structural diagram of the part related to the technical solution of this disclosure and does not constitute a limitation on the electronic device to which the solution of this application is applied. The specific electronic device may include more or fewer components than shown in the figure, or combine certain components, or have different component arrangements.
[0218] A fourth aspect of this invention discloses a computer-readable storage medium. The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of a workpiece edge automatic tracking method according to any one of the first aspects of this invention.
[0219] Please note that the technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments have been described. However, as long as the combination of these technical features does not contradict each other, it should be considered within the scope of this specification. The above embodiments only illustrate several implementation methods of this application, and their descriptions are relatively specific and detailed, but they should not be construed as limiting the scope of the invention patent. It should be pointed out that for those skilled in the art, several modifications and improvements can be made without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A method for automatic edge tracking of a workpiece, characterized in that, The method includes: Step S1: Acquire images of predefined parts of the workpiece and their edge points through the image capture window; input parameter step size based on the edge points; select a preset number of edge points at the end of the motion trajectory of the image as a local point set; wherein the parameter step size is the distance between every two points when acquiring points. Step S2: Using the least squares method, fit the curve at the end of the motion trajectory to the set of local points; calculate the tangent direction vector of the curve based on the polynomial of the obtained curve. Step S3: Based on the normalized tangent direction vector and the step size, apply the current coordinates of the window capturing the image to calculate the coordinates of the next movement of the window; Step S4: The window moves to the next calculated coordinate and repeats steps S1 to S3 until an end marker is encountered. The trigger condition for the end marker is: when the window moves to the specified position to collect a new curve data, it is compared with the beginning and end of the measured curve data. If the distance is less than the step size, the current point is the last closed point, and the automatic measurement stops at this point. In step S1, the preset quantity is 3 to 15; In step S2, before fitting the curve at the end of the motion trajectory, the method further includes: A quadratic polynomial is chosen as the formula for fitting the curve. In step S2, before selecting a quadratic polynomial as the curve fitting formula and fitting the curve at the end of the motion trajectory, the following steps are also included: Perform a perpendicularity check on the points in the local point set. If it is determined that the data points are distributed within a predefined range of a vertical line, then the curve is considered to be a vertical line. The step of calculating the coordinates of the next movement of the window by applying the current coordinates of the captured image window based on the normalized tangent direction vector and the step size includes: The compensation amounts in the x and y directions are calculated based on the normalized tangent direction vector and the step size. The offset in the x-direction is calculated based on the width of the window, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction. The offset in the y-direction is calculated based on the height of the window, the vector in the y-direction of the tangent direction, and the compensation amount in the y-direction. The coordinates of the window's next movement are calculated based on the current coordinates of the window capturing the image, the offset in the x-direction, and the offset in the y-direction. In step S3, calculating the compensation amounts in the x and y directions based on the normalized tangent direction vector and the step size includes: Where tx represents the compensation amount in the x-direction; step represents the step size; dx represents the vector in the x-direction of the tangent direction; ty represents the compensation amount in the y-direction; and dy represents the vector in the y-direction of the tangent direction. In step S3, calculating the offset in the x-direction based on the window width, the vector in the x-direction of the tangent direction, and the compensation amount in the x-direction includes: Where offsetX represents the offset in the x-direction; width represents the width of the window; The step of calculating the offset in the y-direction based on the window height, the y-direction vector of the tangent direction, and the y-direction compensation amount includes: Here, offsetY represents the offset in the y-direction; height represents the height of the window.
2. An automatic edge tracking system for workpieces, characterized in that, The system employs the method described in claim 1, and the system comprises: The first processing module is configured to: acquire images of predefined parts of the workpiece and their edge points through an image capture window; input parameter step size based on the edge points; and select a preset number of edge points as a local point set at the end of the motion trajectory of the image. The second processing module is configured to use the least squares method to fit the curve at the end of the motion trajectory using the set of local points; and to calculate the tangent direction vector of the curve based on the polynomial of the obtained curve. The third processing module is configured to calculate the coordinates of the next movement of the window by applying the current coordinates of the window capturing the image, based on the normalized tangent direction vector and the step size. The fourth processing module is configured to move the window to the next calculated coordinate and repeat the first to third processing modules until an end marker is encountered.
3. An electronic device, characterized in that, The electronic device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, it implements the steps of the automatic workpiece edge tracking method according to claim 1.
4. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the automatic workpiece edge tracking method according to claim 1.