Industrial material defect detection method and device
Through the image preprocessing and feature recognition methods of OpenCV library, the problem of difficulty in low-contrast object detection is solved, and the efficiency and accuracy of industrial material defect detection is achieved.
Patent Information
- Application Number
- CN202411304377.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-19
- Publication Date
- 2025-06-03
AI Technical Summary
The prior art has difficulties in detecting low contrast targets, especially in industrial material defect detection, which makes it difficult to accurately identify and locate defects.
Image preprocessing is performed using the OpenCV library method, including automated rotation, grayscale, histogram calculation, expansion and corrosion operations, combined with the findContours() and minAreaRect() methods, to identify and calculate the geometric parameters of contours and defects in the image.
It significantly improves the accuracy and efficiency of defect detection, can quickly obtain a comprehensive understanding of defects in images, and is suitable for quality control, product inspection and scientific research analysis and other fields.
Smart Images

Figure CN120088181A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of image processing and defect detection, and specifically provides a method and device for detecting defects in industrial materials. Background Art
[0002] OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV is widely used in real-time image processing, video analysis, object detection, and many other fields. OpenCV supports operating systems including Windows, Linux, macOS, and mobile platforms iOS and Android, as well as multiple programming languages, and has a user-friendly interface, making image processing tasks more intuitive. OpenCV has high parameter flexibility. When it is necessary to adjust the detection criteria, it can be achieved by simply adjusting the parameters, and the recognition results are predictable, while deep learning models may need to be retrained under different requirements.
[0003] Similarly, OpenCV also has disadvantages and limitations. For low-contrast targets, traditional algorithms of OpenCV may be difficult to detect. Summary of the Invention
[0004] In view of the above-mentioned deficiencies of the prior art, the present invention provides a method for detecting defects in industrial materials with strong practicability.
[0005] A further technical task of the present invention is to provide a device for detecting defects in industrial materials with reasonable design, safety and applicability.
[0006] The technical solution adopted by the present invention to solve its technical problems is as follows:
[0007] A method for detecting defects in industrial materials includes the following:
[0008] (1) Pretreatment;
[0009] S1.1. Automatically rotate the image and adjust the image angle according to the contour of the overall target to face the user's perspective;
[0010] S1.2. Convert the image into a grayscale image, and then calculate the histogram of the obtained grayscale image to reveal the distribution of grayscale values in the image;
[0011] S1.3. Perform a dilation operation on the image to cover and eliminate black noise points;
[0012] S1.4. Use the findContours() method to comprehensively detect all contours in the image;
[0013] S1.5. Input the filtered contour information as a parameter into the minAreaRect() method to calculate the minimum bounding rectangle of the corresponding contour.
[0014] (2) Defect detection;
[0015] S2.1. Perform precise region division based on the minimum bounding rectangle of the target contour determined in the preprocessing stage.
[0016] S2.2. Use the inRange() function in the OpenCV library to create a binary mask.
[0017] S2.3. Visually annotate the defects in the original image.
[0018] Furthermore, in step S1.2, the x-axis in the histogram represents the gray value, the y-axis represents the number of pixel points corresponding to the gray value, the foreground and background regions of the segmented image are divided, and binary processing is used to obtain the black noise points that appear in the image.
[0019] Furthermore, in step S1.3, perform a dilation operation on the image to cover and eliminate the black noise points by expanding the white area. Subsequently, perform an erosion operation to reduce the white area back to its original size and restore the clarity of the image.
[0020] Furthermore, in step S1.4, use the findContours() method to comprehensively detect all the contours in the image. By this method, obtain the coordinate information of all the contours in the image. For the noise points that are difficult to completely remove, further remove the noise points by calculating the contour area, rectangularity, circularity, and aspect ratio, and retain the finally qualified contours as the target area.
[0021] Furthermore, in step S1.5, input the filtered contour information as a parameter into the minAreaRect() method to calculate the minimum bounding rectangle of the corresponding contour. As the return result, obtain a RotatedRect object. The RotatedRect object describes the geometric attributes of the rectangle in detail, including:
[0022] center: The coordinates of the center point of the rectangle, representing the center position of the target area;
[0023] size: The width and height of the rectangle;
[0024] angle: The rotation angle of the rectangle;
[0025] By analyzing the rotation angle information in the RotatedRect object, perform an automatic rotation process on the overall target contour to make it face the user's perspective.
[0026] Further, in step S2.1, region division is performed according to the minimum bounding rectangle of the target contour determined in the preprocessing stage. The defect detection algorithm is executed for the target region, and other irrelevant regions in the image are not processed.
[0027] Further, in step S2.2, the inRange() function in the OpenCV library is used to create a binary mask. The binary mask clearly identifies the image regions where the pixel values are within a predefined specific range. By this method, the attention is focused on the parts of the image related to the target features, while ignoring those irrelevant pixels. The calling method of the inRange() function is as follows:
[0028] cv2.inRange(src, lowerb, upperrb)
[0029] src: single-channel grayscale image, lowerb: specifies the lower limit of the grayscale value, upperb: specifies the upper limit of the grayscale value. A binary image is returned. The pixel values that satisfy the grayscale value range are 255 (white) in the binary image, and the pixel values that do not satisfy are 0 (black).
[0030] Further, in step S2.3, taking the pixel value range of the defects in the image as an example, a binary image containing only defect information is output. The findContours() method is used to further locate these defects, calculate the geometric parameters of these defects, and visually annotate these defects in the original image;
[0031] In addition, considering the previous rotation processing of the image, it is also necessary to restore the image to the state before rotation and perform corresponding inverse calculations on the coordinates to ensure the accuracy of all annotations;
[0032] The calculation process of this inverse rotation follows the following formula:
[0033]
[0034]
[0035] x and y are the horizontal and vertical coordinates after rotation respectively, centerx and centery are the relative coordinates with the image center as the origin, and angel is the rotation angle.
[0036] An industrial material defect detection device includes: at least one memory and at least one processor;
[0037] The at least one memory is used to store machine-readable programs;
[0038] The at least one processor is configured to call the machine-readable program to execute an industrial material defect detection method.
[0039] Compared with the prior art, an industrial material defect detection method and device of the present invention have the following prominent beneficial effects:
[0040] The present invention can quickly obtain a comprehensive understanding of the defects in the image, thus playing an important role in many fields such as quality control, product detection, and scientific research analysis. The automation and high efficiency of the present invention significantly improve the accuracy and convenience of defect detection, and have broad application prospects and practical values. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0042] Attached Figure 1 is a schematic diagram of a grayscale histogram in an industrial material defect detection method;
[0043] Attached Figure 2 is a schematic diagram of acoustic noise in an industrial material defect detection method. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0044] In order to enable those skilled in the art to better understand the solutions of the present invention, the following will further elaborate on the present invention in conjunction with specific embodiments. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of them. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts fall within the scope of protection of the present invention.
[0045] The following presents a best embodiment:
[0046] An industrial material defect detection method in this embodiment includes the following:
[0047] (1) Pretreatment;
[0048] S1.1. Automatically rotate the image and adjust the image angle according to the contour of the overall target to face the user's perspective;
[0049] This processing method is closer to the observation habits of those in the professional technical field and is convenient for cropping, thereby improving the readability and analysis efficiency of the image.
[0050] S1.2. Convert the image to grayscale, and then calculate the histogram of the resulting grayscale image to reveal the distribution of grayscale values in the image;
[0051] As Figure 1 shown, the x-axis of this histogram represents the grayscale value, while the y-axis represents the number of pixels corresponding to the grayscale value. Through a series of experimental verifications, we found that when using the grayscale value with the largest number of pixels - 20 as the threshold for binarization, the foreground and background regions of the image can be most effectively segmented, achieving the best image segmentation effect.
[0052] S1.3. Perform a dilation operation on the image to cover and eliminate black noise points;
[0053] Due to the interference of noise in the image, black noise points may appear in the image obtained through the above binarization process, and these noise points will interfere with the subsequent contour recognition process, as Figure 2 shown. To improve the accuracy of contour recognition, the present invention adopts a morphological operation method to remove these noise points.
[0054] Specifically, the present invention first performs a dilation operation on the image. This step covers and eliminates black noise points by expanding the white area. Subsequently, the white area is reduced back to its original size through an erosion operation, thereby restoring the clarity of the image. This strategy of dilation first and then erosion not only effectively removes the black noise points in the image but also retains the original contour information of the image, providing a high-quality binary image for subsequent contour extraction and analysis.
[0055] S1.4. Use the findContours() method to comprehensively detect all contours in the image;
[0056] To accurately identify and locate the target contours in the image, the present invention uses the findContours() method to comprehensively detect all contours in the image. Through this method, we can obtain the coordinate information of all contours in the image.
[0057] For those noise points that are difficult to completely remove, the present invention is based on screening mechanisms such as contour area, rectangularity, roundness, aspect ratio, etc. Noise points usually have characteristics such as small area, high rectangularity, low roundness, and small aspect ratio. By calculating the above characteristics, the noise points are further removed, and the finally qualified contours are retained as the target area, thereby further improving the accuracy and reliability of contour recognition.
[0058] S1.5. Input the filtered contour information as a parameter into the minAreaRect() method to calculate the minimum bounding rectangle of the corresponding contour;
[0059] As the return result, we obtain a RotatedRect object, which details the geometric properties of the rectangle, including:
[0060] center: The coordinates of the center point of the rectangle, representing the central position of the target area.
[0061] size: The width and height of the rectangle.
[0062] angle: The rotation angle of the rectangle (relative to the horizontal axis).
[0063] By analyzing the rotation angle information in the RotatedRect object, the present invention can perform an automated rotation process on the overall target contour. This rotation operation aims to adjust the image to face the user's perspective directly, thereby optimizing the display effect of the image and providing a more intuitive and familiar viewing angle for professionals in the technical field. This strategy based on the rotation of the target contour not only improves the readability of the image but also lays a solid foundation for subsequent image analysis and processing.
[0064] (2) Defect detection;
[0065] S2.1. Perform precise region division based on the minimum bounding rectangle of the target contour determined in the preprocessing stage;
[0066] Only execute the defect detection algorithm on this target area and do not process other irrelevant areas in the image. This method effectively reduces unnecessary computational overhead and significantly improves computational efficiency.
[0067] S2.2. Use the inRange() function in the OpenCV library to create a binary mask;
[0068] The function of this mask is to clearly identify the image areas where the pixel values are within a predefined specific range. By this method, we can focus on the parts of the image related to the target features and ignore the irrelevant pixels, thereby further improving the pertinence and efficiency of image processing. The calling method of the inRange() function is as follows.
[0069] cv2.inRange(src, lowerb, upperrb)
[0070] src: A single-channel grayscale image. lowerb: Specify the lower limit of the grayscale value. upperb: Specify the upper limit of the grayscale value. Returns a binary image, where the pixel values within the grayscale value range are 255 (white) in the binary image, and the pixel values that do not meet the range are 0 (black).
[0071] S2.3. Mark the defects in the original image in a visual way;
[0072] Taking the pixel value range of defects in the image (for example, 80 to 120) as an example, we can output a binary image that only contains defect information.
[0073] Using the findContours() method, further locate these defects and calculate their geometric parameters, including position, length, width, area, and roundness, etc. To more intuitively display the detection results, we annotate these defects in the original image in a visual way. In addition, considering the previous rotation processing of the image, we also need to restore the image to the state before rotation and perform corresponding reverse calculations on the coordinates to ensure the accuracy of all annotations. This reverse rotation calculation process follows the following formula:
[0074]
[0075] x and y are the horizontal and vertical coordinates after rotation respectively, centerx and centery are the relative coordinates with the image center as the origin, and angel is the rotation angle.
[0076] Based on the above method, an industrial material defect detection device in this embodiment includes: at least one memory and at least one processor;
[0077] The at least one memory is used to store machine-readable programs;
[0078] The at least one processor is used to call the machine-readable program and execute an industrial material defect detection method.
[0079] This method can automatically process image data and accurately annotate the surface defects. Only by providing the image to be detected, the present invention can automatically output the detailed index parameters of the defects, including key information such as the exact position, length and width, area, and roundness of the defects. By providing image data, the defects can be automatically annotated, and the defect position, length and width, area, roundness, etc. are output. The index parameters are shown in Table 1.
[0080] Table 1 Comparison of true size and measured size
[0081]
[0082] In the test session, the calculation resources of the method of the present invention are saved by 82%, the accuracy of the measured defect length reaches 99.7%, and the accuracy of the defect width reaches 94.5%. Through this method, users can quickly obtain a comprehensive understanding of the defects in the image, thus playing an important role in multiple fields such as quality control, product detection, and scientific research analysis. The automation and high efficiency characteristics of the present invention significantly improve the accuracy and convenience of defect detection, and have broad application prospects and practical values.
[0083] The above specific embodiments are only specific cases of the present invention. The patent protection scope of the present invention includes but is not limited to the above specific embodiments. Any technical solution that conforms to the above specific embodiments of the present invention and any appropriate changes or substitutions made by any person of ordinary skill in the relevant technical field shall fall within the patent protection scope of the present invention.
[0084] Although the embodiments of the present invention have been shown and described, for those of ordinary skill in the art, it can be understood that various changes, modifications, substitutions, and variations can be made to these embodiments without departing from the principles and spirit of the present invention. The scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for detecting defects in industrial materials, characterized in that: These include: (a) pretreatment; S1.1, automatically rotate the image and adjust the image angle according to the outline of the overall target to face the user's perspective; S1.2, converting the image into a grayscale image, and then calculating the histogram of the obtained grayscale image to reveal the distribution of grayscale values in the image; S1.3, dilate the image to cover and eliminate black noise; S1.4, use the findContours() method to fully detect all contours in the image; S1.5, input the filtered contour information as a parameter to the minAreaRect() method to calculate the minimum enclosing rectangle of the corresponding contour; (2) Defect detection; S2.1, perform accurate area division based on the minimum bounding rectangle of the target contour determined in the preprocessing stage; S2.
2. Use the inRange() function in the OpenCV library to create a binary mask; S2.
3. Mark the defects in the original image in a visual way.
2. The method for detecting industrial material defects according to claim 1, characterized in that: In step S1.2, the x-axis in the histogram represents the grayscale value, the y-axis represents the number of pixels corresponding to the grayscale value, the foreground and background areas of the image are segmented, and the black noise points appearing in the image are obtained by binarization processing.
3. The method for detecting industrial material defects according to claim 2, characterized in that: In step S1.3, a dilation operation is performed on the image to cover and eliminate black noise by enlarging the white area, and then the white area is reduced to its original size through an erosion operation to restore the clarity of the image.
4. The industrial material defect detection method according to claim 3, characterized in that: In step S1.4, the findContours() method is used to comprehensively detect all contours in the image. Through this method, the coordinate information of all contours in the image is obtained. For noise points that are difficult to completely remove, the noise points are further removed by calculating the contour area, rectangularity, roundness and aspect ratio, and the final contour that meets the conditions is retained as the target area.
5. The industrial material defect detection method according to claim 4, characterized in that: In step S1.5, the filtered contour information is input as a parameter to the minAreaRect() method, and the minimum enclosing rectangle of the corresponding contour is calculated. As a return result, a RotatedRect object is obtained, which describes the geometric properties of the rectangle in detail, including: center: the coordinates of the center point of the rectangle, indicating the center position of the target area; size: the width and height of the rectangle; angle: the rotation angle of the rectangle; By analyzing the rotation angle information in the RotatedRect object, the overall target outline is automatically rotated to face the user's perspective.
6. The method for detecting industrial material defects according to claim 5, characterized in that: In step S2.1, the region is divided according to the minimum bounding rectangle of the target contour determined in the preprocessing stage, and the defect detection algorithm is executed on the target region, while other irrelevant regions in the image are not processed.
7. The method for detecting industrial material defects according to claim 6, characterized in that: In step S2.2, the inRange() function in the OpenCV library is used to create a binary mask, which explicitly identifies the image area whose pixel values are within a predefined specific range. In this way, attention is focused on the parts of the image related to the target features, while ignoring those irrelevant pixels. The inRange() function is called as follows: cv2.inRange(src,lowerb,upperrb) src: single-channel grayscale image, lowerb: specifies the lower limit of grayscale value, upperb: specifies the upper limit of grayscale value, returns a binary image, the pixel value that meets the grayscale value range is 255 white in the binary image, and the pixel value that does not meet the range is 0 black.
8. The method for detecting industrial material defects according to claim 6, characterized in that: In step S2.3, taking the pixel value range of defects in the image as an example, a binary image containing only defect information is output, and the findContours() method is used to further locate these defects, calculate the geometric parameters of these defects, and annotate these defects in the original image in a visual way; In addition, considering the previous rotation processing of the image, it is necessary to restore the image to the state before the rotation and perform the corresponding reverse calculation of the coordinates to ensure the accuracy of all annotations; This reverse rotation is calculated using the following formula: x, y are the horizontal and vertical coordinates after rotation, centerx, centery are the relative coordinates with the center of the image as the origin, and angel is the rotation angle.
9. An industrial material defect detection device, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to call the machine-readable program to execute the method according to any one of claims 1 to 8.