Intelligent detection method for thread geometrical morphology and defects based on OpenCV

Through OpenCV's thread geometric morphology and defect intelligent detection methods, the problem of sample imbalance and insufficient diversity in traditional detection methods is solved, efficient and accurate identification of thread defects is achieved, and suitable for the detection of threads of various types and specifications, and extended to other industrial products.

CN120298411AActive Publication Date: 2025-07-11CHENGDU UNIV OF INFORMATION TECH
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202510785332.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-12
Publication Date
2025-07-11
Estimated Expiration
2045-06-12

AI Technical Summary

Technical Problem

Traditional thread defect detection methods have sample imbalance and insufficient diversity, making it difficult to fully cover different types of defects, affecting the accuracy and reliability of the detection, especially inadequate performance in non-standard thread detection.

Method used

The intelligent detection method of thread geometric morphology and defects based on OpenCV is adopted, and the proportion and diversity of positive and negative samples are ensured through bilinear interpolation downsampling, template matching, morphological processing, sinusoidal model fitting and other steps, the left and right contours are independently processed, and the sinusoidal model is fitted using discrete Fourier transform and least squares method to achieve efficient identification of thread defects.

Benefits of technology

Enhance the detection capability of threads of various types and specifications, provides wider applicability, achieve efficient and accurate defect detection, and extend application to other industrial products with similar geometric characteristics.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120298411A_ABST
    Figure CN120298411A_ABST
Patent Text Reader

Abstract

The invention discloses an intelligent detection method for thread geometrical morphology and defects based on OpenCV, and belongs to the technical field of thread defect detection.The intelligent detection method comprises the steps that an original image of a thread is collected; carrying out downsampling processing on the original image; template matching is carried out on the original image, and a thread region of interest is extracted; separating the original image into a thread and a background, and obtaining a binary image; smoothing the binary image by adopting a morphological method; dividing the thread region of interest into a left contour and a right contour; performing normalization processing on the single-side contour, and performing fitting to obtain a sine model of the single-side contour; and performing thread defect detection and result evaluation based on the sine model. According to the invention, the reasonable proportion and diversity of positive and negative samples are ensured, the capability of detecting threads of various types and specifications is enhanced, and the method has wider applicability so as to realize efficient and accurate thread defect detection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of thread defect detection, and particularly relates to an intelligent detection method for the geometric shape and defects of threads based on OpenCV. Background Art

[0002] Thread connection is an important connection method commonly used in mechanical assembly. Any minor defect may lead to insufficient connection strength, thereby affecting the performance and safety of the overall product. Therefore, it is crucial to implement effective defect detection, which can timely identify and correct potential problems to ensure that the product meets the quality standards. This not only helps to prevent unqualified products from entering the subsequent processing and assembly links, but also significantly reduces material waste, saves time and costs, thereby improving the overall production efficiency.

[0003] Traditional defect detection algorithms usually rely on image processing technology to identify defects by extracting features of specific local regions and evaluating their performance within the specified tolerance range. However, this method often requires a large number of samples and diverse feature information to cope with various possible negative sample situations. Therefore, traditional methods may have certain limitations in application, making it difficult to comprehensively cover different types of defects, thereby affecting the accuracy and reliability of detection.

[0004] Traditional methods often lack sufficient negative samples, resulting in insufficient analysis of various situations and thus affecting the ability to identify defects. In addition, existing technologies perform well in detecting standard threads, but are insufficiently adaptable to non-standard threads and other types of threads, limiting their application scope. Summary of the Invention

[0005] The purpose of the present invention is to provide an intelligent detection method for the geometric shape and defects of threads based on OpenCV in view of the above deficiencies in the prior art, so as to solve the problems of sample imbalance and lack of diversity in existing thread visual defect detection.

[0006] To achieve the above purpose, the technical solution adopted by the present invention is:

[0007] An intelligent detection method for the geometric shape and defects of threads based on OpenCV, which includes the following steps:

