Using OpenCV and Qt5 to build a caliper fitting straight line tool (C++ implementation)


Building a caliper fitting straight line tool based on OpenCV and Qt5

foreword

The blogger recently made an industrial vision engineering project based on Hikvision Vision Master4.0, which used the line-finding tool of Hikvision VM, and then the blogger developed a similar function based on the technical principle, based on OpenCV Build a caliper tool for finding straight lines with Qt5. Caliper finding a straight line is a commonly used technology in the field of computer vision. It is used to detect images in images and is widely used in industrial vision and other fields.
For the caliper fitting circle tool, you can refer to the blogger’s use of OpenCV and Qt5 to build a caliper circle finding tool (C++ implementation)

1. Overview of caliper edge straight line fitting

Caliper edge line fitting is a commonly used computer vision technique for detecting and fitting straight edges of objects in images. Its principles are based on mathematical geometry and statistical methods.

The following is the basic principle of caliper edge straight line fitting:

  1. Edge detection: First, edge detection is performed on the image to extract the edge information in the image. Commonly used edge detection algorithms include Sobel operator, Canny operator, etc.

  2. Caliper sliding window: select a starting point from the image, then move a small window (usually one-dimensional) with a fixed step size and direction, and calculate the gradient of edge pixels within the window.

  3. Distance metric and fitting: For each window, the error between the pixels in the window and the line is measured by a distance metric function (eg, least squares). The most common distance metric function is the distance from a point to a line.

  4. Iterative update: iteratively adjusts the position of the window so that the error of the distance measure is minimized. This can be achieved by continuously adjusting the position and orientation of the window until the convergence condition is reached.

  5. Line Fitting: Finally, when the window is moved to the best-fit line, the points within the window are fitted to a line model. This can be done by fitting algorithms such as least squares to compute line parameters such as slope and intercept.

Through the above steps, the caliper edge straight line fitting can effectively extract the straight line edge information from the image, and fit the corresponding straight line model. This method has been widely used in many computer vision tasks, such as object detection, line segment extraction, road marking detection, etc.


2. Caliper principle

The principle of the caliper is to find the place where the gray value of N small rectangular ROIs changes suddenly. That is: traverse each small rectangular ROI and find a point respectively, which is the peak value of the grayscale mutation. Then fit the N points into a straight line or a circle.

3. 1D edge extraction

insert image description here

  1. Construct a caliper line finder tool shape through Qt5, which is constructed by each rectangular tool;
    insert image description here

  2. Gaussian filtering is performed on the average gray value (contour) to make the curve smoother and eliminate noise;
    insert image description here

  3. Find the first derivative of the smoothed contour;
    insert image description here

  4. Extract edge points according to the set parameters.

4. The principle of straight line fitting

Line fitting is a common mathematical method for estimating and fitting a straight line model through a set of discrete data points. This method can be applied in many fields, such as statistics, machine learning and computer vision.

The following is the principle of straight line fitting:

  1. Data Collection: First, collect a known set of data points. These data points can be experimental observations, sample data, or data obtained by other means.

  2. Determine the fitting model: For straight line fitting, we assume that the model to be fitted is a straight line. The linear model can be expressed by slope and intercept, that is, y = mx + b, where m is the slope and b is the intercept.

  3. Loss function selection: Choose a loss function to measure the error between the observed point and the fitted line. A common loss function is the least squares method (Least Squares), which is the sum of the squares of the vertical distances from each observation point to the line.

  4. Parameter Estimation: Find the best line parameters (slope and intercept) by minimizing the loss function. In the least squares method, closed solutions can be calculated by formulas, or iterative optimization can be performed using optimization algorithms such as gradient descent.

  5. Model Evaluation: After fitting a line, the model can be evaluated to determine its applicability and effectiveness. Common evaluation indicators include Mean Squared Error (Mean Squared Error), Coefficient of Determination (Coefficient of Determination), etc.

5. Software implementation

No commercial image processing library is used, but pure Qt5+OpenCV
insert image description here

  1. Select an image to find straight lines
    insert image description here

  2. Draw a Line Finder
    insert image description here

  3. straight line fitting
    insert image description here

4. Compared with Hikvision Vision Master
insert image description here

conclusion

Due to the limited ability of bloggers, the methods mentioned in this article will inevitably have omissions. I hope you can enthusiastically point out the mistakes, so that the next revision can be presented to everyone in a more perfect and rigorous manner. before.

Guess you like

Origin blog.csdn.net/weixin_40280870/article/details/131423657