An intelligent detection method for thread geometry and defects based on OpenCV
Through OpenCV technology, thread images are collected, downsampled and template matching, segmented outlines and fitted sinusoidal models, which solves the problem of sample imbalance in traditional detection methods, and achieves efficient and accurate thread defect detection, which is suitable for a variety of industrial products.
Patent Information
- Application Number
- CN202510785332.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-12
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2045-06-12
AI Technical Summary
In the case of sample imbalance and insufficient diversity, traditional thread defect detection methods are difficult to fully cover different types of defects, which affects the accuracy and reliability of the detection, especially inadequate adaptability to non-standard threads.
Using the OpenCV-based intelligent detection method for thread geometric morphology and defects, the original image is processed by collecting and downsampling, template matching and binarization are performed, the region of interest of threads is divided, and the sinusoidal model of the single-sided contour is fitted to ensure the proportion and diversity of positive and negative samples, and defect evaluation is carried out in combination with morphology and Fourier transform.
It improves the detection ability of threads of various types and specifications, achieves efficient and accurate defect identification, expands application to other industrial products with similar geometric characteristics, and enhances the wide applicability of inspection.
Smart Images

Figure CN120298411B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of thread defect detection, and in particular relates to an intelligent detection method for thread geometry and defects based on OpenCV. Background Art
[0002] Threaded connections are a common and important connection method used in mechanical assembly. Even the slightest defect can lead to insufficient connection strength, compromising overall product performance and safety. Therefore, effective defect detection is crucial to promptly identify and correct potential issues, ensuring product quality standards are met. This not only helps prevent substandard products from entering subsequent processing and assembly, but also significantly reduces material waste, saving time and costs, thereby improving overall production efficiency.
[0003] Traditional defect detection algorithms typically rely on image processing techniques, identifying defects by extracting features from specific local areas and evaluating their performance within a specified tolerance. However, this approach often requires a large number of samples and diverse feature information to account for a wide range of possible negative examples. Consequently, these traditional methods can be limited in their application and struggle to comprehensively cover different types of defects, which in turn affects detection accuracy and reliability.
[0004] Traditional methods often lack sufficient negative samples, resulting in incomplete analysis of various situations, which in turn affects the ability to identify defects. Furthermore, existing technologies perform well in detecting standard threads, but are less adaptable to non-standard threads and other thread types, limiting their application. Summary of the Invention
[0005] The purpose of the present invention is to address the above-mentioned deficiencies in the prior art and provide an intelligent detection method for thread geometry and defects based on OpenCV, so as to solve the problems of sample imbalance and insufficient diversity in existing thread visual defect detection.
[0006] In order to achieve the above object, the technical solution adopted by the present invention is:
[0007] An intelligent detection method for thread geometry and defects based on OpenCV, comprising the following steps:
[0008] S1, collect the original image of the thread;
[0009] S2, using bilinear interpolation method to downsample the original image;
[0010] S3, performing template matching on the downsampled original image, locating the position of the thread in the original image, and extracting the thread region of interest;
[0011] S4, using a binarization method to separate the original image into threads and background, and obtain a binary image;
[0012] S5. Use morphological methods to smooth the binary image;
[0013] S6, segmenting the thread region of interest into a left contour and a right contour;
[0014] S7. Performing independent normalization processing on the left and right contours, and fitting a sinusoidal model of the unilateral contour based on discrete Fourier transform and least squares method;
[0015] S8. Extract the mean contour curve based on the sine model, align the contour curve corresponding to the thread of the component to be inspected with the mean contour curve, and perform thread defect detection and result evaluation.
[0016] Furthermore, in S1, the original image includes thread images with qualified quality and thread images with unqualified quality, and the ratio of the thread images with qualified quality to the thread images with unqualified quality is 10:1.
[0017] Furthermore, in S2, the original image is downsampled, specifically:
[0018]
[0019] Where, is the floating coordinate of the target point; 、 、 、 are the four adjacent integer coordinates of the floating coordinates in the original image, where is the upper left corner coordinate among four adjacent integer coordinates; Floating coordinates Relative to the upper left corner coordinates The offset of For the original image in floating coordinates The pixel value at .
[0020] Furthermore, the S3 specifically includes: using a normalized cross-correlation matching algorithm to calculate the similarity between the template and the downsampled original image, determining the best matching position of the original image on the template, and restoring the thread region of interest obtained on the template to the original image;
[0021] Among them, the normalized cross-correlation matching algorithm is:
[0022]
[0023] Where, The maximum matching degree between the template and the original image in all possible positions; The set of pixel positions for calculating cross-correlation; For the original image exist The pixel value of the offset at ; is the mean value of the image within the template window; For template T The pixel value at ; is the mean value of the template.
[0024] Furthermore, the step S6 includes the following sub-steps:
[0025] S61. Divide the thread area of interest into Partial and part;
[0026] S62, according to Partial and Partial detection obtains the maximum and minimum values of the single-side profile, and then divides the thread area of interest into left and right profiles, while retaining the single-side profile sequence .
[0027] Furthermore, the S61 specifically includes:
[0028] The vertical centerline of the thread area of interest for:
[0029]
[0030] Where, and are the leftmost and rightmost x-coordinates of the thread region of interest, respectively;
[0031] Based on vertical centerline , the thread area of interest is divided into Partial and part:
[0032]
[0033]
[0034] Where C is the thread region of interest.
[0035] Furthermore, the step S7 includes the following sub-steps:
[0036] S71, for unilateral contour sequence Perform the mean removal operation;
[0037] S72, contour sequence after de-averaging operation Perform normalization to obtain the sequence ;
[0038] S73, sequence Perform discrete Fourier transform and use the frequency corresponding to the maximum amplitude of the transformed frequency domain signal as the initial angular frequency of the sine model ;
[0039] S74. Use the least square method to fit the objective function to obtain the best fitting parameters, and then obtain the sinusoidal model of the unilateral profile.
[0040] Furthermore, in the step S72, the contour sequence after the mean removal operation is Perform normalization processing, specifically:
[0041]
[0042] Where, A one-sided contour sequence The i-th element in it; and Represents the minimum and maximum values in the element data respectively.
[0043] Furthermore, in S73, the sequence Perform discrete Fourier transform, specifically:
[0044]
[0045] Where, is the sequence index, is the frequency index, is the total length of the sequence; is the frequency domain signal, Represents an imaginary unit.
[0046] Furthermore, in S74, the least squares method is used to fit the objective function for:
[0047]
[0048] Fitting the objective function according to the least squares method The best fitting parameters are obtained, and then the sinusoidal model of the fitted unilateral profile is obtained:
[0049]
[0050] in, The first True values; is the amplitude of the initial waveform, is the initial angular frequency, is the initial phase; is the initial offset; is a set of unilateral contour fitting points; is the amplitude of the fitted waveform, is the angular frequency after fitting, is the phase after fitting, is the offset after fitting.
[0051] The intelligent detection method for thread geometry and defects based on OpenCV provided by the present invention has the following beneficial effects:
[0052] Through an improved detection scheme, the present invention ensures a reasonable ratio and diversity of positive and negative samples (thread images of qualified quality and thread images of unqualified quality), enhances the detection capability of various types and specifications of threads, and provides wider applicability. At the same time, the method can also be extended to other industrial products with similar geometric features, thereby achieving efficient and accurate defect detection and defect identification. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 This is a flow chart of the intelligent detection method for thread geometry and defects based on OpenCV of the present invention. DETAILED DESCRIPTION
[0054] The specific embodiments of the present invention are described below to facilitate understanding of the present invention by those skilled in the art. However, it should be clear that the present invention is not limited to the scope of the specific embodiments. For those skilled in the art, as long as various changes are within the spirit and scope of the present invention as defined and determined by the appended claims, these changes are obvious, and all inventions and creations utilizing the concepts of the present invention are protected.
[0055] This embodiment provides an intelligent detection method for thread geometry and defects based on OpenCV. This embodiment forms a systematic detection framework from data acquisition to defect assessment. By collecting a large number of positive and negative samples, the image is preprocessed using morphological operations and downsampling technology to extract the thread area of interest and contour information. Subsequently, the left and right contours are processed independently, and the phase difference between samples is eliminated through linear interpolation and trigonometric function fitting. A large number of positive samples are aligned, and the mean contour curve is extracted to capture their common features. Finally, by aligning the contour curve to be tested with the mean contour curve, it is evaluated whether the thread of the component to be tested has defects. Figure 1 , which specifically includes the following steps:
[0056] Step S1, collecting the original image of the thread;
[0057] Specifically, this example captures comprehensive visual data of screws, ensuring a 10:1 ratio of positive to negative samples. Using multi-angle imaging technology, images of multiple screws are acquired from 60 different viewing angles, resulting in a dataset of 24,000 images of qualified threads (positive samples) and 2,400 images of unqualified threads (negative samples). Negative samples are selected based on geometric distortion during the manufacturing process and potential assembly defects, such as burrs on contour edges, to ensure a rich and diverse dataset and enhance the model's generalization capabilities.
[0058] Step S2: downsampling the original image using a bilinear interpolation method to reduce the image resolution to 25% of the original image, in order to reduce the complexity of subsequent calculations, improve the efficiency of template matching, and thus speed up processing;
[0059] The bilinear interpolation method downsamples the original image, specifically:
[0060]
[0061] Where, is the floating coordinate of the target point; 、 、 、 are the four adjacent integer coordinates of the floating coordinates in the original image, where is the upper left corner coordinate among four adjacent integer coordinates; Floating coordinates Relative to the upper left corner coordinates The offset of For the original image in floating coordinates The pixel value at ;
[0062] After calculation by the above bilinear interpolation method, the new pixel value It is calculated by taking the weighted average of the values of four adjacent pixels, where the weighting coefficients are the distance factors (1 - a), a, (1 - b), b.
[0063] Step S3: performing template matching on the downsampled original image to locate the position of the thread in the original image and extract the thread region of interest;
[0064] Specifically, this embodiment uses a normalized cross-correlation matching algorithm to calculate the similarity between the template and the downsampled original image, determines the best matching position of the original image on the template, and restores the thread region of interest obtained on the template to the original image;
[0065] Among them, the normalized cross-correlation matching algorithm is:
[0066]
[0067] Where, The maximum matching degree between the template and the original image in all possible positions; The set of pixel positions for calculating cross-correlation; For the original image exist The pixel value of the offset at ; is the mean value of the image within the template window; For template T The pixel value at ; is the mean value of the template.
[0068] Step S4: using a binarization method to separate the original image into threads and background, and obtaining a binary image, which specifically includes the following sub-steps:
[0069] Step S41: Count each gray level of the input original image The number of pixels on the grayscale (usually from 0 to 255) is obtained, and the grayscale histogram of the original image is calculated according to the histogram. , that is, pixel gray level The ratio of the number of pixels to the total number of pixels :
[0070]
[0071] Step S42: Set a threshold t. Initially, all gray levels from 0 to 255 can be traversed. For each threshold t, the image is divided into two categories: pixels with gray values less than or equal to t belong to the background (category 0), and pixels with gray values greater than t belong to the foreground (category 1). The foreground is the thread.
[0072] For each possible threshold t, calculate the probability of foreground and background and :
[0073]
[0074] Where L is the maximum value of gray level;
[0075] Calculate the average gray value of foreground and background and :
[0076]
[0077] Step S43: Calculate the inter-class variance. The inter-class variance reflects the degree of differentiation between foreground and background. The larger the variance, the better the current threshold t can differentiate between foreground and background:
[0078]
[0079] Where, is the between-class variance.
[0080] Step S44: Determine the optimal threshold by traversing all possible thresholds t and selecting the threshold that maximizes the inter-class variance. ,this It is the optimal segmentation threshold automatically selected.
[0081] Step S45: passing the threshold , effectively separating the thread part from the background part in the original image, thus facilitating subsequent processing, such as thread detection or burr detection.
[0082] Specifically, use the optimal threshold Binarize the image and make the pixel gray value less than or equal to The part is set to 0 (background), and the part greater than The part is set to 255 (foreground), the formula is:
[0083]
[0084] Where, is the pixel grayscale value.
[0085] Step S5: Smoothing the binary image using a morphological method effectively removes small noise and glitches in the binary image. This step utilizes the properties of structural elements to smooth tiny irregular shapes, thereby obtaining a clearer image. The morphological method used in this embodiment is a mature technology, so its specific process is not described in detail.
[0086] Step S6: Segment the thread region of interest into a left contour and a right contour, which specifically includes the following sub-steps:
[0087] Step S61: Divide the thread region of interest into Partial and part;
[0088] By determining the vertical center point of the thread region of interest (ROI), the profile is divided into two parts, representing the left and right features of the thread respectively. This processing method makes it possible to analyze the left and right profiles independently, thereby more accurately evaluating the geometric characteristics of the thread and its potential defects.
[0089] Specifically, given an image First, determine the thread region of interest (ROI) and place the Split into left and right parts;
[0090] The vertical centerline of the thread area of interest for:
[0091]
[0092] Where, and are the leftmost and rightmost x-coordinates of the thread region of interest, respectively;
[0093] Based on vertical centerline , the thread area of interest is divided into Partial and part:
[0094]
[0095]
[0096] Where C is the thread region of interest.
[0097] Step S62: Partial and Partial detection obtains the maximum and minimum values of the single-side profile, and then divides the thread area of interest into left and right profiles, while retaining the single-side profile sequence ;
[0098] This embodiment starts from Partial and The contours are extracted from each part, specifically:
[0099] , Represents a list containing contour coordinates, where Is the x-coordinate of a series of points on the contour. Since the geometric features of the thread tip and end are small when the thread rotates, based on this, further single-sided contour segmentation can be achieved by detecting the maximum and minimum values of the single-sided contour (that is, the vertex positions of the major diameter and minor diameter), that is, finding All local maxima in the sequence, i.e. peaks. Using a custom tolerance range, the most similar maximum values in the eigenvalues are retained to refine the contour segmentation. The final retained unilateral contour sequence The shape of the curve is close enough to a sine wave that any defects in the thread profile will be reflected in the retained profile.
[0100] Step S7: performing independent normalization processing on the left and right contours, and fitting a sinusoidal model of the unilateral contour based on discrete Fourier transform and least squares method;
[0101] Specifically, this embodiment further segments the single-sided contour After normalization, the primary frequency components of the signal are identified through discrete Fourier transforms to establish an initial sinusoidal model. Least squares fitting is then performed to continuously adjust the parameters of the sinusoidal model. Ultimately, a fitting curve and a uniformly distributed set of fitting points are obtained. For the multiple sets of single-sided contour fitting points formed after processing multiple images, linear interpolation is used to calculate the phase deviation between the fitting points to eliminate the phase difference of the original contour point set.
[0102] Among them, the sinusoidal model is:
[0103]
[0104] This embodiment specifically includes the following steps:
[0105] Step S71: Single-side contour sequence Perform the mean removal operation;
[0106]
[0107] Step S72: De-meaning the contour sequence Perform normalization to obtain the sequence ;
[0108]
[0109] Where, A one-sided contour sequence The i-th element in it; and Respectively represent the minimum and maximum values in the element data; multiple Combine into a sequence .
[0110] Step S73: Sequence Perform discrete Fourier transform and use the frequency corresponding to the maximum amplitude of the transformed frequency domain signal as the initial parameter of the sine model, that is, the initial angular frequency ;
[0111]
[0112] Where, is the sequence index, is the frequency index, is the total length of the sequence; is the frequency domain signal, Represents an imaginary unit.
[0113] Step S74: fitting the objective function using the least squares method to obtain the best fitting parameters, and then obtaining a sinusoidal model of the unilateral profile;
[0114] Least squares method to fit the objective function for:
[0115]
[0116] Fitting the objective function according to the least squares method The best fitting parameters are obtained, and then the sinusoidal model of the fitted unilateral profile is obtained:
[0117]
[0118] in, The first True values; is the amplitude of the initial waveform, is the initial angular frequency; is the initial phase, which determines the offset of the sine wave relative to the time axis; is the initial offset, which determines the vertical position of the waveform; is a set of unilateral contour fitting points; is the amplitude of the fitted waveform, is the angular frequency after fitting, is the phase after fitting, is the offset after fitting.
[0119] Step S8: defect detection and result evaluation;
[0120] By averaging multiple aligned single-sided contour fitting point sets, the mean contour curve is extracted to capture their common features.
[0121] After the threads of the component to be inspected have been processed through steps S1 to S7 above, they are aligned with the mean profile curve and the difference is calculated, thereby effectively evaluating whether the thread profile of the component to be inspected has defects.
[0122] The method of the present invention combines statistics and signal processing technology to ensure accurate evaluation of thread geometric characteristics and provide a scientific basis for defect detection.
[0123] Although the specific embodiments of the invention are described in detail in conjunction with the accompanying drawings, this should not be construed as limiting the scope of protection of this patent. Within the scope described by the claims, various modifications and variations that can be made by those skilled in the art without creative work still fall within the scope of protection of this patent.
Claims
1. An intelligent detection method for thread geometry and defects based on OpenCV, characterized in that: The following steps are involved: S1, collect the original image of the thread; S2, using bilinear interpolation method to downsample the original image; S3, performing template matching on the downsampled original image, locating the position of the thread in the original image, and extracting the thread region of interest; S4, using a binarization method to separate the original image into threads and background, and obtain a binary image; S5. Use morphological methods to smooth the binary image; S6, segmenting the thread region of interest into a left contour and a right contour; S7. Performing independent normalization processing on the left and right contours, and fitting a sinusoidal model of the unilateral contour based on discrete Fourier transform and least squares method; S8. Extract the mean contour curve based on the sine model, align the contour curve corresponding to the thread of the component to be inspected with the mean contour curve, and perform thread defect detection and result evaluation.
2. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 1, characterized in that: In S1, the original image includes thread images with qualified quality and thread images with unqualified quality, and the ratio of the thread images with qualified quality to the thread images with unqualified quality is 10:
1.
3. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 1 is characterized in that: In S2, the original image is downsampled, specifically: ; Where, is the floating coordinate of the target point; 、 、 、 are the four adjacent integer coordinates of the floating coordinates in the original image, where is the upper left corner coordinate among four adjacent integer coordinates; Floating coordinates Relative to the upper left corner coordinates The offset of For the original image in floating coordinates The pixel value at .
4. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 3 is characterized in that: S3 specifically includes: using a normalized cross-correlation matching algorithm to calculate the similarity between the template and the downsampled original image, determining the best matching position of the original image on the template, and restoring the thread region of interest obtained on the template to the original image; Among them, the normalized cross-correlation matching algorithm is: ; Where, The maximum matching degree between the template and the original image in all possible positions; The set of pixel positions for calculating cross-correlation; For the original image exist The pixel value of the offset at ; is the mean value of the image within the template window; For template T The pixel value at ; is the mean value of the template.
5. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 4 is characterized in that: The S6 comprises the following sub-steps: S61. Divide the thread area of interest into Partial and part; S62, according to Partial and Partial detection obtains the maximum and minimum values of the single-side profile, and then divides the thread area of interest into left and right profiles, while retaining the single-side profile sequence .
6. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 5, characterized in that: The S61 specifically includes: The vertical centerline of the thread area of interest for: ; Where, and are the leftmost and rightmost x-coordinates of the thread region of interest, respectively; Based on vertical centerline , the thread area of interest is divided into Partial and part: ; ; Where C is the thread region of interest.
7. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 1, characterized in that: The S7 comprises the following sub-steps: S71, for unilateral contour sequence Perform the mean removal operation; S72, contour sequence after de-averaging operation Perform normalization to obtain the sequence ; S73, sequence Perform discrete Fourier transform and use the frequency corresponding to the maximum amplitude of the transformed frequency domain signal as the initial angular frequency of the sine model ; S74. Use the least square method to fit the objective function to obtain the best fitting parameters, and then obtain the sinusoidal model of the unilateral profile.
8. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 7, characterized in that: In the step S72, the contour sequence after the mean removal operation is Perform normalization processing, specifically: ; Where, A one-sided contour sequence The i-th element in it; and Represents the minimum and maximum values in the element data respectively.
9. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 7, characterized in that: In the above S73, the sequence Perform discrete Fourier transform, specifically: ; Where, is the sequence index, is the frequency index, is the total length of the sequence; is the frequency domain signal, Represents an imaginary unit.
10. The intelligent detection method for thread geometry and defects based on OpenCV according to claim 7, characterized in that: In S74, the least squares method is used to fit the objective function for: ; Fitting the objective function according to the least squares method The best fitting parameters are obtained, and then the sinusoidal model of the fitted unilateral profile is obtained: ; in, The first True values; is the amplitude of the initial waveform, is the initial angular frequency, is the initial phase; is the initial offset; is a set of unilateral contour fitting points; is the amplitude of the fitted waveform, is the angular frequency after fitting, is the phase after fitting, is the offset after fitting.
Citation Information
Patent Citations
Deformed steel bar surface defect detection method and system based on machine vision
CN110223296A
Nut internal thread detection method with self-adaptive scale space
CN115393355A