[0008] S1. Collect the original image of the thread;

[0009] S2. Perform downsampling processing on the original image using the bilinear interpolation method;

[0010] S3. Perform template matching on the downsampled original image to locate the position of the thread in the original image and extract the region of interest of the thread;

[0011] S4. Use a binarization method to separate the original image into threads and the background, and obtain a binary image;

[0012] S5. Use a morphological method to smooth the binary image;

[0013] S6. Divide the region of interest of the thread into a left contour and a right contour;

[0014] S7. Perform independent normalization processing on the left contour and the right contour, and based on the discrete Fourier transform and the least squares method, fit to obtain a sine model of the unilateral contour;

[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 detected with the mean contour curve, and perform thread defect detection and result evaluation.

[0016] Furthermore, in the S1, the original image includes a thread image with qualified quality and a thread image with unqualified quality, and the ratio of the thread image with qualified quality to the thread image with unqualified quality is 10:1.

[0017] Furthermore, in the S2, perform downsampling processing on the original image. Specifically:

[0018]

[0019] In the formula, is the floating coordinate of the target point; , , , are respectively four adjacent integer coordinates of the floating coordinate in the original image, where is the upper left corner coordinate of the four adjacent integer coordinates; are respectively the floating coordinate relative to the upper left corner coordinate offset; is the pixel value of the original image at the floating coordinate .

[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 region of interest of the thread obtained on the template to the original image;

[0021] Among them, the normalized cross-correlation matching algorithm is:

[0022]

[0023] In the formula, is the maximum matching degree between the template and the original image at all possible positions; is the set of pixel positions for calculating the cross - correlation; is the original image at is the pixel value of the offset; is the mean value of the image within the template window; is the pixel value of the template T at ; is the mean value of the template.

[0024] Furthermore, the said S6 includes the following sub - steps:

[0025] S61. According to the vertical center line of the thread region of interest, divide the thread region of interest into part and part;

[0026] S62. Detect the maximum and minimum values of the unilateral contour according to the part and part, and then divide the thread region of interest into a left contour and a right contour, while retaining the unilateral contour sequence .

[0027] Furthermore, the said S61 specifically includes:

[0028] The vertical center line of the thread region of interest is:

[0029]

[0030] wherein, and are respectively the left - most and right - most x - coordinates of the thread region of interest;

[0031] Based on the vertical center line , divide the thread region of interest into part and part:

[0032]

[0033]

[0034] wherein C is the thread region of interest.

[0035] Furthermore, the said S7 includes the following sub - steps:

[0036] S71. Perform a de - mean operation on the unilateral contour sequence ;

[0037] S72. Normalize the contour sequence after the mean removal operation to obtain a sequence ;

[0038] S73. Perform a discrete Fourier transform on the sequence and use the frequency corresponding to the maximum amplitude in the transformed frequency-domain signal as the initial angular frequency of the sine model ;

[0039] S74. Use the least squares method to fit the objective function to obtain the best fit parameters, and then obtain the sine model of the unilateral contour.

[0040] Further, in S72, when normalizing the contour sequence after the mean removal operation , specifically:

[0041]

[0042] where is the i-th element in the unilateral contour sequence ; and represent the minimum and maximum values in the element data, respectively.

[0043] Further, in S73, when performing a discrete Fourier transform on the sequence , 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 the imaginary unit.

[0046] Further, in S74, the least squares method to fit the objective function is:

[0047]

[0048] According to the best fit parameters obtained by fitting the objective function using the least squares method, the sine model of the fitted unilateral contour is obtained:

[0049]

[0050] where is the -th true value in the sequence; is the amplitude of the initial waveform, is the initial angular frequency, is the initial phase; is the initial offset; is the set of unilateral contour fitting points; is the amplitude of the waveform after fitting, is the angular frequency after fitting, is the phase after fitting, is the offset after fitting.

[0051] The intelligent detection method for thread geometric morphology and defects based on OpenCV provided by the present invention has the following beneficial effects:

[0052] Through the improved detection scheme, the present invention ensures the reasonable ratio and diversity of positive and negative samples (thread images with qualified quality and thread images with unqualified quality), enhances the detection ability for various types and specifications of threads, provides a wider applicability. At the same time, this method can also be extended and applied to other industrial products with similar geometric features, so as to realize efficient and accurate defect detection and can efficiently and accurately identify defects. Description of the Drawings

[0053] Figure 1 is the flow chart of the intelligent detection method for thread geometric morphology and defects based on OpenCV of the present invention. Detailed Embodiments

[0054] The following describes the detailed embodiments of the present invention to facilitate those skilled in the art of the present technology to understand the present invention. However, it should be clear that the present invention is not limited to the scope of the detailed embodiments. For those of ordinary skill in the art of the present technology, as long as various changes are within the spirit and scope of the present invention defined and determined by the appended claims, these changes are obvious, and all inventions and creations using the concept of the present invention are within the scope of protection.

[0055] This embodiment provides an intelligent detection method for thread geometric morphology and defects based on OpenCV. This embodiment forms a systematic detection framework from data acquisition to defect evaluation. By collecting a large number of positive and negative samples, morphological operations and downsampling techniques are used to preprocess the images to extract the regions of interest and contour information of the threads. Subsequently, the left and right contours are processed independently. The phase differences between samples are eliminated through linear interpolation and trigonometric function fitting to align a large number of positive samples, and the mean contour curve is extracted to capture their common features. Finally, by aligning the contour curve to be measured with the mean contour curve, it is evaluated whether there are defects in the threads of the component to be detected. Refer to Figure 1 , and it specifically includes the following steps:

[0056] Step S1, collect the original images of the threads;

[0057] Specifically, in this embodiment, comprehensive visual data capture of screws is performed to ensure that the ratio of positive to negative samples is 10:1. For multiple screws, multi-angle imaging technology is adopted to obtain images at 60 different perspectives, forming a dataset containing 24,000 qualified thread images (positive samples) and 2,400 unqualified thread images (negative samples). The negative samples are selected based on geometric shape distortion and potential assembly defects during the manufacturing process, such as burrs on the contour edges, so as to ensure the richness and diversity of the dataset and enhance the generalization ability of the model.

[0058] Step S2: Use the bilinear interpolation method to downsample the original image, reducing the image resolution to 25% of the original. The purpose is to reduce the complexity of subsequent calculations, improve the efficiency of template matching, and speed up the processing speed.

[0059] The specific process of using the bilinear interpolation method to downsample the original image is as follows:

[0060]

[0061] In the formula, is the floating coordinate of the target point; , , , are the four adjacent integer coordinates of the floating coordinate in the original image. Among them, is the upper left corner coordinate of the four adjacent integer coordinates; are the offsets of the floating coordinate relative to the upper left corner coordinate ; is the pixel value of the original image at the floating coordinate ;

[0062] After calculation by the above bilinear interpolation method, the new pixel value is calculated by weighted averaging of the four adjacent pixel values, and the weighting coefficients are the distance factors (1 - a), a, (1 - b), b.

[0063] Step S3: Perform template matching on the downsampled original image to locate the position of the thread in the original image and extract the region of interest of the thread.

[0064] Specifically, in this embodiment, the normalized cross-correlation matching algorithm is used to calculate the similarity between the template and the downsampled original image, determine the best matching position of the original image on the template, and restore the region of interest of the thread obtained on the template to the original image.

[0065] Among them, the normalized cross-correlation matching algorithm is:

[0066]

[0067] Wherein, is the maximum matching degree between the template and the original image at all possible positions; is the set of pixel positions for calculating the cross-correlation; is the original image at the pixel value of the offset at; is the mean value of the image within the template window; is at the pixel value of the template T at; is the mean value of the template.

[0068] Step S4: Use a binarization processing method to separate the original image into threads and the background, and obtain a binary image, which specifically includes the following sub-steps:

[0069] Step S41: Count the number of pixels at each gray level (usually from 0 to 255) of the input original image to obtain the gray histogram of the original image, and calculate the probability of each gray level , that is, the proportion of the number of pixels with the pixel gray level 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 (class 0), and pixels with gray values greater than t belong to the foreground (class 1), and the foreground is the thread;

[0072] For each possible threshold t, calculate the probabilities of the foreground and the background and :

[0073]

[0074] Wherein, L is the maximum value of the gray level;

[0075] Calculate the average gray values of the foreground and the background and :

[0076]

[0077] Step S43: Calculate the between-class variance. The between-class variance reflects the distinguishability between the foreground and the background. The larger the variance, the better the current threshold t can distinguish the foreground and the background:

[0078]

[0079] In the formula, is the between-class variance.

[0080] Step S44: Determine the optimal threshold. By traversing all possible thresholds t, select the threshold that maximizes the between-class variance , and this is the automatically selected optimal segmentation threshold.

[0081] Step S45: Through the threshold , effectively separate the thread part and the background part in the original image, so as to facilitate subsequent processing, such as thread detection or burr detection.

[0082] Specifically, use the optimal threshold to binarize the image, set the part with pixel gray value less than or equal to to 0 (background), and the part greater than to 255 (foreground). The formula is:

[0083]

[0084] In the formula, is the pixel gray value.

[0085] Step S5: Use morphological methods to smooth the binary image, effectively removing small noises and burrs in the binary image. This step utilizes the characteristics of the structural element to smooth tiny irregular shapes, thereby obtaining a clearer image. The morphological method in this embodiment is a mature existing technology, so the specific process is not described in detail.

[0086] Step S6: Divide the thread region of interest into a left contour and a right contour, which specifically includes the following sub-steps:

[0087] Step S61: According to the vertical center line of the thread region of interest, divide the thread region of interest into part and part;

[0088] By determining the vertical center point of the thread region of interest (ROI), the contour is divided into left and right parts, respectively representing the left and right characteristics of the thread. Such a processing method makes it possible to independently analyze the left and right contours, so as to more accurately evaluate the geometric characteristics of the thread and its potential defects.

[0089] Specifically, for a given image , first determine the region of interest (ROI) of the thread, and divide this region into left and right parts;

[0090] The vertical center line of the region of interest of the thread is:

[0091]

[0092] wherein, and are respectively the leftmost and rightmost x coordinates of the region of interest of the thread;

[0093] Based on the vertical center line , divide the region of interest of the thread into part and part:

[0094]

[0095]

[0096] wherein, C is the region of interest of the thread.

[0097] Step S62, according to part and part, detect the maximum and minimum values of the unilateral contour, and then divide the region of interest of the thread into a left contour and a right contour, while retaining the obtained unilateral contour sequence ;

[0098] In this embodiment, the contours are extracted from part and part respectively, 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, the further segmentation of the unilateral contour can be realized by detecting the maximum and minimum values of the unilateral contour (i.e., the vertex positions of the major diameter and minor diameter), that is, by searching for all local maxima, that is, peaks, in the sequence. Using a custom tolerance range, retain the maxima that are most similar in eigenvalue to refine the contour segmentation. The finally retained unilateral contour sequence presents a curve shape that is close enough to a sine wave. If there are defects in the thread contour, they will also be reflected in the retained contour.

[0100] Step S7: Independently normalize the left and right contours, and based on the discrete Fourier transform and the least squares method, fit to obtain the sine model of the unilateral contour;

[0101] Specifically, in this embodiment, the unilateral contour obtained by further segmentation is normalized, and the main frequency components in the signal are identified through the discrete Fourier transform to establish an initial sine model. Then, least squares fitting is performed to continuously correct the parameters of the sine model. Finally, a fitting curve and a set of fitting points with uniform distribution are obtained. For the multiple sets of fitting points formed after processing multiple images, a linear interpolation method is used to calculate the phase deviation existing between the sets of fitting points to eliminate the phase gap of the original set of contour points.

[0102] Among them, the sine model is:

[0103]

[0104] This embodiment specifically includes the following sub-steps:

[0105] Step S71: Perform a mean removal operation on the unilateral contour sequence ;

[0106]

[0107] Step S72: Normalize the contour sequence after the mean removal operation to obtain the sequence ;

[0108]

[0109] In the formula, is the i-th element in the unilateral contour sequence ; and respectively represent the minimum and maximum values in the element data; multiple are combined into the sequence ;

[0110] Step S73: Perform a discrete Fourier transform on the sequence , and use the frequency corresponding to the maximum amplitude in the transformed frequency-domain signal as the initial parameter of the sine model, that is, the initial angular frequency ;

[0111]

[0112] In the formula, is the sequence index, is the frequency index, is the total length of the sequence; is a frequency-domain signal, represents the imaginary unit.

[0113] Step S74: Use the least squares method to fit the objective function to obtain the best fitting parameters, and then obtain the sine model of the unilateral profile;

[0114] The objective function for least squares fitting is:

[0115]

[0116] According to the objective function for least squares fitting the best fitting parameters are obtained, and then the sine model of the fitted unilateral profile is obtained:

[0117]

[0118] wherein, is the th true value in the sequence; 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 the set of unilateral profile fitting points; is the amplitude of the fitted waveform, is the fitted angular frequency, is the fitted phase, is the fitted offset.

[0119] Step S8: Defect detection and result evaluation;

[0120] By taking the mean of multiple sets of aligned unilateral profile fitting points, the mean profile curve is extracted to capture its common features.

[0121] When the thread of the component to be detected undergoes the above steps S1 to S7 and is aligned with the mean profile curve for difference calculation, it can effectively evaluate whether there are defects in the thread profile of the component to be detected.

[0122] The method of the present invention combines statistical and signal processing technologies, ensuring accurate evaluation of the geometric features of the thread and providing a scientific basis for defect detection.

[0123] Although the specific embodiments of the invention have been described in detail with reference to the accompanying drawings, it should not be construed as a limitation on the protection scope of this patent. Within the scope described in the claims, various modifications and deformations that can be made by those skilled in the art without creative efforts still fall within the protection scope of this patent.

Claims

1. An intelligent detection method for the geometric morphology and defects of threads based on OpenCV, characterized in that, It includes the following steps: S1. Collect the original image of the thread; S2. Perform downsampling processing on the original image using the bilinear interpolation method; S3. Perform template matching on the downsampled original image, locate the position of the thread in the original image, and extract the region of interest of the thread; S4. Use the binarization processing method to separate the original image into the thread and the background, and obtain a binary image; S5. Use the morphological method to smooth the binary image; S6. Divide the region of interest of the thread into a left contour and a right contour; S7. Perform independent normalization processing on the left contour and the right contour, and based on the discrete Fourier transform and the least squares method, fit to obtain the sine model of the unilateral contour; S8. Extract the mean contour curve based on the sine model, align the contour curve corresponding to the thread of the component to be detected with the mean contour curve, and perform thread defect detection and result evaluation.

2. The intelligent detection method for the geometric morphology and defects of threads based on OpenCV according to claim 1, wherein: In S1, the original image includes a thread image with qualified quality and a thread image with unqualified quality, and the ratio of the thread image with qualified quality to the thread image with unqualified quality is 10:

1.

3. The intelligent detection method for thread geometric morphology and defects based on OpenCV according to claim 1, characterized in that In S2, the downsampling processing of the original image is specifically as follows: ; Wherein, is the floating coordinate of the target point; , , , are respectively the four adjacent integer coordinates of the floating coordinate in the original image, wherein, is the upper left corner coordinate among the four adjacent integer coordinates; are respectively the offsets of the floating coordinate relative to the upper left corner coordinate ; is the pixel value of the original image at the floating coordinate .

4. The intelligent detection method for thread geometric morphology and defects based on OpenCV according to claim 3, wherein S3 specifically includes: calculating the similarity between the template and the downsampled original image using the normalized cross-correlation matching algorithm, determining the best matching position of the original image on the template, and restoring the region of interest of the thread obtained on the template to the original image; Among them, the normalized cross-correlation matching algorithm is: ; Wherein, is the maximum matching degree between the template and the original image at all possible positions; is the set of pixel positions for calculating the cross-correlation; is the original image at the pixel value of the offset at; is the mean value of the image within the template window; is the template T at the pixel value at; is the mean value of the template.

5. The intelligent detection method for the geometric morphology and defects of threads based on OpenCV according to claim 4, characterized in that, S6 includes the following sub-steps: S61. Divide the region of interest of the thread into a part and a part according to the vertical center line of the region of interest of the thread; S62. According to the partial sum to detect the maximum and minimum values of the unilateral profile, and then divide the region of interest of the thread into a left profile and a right profile, while retaining the obtained unilateral profile sequence .

6. The intelligent detection method for the geometric morphology and defects of threads based on OpenCV according to claim 5, characterized in that, S61 specifically includes: Vertical center line of the thread region of interest is as follows: ; In the formula, and are the leftmost and rightmost x - coordinates of the thread region of interest, respectively; Based on the vertical center line , the thread region of interest is divided into part and part: ; ; In the formula, C is the region of interest of the thread.

7. The intelligent detection method for the geometric morphology and defects of threads based on OpenCV according to claim 1, characterized in that S7 includes the following sub-steps: S71. Perform a de-mean operation on the unilateral contour sequence ; S72. For the contour sequence after the mean removal operation perform normalization processing to obtain a sequence ; S73. Discretely Fourier transform the sequence and use the frequency corresponding to the maximum amplitude in the frequency-domain signal obtained by the transformation as the initial angular frequency of the sine model ; S74. Use the least squares method to fit the objective function to obtain the best fitting parameters, and then obtain the sine model of the unilateral contour.

8. The intelligent detection method for the geometric shape and defects of threads based on OpenCV according to claim 7, characterized in that, In the S72, the contour sequence after the mean removal operation is normalized, specifically as follows: ; In the formula, is the i-th element in the unilateral contour sequence ; and represent the minimum and maximum values in the element data, respectively.

9. The intelligent detection method for the geometric shape and defects of threads based on OpenCV according to claim 7, characterized in that In the S73, for the sequence perform a discrete Fourier transform, specifically: ; In the formula, is the sequence index, is the frequency index, is the total length of the sequence; is the frequency-domain signal, represents the imaginary unit.

10. The intelligent detection method for the geometric morphology and defects of threads based on OpenCV according to claim 7, wherein In the S74, the least squares fitting objective function is as follows: ; Fitting the objective function according to the least squares method The best fitting parameters are obtained, and then the sine model of the unilateral profile after fitting is obtained: ; Among them, is the th true value in the sequence; is the amplitude of the initial waveform, is the initial angular frequency, is the initial phase; is the initial offset; is the set of one-sided profile fitting points; is the amplitude of the waveform after fitting, 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

  • External thread detection method based on machine vision and real-time detection system

    CN112146593A

  • Nut internal thread detection method with self-adaptive scale space

    CN115393355A

  • Method for three-dimensional reconstruction of the thread of the holes for the studs of the main connector of the reactor pressure vessel and automatic identification of defects

    RU2791416C1

  • Workpiece surface defect detection device and detection method, workpiece surface inspection system, and program

    US20220292665A1