A method and system for detecting defects in vehicle headlight lenses based on traditional image processing
Through traditional image processing methods, including ROI extraction, halo removal and gradient information detection, the dataset requirements and applicability issues of existing headlight defect detection technologies are solved, and efficient and accurate headlight lens defect detection is achieved.
Patent Information
- Application Number
- CN202411755556.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-03
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-12-03
Smart Images

Figure CN119693324B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of industrial intelligent production detection technology, and in particular to a method and system for detecting vehicle lamp lens defects based on traditional image processing. Background Art
[0002] The technologies used for headlight defect detection primarily involve computer vision and image processing. Computer vision uses a camera or video camera to capture images of objects, then processes and analyzes them to achieve object recognition, tracking, measurement, and control. Image processing, on the other hand, involves analyzing, processing, extracting features, and classifying images. Headlight defect detection begins with designing a lighting and imaging solution based on the material and structural characteristics of the headlight area to be inspected, along with a supporting device structure. This structure works in conjunction with the processing and control modules to perform comprehensive headlight inspections.
[0003] Vehicle lights play a vital role in road illumination, driving status, and vehicle appearance. Therefore, headlight inspection is a crucial component of factory inspection. Headlight inspection primarily examines the light distribution performance of headlights and the functional status of the vehicle's lighting. With the rapid development of the automotive industry, headlight defect detection technology is constantly being updated and refined. Currently, some advanced headlight defect detection systems offer automated and intelligent detection capabilities. These systems not only improve inspection efficiency but also reduce human error and enhance the accuracy of test results. For example, machine vision-based automatic headlight defect detection systems capture high-speed, high-precision images of headlights, then analyze and process these images using computer vision and image processing techniques to automatically identify and locate headlight defects.
[0004] Furthermore, with the development of artificial intelligence, deep learning technology has also been introduced to headlight defect detection. By training on large amounts of headlight image data, deep learning models can learn the characteristic representations of headlight defects, thereby achieving high-precision identification of headlight defects. This approach not only improves detection accuracy but also addresses a wide range of complex headlight defect types, making it highly practical. However, this detection technology requires large datasets and time-consuming sample labeling, which is time-consuming and labor-intensive. Furthermore, when the background changes, the model must be retrained, making it less adaptable.
[0005] The above problems are in urgent need of resolution. Summary of the Invention
[0006] The purpose of the present invention is to provide a method and system for detecting defects in headlight lenses based on traditional image processing, so as to solve the technical problems existing in the prior art, such as the need for a large data set, the time and effort required to label samples, the need to retrain the model when the background changes, and the low applicability.
[0007] In order to solve the above technical problems, on the one hand, the present invention provides a method for detecting defects in a headlight lens based on traditional image processing, the method comprising: S1: collecting an original image of a headlight lens through a camera; S2: preprocessing the original image to obtain a first image; S3: extracting a region of interest in the first image using a local binarization method to generate an ROI image, wherein the ROI image only contains information about the headlight lens; S4: performing single-channel overlay processing on the ROI image to enhance the contrast of defects in the ROI image to obtain a second image; S5: using OpenCV to segment the target by taking the average threshold of three regions in the environment near the halo of the central light source of the second image. ; S6: stitching the three target areas after segmentation and removing the halo of the central light source of the second image to obtain the third image; S7: adaptively segmenting the third image through the image binarization segmentation threshold algorithm, and restoring the segmented information to the second image; S8: preliminarily judging whether the headlight lens is qualified by comparing the pixel value corresponding to each pixel point in the restored second image with the preset pixel threshold; S9: in response to the headlight lens being qualified, grayscale processing is performed on the ROI image, and then horizontal and vertical gradient information are extracted to obtain the fourth image and the fifth image respectively; S10: median filtering is performed on the fourth image and the fifth image to filter out random noise respectively; S11: by using OpenCV The thresholding method of the large law is improved, and the custom threshold value obtained by the improved thresholding method is used to perform threshold segmentation on the fourth image and the fifth image after filtering out the noise to obtain the sixth image and the seventh image respectively; S12: the sixth image and the seventh image are subjected to a sum operation to obtain the eighth image; S13: in response to the fact that the headlight lens is a qualified product, the ROI image is subjected to grayscale processing and the gradient information in the left oblique angle of 45° and the right oblique angle of 45° is extracted to obtain the ninth image and the tenth image respectively; S14: the ninth image and the tenth image are subjected to median filtering to filter out random noise respectively; S15: by improving the thresholding method of the large law in OpenCV, the thresholding method of the large law is used to obtain the sixth image and the seventh image respectively based on the custom threshold value obtained by the improved thresholding method The ninth image and the tenth image after the noise is filtered are respectively subjected to threshold segmentation using a custom threshold value obtained by the value method to obtain an eleventh image and a twelfth image; S16: performing an AND operation on the eleventh image and the twelfth image to obtain a thirteenth image; S17: performing an AND operation on the thirteenth image and the eighth image to obtain a fourteenth image; S18: morphologically processing the fourteenth image to merge objects with similar distances in the fourteenth image to obtain a fifteenth image; S19: performing an AND operation on the fifteenth image using a custom mask to obtain a sixteenth image; S20: extracting the ROI information retained in the sixteenth image; S21: judging the qualification of the headlight lens based on the ROI information.
[0008] Furthermore, the step S2: pre-processing the original image to obtain the first image includes: S201: rotating the original image by a custom angle; S202: manually positioning the rotated original image to obtain the first image.
[0009] The further step S3: using the local binarization method to extract the region of interest in the first image to generate an ROI image, wherein the ROI image only contains the headlight lens information, includes: using OpenCv based on the first image to find the maximum connected domain, and performing ROI extraction on it to obtain the ROI image.
[0010] Furthermore, the step S6: splicing the three target areas after segmentation and then removing the halo of the central light source of the second image to obtain the third image includes: S601: using the existing interface of Opencv to obtain the three target areas; S602: when calculating the first target area, the grayscale values corresponding to the second target area and the third target area are set to black; S603: the grayscale of the first target area with the zero component removed is the first target area to be obtained; S604: when calculating the second target area, the grayscale values corresponding to the first target area and the third target area are set to black; S605: the grayscale of the second target area with the zero component removed is the second target area to be obtained; S606: when calculating the third target area, the grayscale values corresponding to the first target area and the second target area are set to black; S607: the grayscale of the third target area with the zero component removed is the third target area to be obtained.
[0011] Furthermore, the step S8: preliminarily judging whether the headlight lens is a qualified product by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold includes: S801: in response to the pixel value corresponding to each pixel point in the restored second image being greater than the preset pixel threshold, directly judging the headlight lens as an unqualified product; S802: in response to the pixel value corresponding to each pixel point in the restored second image being less than the preset pixel threshold, going to step S9.
[0012] Furthermore, the gradient information extraction formula in step S9 is:
[0013]
[0014] Where, f x (x,y) represents the pixel value on the x-axis corresponding to the pixel point (x, y). Represents the pixel variable in the x-axis direction, f y (x,y) represents the pixel value on the y-axis corresponding to the pixel point (x, y). A variable representing pixels in the y-axis direction.
[0015] Furthermore, the step S11: by improving the thresholding method of the large law in OpenCV, based on the custom threshold value obtained by the improved thresholding method, the fourth image and the fifth image after the noise is filtered are threshold segmented to obtain the sixth image and the seventh image respectively, including: S1101: performing the operations of steps S1101-S1107 on the fourth image and the fifth image respectively; S1102: calculating the normalized histogram of the image, using P i , i=1,2,3,...255 represents each component of the histogram; S1103: calculate the cumulative sum P I (k), S1104: Calculate the cumulative mean m(k), S1105: Calculate the global grayscale mean m G ; S1106: Calculate inter-class variance S1107: Obtaining a custom threshold k based on the inter-class variance * ,include: The maximum k value is the custom threshold k * If the maximum value is not unique, take the average value of each k value corresponding to each maximum value as the custom threshold k * ; S1108: Based on the custom threshold k * The fourth image and the fifth image after noise filtering are respectively subjected to threshold segmentation to obtain the sixth image and the seventh image.
[0016] Furthermore, the step S21: judging the qualification of the headlight lens based on the ROI information includes: S2101: counting the number of pixels occupied by each ROI information and comparing it with a preset pixel threshold; S2102: judging the qualification of the headlight lens based on the comparison result.
[0017] Furthermore, the step S2102: judging the qualification of the headlight lens based on the comparison result includes: S21021: judging that the headlight lens is unqualified in response to the fact that the number of pixels occupied by each statistical ROI information is greater than a preset pixel threshold; S21022: judging that the headlight lens is qualified in response to the fact that the number of pixels occupied by each statistical ROI information is less than a preset pixel threshold.
[0018] In a second aspect, the present invention provides a headlight lens defect detection system based on traditional image processing, the system adopts the above-mentioned headlight lens defect detection method based on traditional image processing, and the system includes: an image acquisition module, suitable for acquiring the original image of the headlight lens through a camera; an image preprocessing module, suitable for preprocessing the original image to obtain a first image; a ROI image generation module, suitable for extracting the region of interest in the first image using a local binarization method to generate an ROI image, wherein the ROI image only contains the headlight lens information; a second image generation module, suitable for performing single-channel overlay processing on the ROI image to enhance the contrast of defects in the ROI image to obtain a second image; The invention provides a plurality of image processing modules, comprising: a target segmentation module, which is adapted to segment the environment near the halo of the central light source of the second image by taking the average threshold of three areas through OpenCV to perform target segmentation; a third image generation module, which is adapted to splice the three segmented target areas and then remove the halo of the central light source of the second image to obtain the third image; an adaptive segmentation module, which is adapted to adaptively segment the third image through the image binarization segmentation threshold algorithm and restore the segmented information to the second image; a first judgment module, which is adapted to preliminarily judge whether the headlight lens is qualified by comparing the pixel value corresponding to each pixel point in the restored second image with the preset pixel threshold; a gradient information extraction module, which is adapted to preliminarily judge whether the headlight lens is qualified in response to the headlight lens being qualified. After the ROI image is grayscale processed, the horizontal and vertical gradient information are extracted respectively to obtain the fourth image and the fifth image respectively; in response to the headlight lens being a qualified product, the ROI image is grayscale processed and the gradient information in the left oblique angle of 45° and the right oblique angle of 45° are extracted respectively to obtain the ninth image and the tenth image respectively; a noise filtering module is suitable for performing median filtering on the fourth image and the fifth image to filter out random noise respectively; a median filtering is performed on the ninth image and the tenth image to filter out random noise respectively; a threshold segmentation module is suitable for improving the large-scale thresholding method in OpenCV, and performing the custom threshold value obtained based on the improved large-scale thresholding method to filter the fourth image and the tenth image after the noise is filtered respectively. The fifth image is threshold segmented to obtain a sixth image and a seventh image; the ninth image and the tenth image after noise filtering are threshold segmented based on a custom threshold obtained by improving the large-scale threshold method in OpenCV, respectively, to obtain an eleventh image and a twelfth image; a sum operation module is configured to perform a sum operation on the sixth image and the seventh image to obtain an eighth image; a sum operation is performed on the eleventh image and the twelfth image to obtain a thirteenth image; and a sum operation is performed on the thirteenth image and the eighth image to obtain a fourteenth image; a morphological processing module is configured to perform morphological processing on the fourteenth image to merge objects with similar distances in the fourteenth image to obtain a fifteenth image;a mask processing module adapted to process the fifteenth image using a custom mask to obtain a sixteenth image; a ROI information extraction module adapted to extract ROI information retained in the sixteenth image; and a second judgment module adapted to judge the eligibility of the headlight lens based on the ROI information.
[0019] In a third aspect, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores one or more instructions, and the computer instructions are used to enable the computer to execute the above-mentioned vehicle headlight lens defect detection method based on traditional image processing.
[0020] In a fourth aspect, the present invention provides an electronic device comprising: a memory and a processor; the memory stores at least one program instruction; the processor implements the above-mentioned vehicle headlight lens defect detection method based on traditional image processing by loading and executing the at least one program instruction.
[0021] The beneficial effects of the present invention are:
[0022] (1) The ROI image is obtained by finding the maximum connected domain in the image. By performing subsequent operations only on the region of interest, the computational complexity of subsequent image processing can be effectively reduced and the accuracy of image processing can also be improved.
[0023] (2) The three regions in the image are binarized and segmented using three thresholds. The halo removal operation can avoid the halo caused by reflected light from affecting subsequent image recognition and image processing.
[0024] (3) By summing up the gradient information in the four directions of horizontal, vertical, left angle 45°, and right angle 45°, the edge detection of the image can be performed, which can effectively feedback the rate of change of the image grayscale.
[0025] (4) By removing a certain component to obtain the class segmentation threshold, it is possible to effectively avoid the grayscale value of most backgrounds in the gradient information image being 0. If the Otsu method in OpenCV is directly used to obtain the threshold for image segmentation, the segmentation effect will be unsatisfactory.
[0026] (5) By adopting traditional image processing methods, we can effectively avoid the problems brought about by the use of deep learning models in existing technologies to process images, such as the need for large data sets, the time and effort required to label samples, the need to retrain the model when the background changes, and the low applicability. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The present invention will be further described below with reference to the accompanying drawings and examples.
[0028] Figure 1 This is a flow chart of a method for detecting defects in a headlight lens based on traditional image processing provided in Example 1 of the present invention.
[0029] Figure 2 This is a schematic diagram of an original image of a headlight lens captured by a camera provided in Example 1 of the present invention.
[0030] Figure 3 This is a first image schematic diagram provided by Example 1 of the present invention.
[0031] Figure 4 This is a schematic diagram of an ROI image provided by Example 1 of the present invention.
[0032] Figure 5 This is a schematic diagram of a second image provided by Example 1 of the present invention.
[0033] Figure 6 This is a schematic diagram of a third image provided by Example 1 of the present invention.
[0034] Figure 7 This is a schematic diagram of target segmentation using three target areas in the environment near the halo, provided by Example 1 of the present invention.
[0035] Figure 8 This is a schematic diagram of a headlight lens provided by Example 1 of the present invention when there are noise points in the headlight lens.
[0036] Figure 9a This is a fourth image schematic diagram provided by Example 1 of the present invention.
[0037] Figure 9b This is a sixth image schematic diagram provided by Example 1 of the present invention.
[0038] Figure 10a This is a fifth image schematic diagram provided by Example 1 of the present invention.
[0039] Figure 10b This is a seventh image schematic diagram provided by Example 1 of the present invention.
[0040] Figure 11 This is an eighth image schematic diagram provided by Example 1 of the present invention.
[0041] Figure 12a This is a ninth image schematic diagram provided by Example 1 of the present invention.
[0042] Figure 12b This is an eleventh image schematic diagram provided by Example 1 of the present invention.
[0043] Figure 13aThis is a tenth image schematic diagram provided by Example 1 of the present invention.
[0044] Figure 13b This is a twelfth image schematic diagram provided by Example 1 of the present invention.
[0045] Figure 14 This is a thirteenth image schematic diagram provided by Example 1 of the present invention.
[0046] Figure 15 This is a fourteenth image schematic diagram provided by Example 1 of the present invention.
[0047] Figure 16 This is a fifteenth image schematic diagram provided by Example 1 of the present invention.
[0048] Figure 17 This is a schematic diagram of a custom mask shape provided in Example 1 of the present invention.
[0049] Figure 18 This is a sixteenth image schematic diagram provided by Example 1 of the present invention.
[0050] Figure 19 This is a schematic diagram of the defect detection results of a headlight lens provided by Example 1 of the present invention.
[0051] Figure 20 This is a schematic diagram of a vehicle headlight lens defect detection structure based on traditional image processing provided by Example 2 of the present invention.
[0052] Figure 21 This is a partial block diagram of an electronic device provided in Example 4 of the present invention. DETAILED DESCRIPTION
[0053] Before discussing the exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flow charts. Although the flow charts describe the various operations as sequential processes, many of the operations therein can be implemented in parallel, concurrently, or simultaneously. In addition, the order of the various operations can be rearranged. The process can be terminated when its operation is completed, but can also have additional steps not included in the accompanying drawings. The process can correspond to a method, function, procedure, subroutine, subprogram, etc.
[0054] It should be understood that although the terms "first," "second," and the like may be used herein to describe various elements, these elements should not be limited by these terms. These terms are used solely to distinguish one element from another. For example, a first element may be referred to as a second element, and similarly, a second element may be referred to as a first element, without departing from the scope of the exemplary embodiments. The term "and / or" as used herein includes any and all combinations of one or more of the listed associated items.
[0055] The present invention will now be described in detail with reference to the accompanying drawings. This figure is a simplified schematic diagram, which only illustrates the basic structure of the present invention in a schematic manner, and therefore only shows the components related to the present invention.
[0056] To facilitate subsequent understanding, the professional terms appearing in the following embodiments are explained here:
[0057] OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. It provides a variety of image processing and computer vision functions and is widely used in academic research, industrial production, and commercial products.
[0058] Mask: A mask is a technique used to block (completely or partially) the image being processed using a selected image, graphic, or object to control the area or process of image processing. The specific image or object used for masking is called a mask or template. In optical image processing, masks can be films, filters, and other materials.
[0059] ROI: (region of interest), region of interest.
[0060] Example 1
[0061] For ease of understanding, the overall concept of the invention is explained below before describing the embodiments of the present invention in detail: first, the original image of the lampshade surface is collected, and the angle of the original image is adaptively adjusted; the local adaptive binarization method is used to extract the sample ROI of interest in the original image; the contrast of the defect in the image background is increased by channel superposition; due to the limitations of the high-light environment, the light source forms a reflection area in the center of the inspected part, and it is necessary to perform a preliminary detection on the area first. If no abnormal points or areas are found in the reflection area, all information in the area is filtered out and does not participate in the next step of detection. At this time, image T is obtained; image T is adaptively segmented, and the binarized image is processed with the image ROI, and the inspected part is detected for noise points through the pixel value of a certain pixel. If the pixel value of a certain pixel is greater than the set threshold, there are noise points, and the inspected part is not unqualified. If not, the miscellaneous material area detection is performed.
[0062] The algorithm steps of the debris detection are as follows: 1. Extract the transverse gradient information in the ROI of the inspected part, then use the median filter to filter the random noise in the transverse gradient information map, and finally use the adaptive threshold to perform binary segmentation on the filtered gradient information map to obtain image X; 2. Extract the longitudinal gradient information in the ROI of the inspected part, then use the median filter to filter the random noise in the longitudinal gradient information map, and finally use the adaptive threshold to perform binary segmentation on the filtered gradient information map to obtain image Y; 3. Extract the left oblique angle 45° gradient information in the ROI of the inspected part, and then use the median filter to perform binary segmentation on the left oblique angle 45° Filter the random noise in the 45° gradient information map at right angles, and finally use the adaptive threshold to perform binary segmentation on the filtered gradient information map to obtain image L; 4. Extract the right 45° gradient information in the ROI of the inspected part, and then use the median filter to filter the random noise in the right 45° gradient information map, and finally use the adaptive threshold to perform binary segmentation on the filtered gradient information map to obtain image R; 5. Perform the sum operation on the four images X, Y, L, and R to form the final binary image to obtain image W; 6. Use the custom mask to filter the edges of image W and do not detect defects in the edge area. 7. Finally, the area of each highlight block in the image is calculated to judge the impurities. If there is a highlight block with an area greater than the set threshold, the inspected part is unqualified. If there is no highlight block with an area greater than the set threshold, the inspected part is qualified.
[0063] The specific implementation is as follows:
[0064] like Figure 1 FIG. 1 is a flow chart of a method for detecting defects in a headlight lens based on traditional image processing provided by the present invention.
[0065] As an example, the method includes:
[0066] S1: Collect the original image of the headlight lens through the camera.
[0067] Preferably, reference Figure 2 Schematic diagram of the original image, step S1: collecting the original image of the headlight lens by a camera includes: collecting the original image of the entire lens by using a high-precision industrial camera.
[0068] S2: Preprocess the original image to obtain a first image.
[0069] Preferably, the step S2: pre-processing the original image to obtain the first image includes: S201: rotating the original image by a custom angle; S202: manually positioning the rotated original image to obtain the first image. Specifically, refer to Figure 3The figure shows a schematic diagram of the first image. The original image is rotated at a custom angle, usually 60°. The sample in the original image is manually positioned. This positioning is a rough positioning, and pixel cropping is performed with the center after rotation in step S202 as the origin.
[0070] S3: Using a local binarization method to extract a region of interest in the first image to generate an ROI image, wherein the ROI image only contains the headlight lens information.
[0071] Preferably, reference Figure 4 The figure shows a schematic diagram of an ROI image. Step S3: extracting the region of interest in the first image using a local binarization method to generate an ROI image, wherein the ROI image only contains the headlight lens information, includes: finding the maximum connected domain based on the first image using OpenCV, and performing ROI extraction on it to obtain the ROI image.
[0072] S4: performing single-channel overlay processing on the ROI image to enhance the contrast of defects in the ROI image to obtain a second image.
[0073] S5: Using OpenCV, an average threshold of three regions is taken to perform target segmentation on the environment near the halo of the central light source of the second image.
[0074] S6: The three segmented target areas are stitched together to remove the halo of the central light source of the second image to obtain a third image.
[0075] Preferably, reference Figure 5-6 They are respectively the second image schematic diagram and the third image schematic diagram, combined Figure 7 As shown, the step S6: splicing the three target areas after segmentation and then removing the halo of the central light source of the second image to obtain the third image includes: S601: using the existing interface of Opencv to obtain the three target areas; S602: when calculating the first target area, the grayscale values corresponding to the second target area and the third target area are set to black; S603: the grayscale of the first target area with zero component removed is the first target area to be obtained; S604: when calculating the second target area, the grayscale values corresponding to the first target area and the third target area are set to black; S605: the grayscale of the second target area with zero component removed is the second target area to be obtained; S606: when calculating the third target area, the grayscale values corresponding to the first target area and the second target area are set to black; S607: the grayscale of the third target area with zero component removed is the third target area to be obtained. That is, when removing halo noise, the average grayscale value near the halo is calculated. Due to the limitations of tooling conditions, it is necessary to take the average threshold of three areas for the environment near the halo to perform target segmentation, and finally splice the three segmented areas.
[0076] S7: Adaptively segment the third image using an image binarization segmentation threshold algorithm, and restore the segmented information to the second image.
[0077] S8: Preliminarily determine whether the headlight lens is qualified by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold.
[0078] Preferably, the step S8: preliminarily judging whether the headlight lens is a qualified product by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold value includes: S801: in response to the pixel value corresponding to each pixel point in the restored second image being greater than the preset pixel threshold value, directly judging the headlight lens as a unqualified product; S802: in response to the pixel value corresponding to each pixel point in the restored second image being less than the preset pixel threshold value, going to step S9. Figure 8 This is a schematic diagram of detected noise defects, where the preset pixel threshold is 160, that is, the third image is adaptively segmented, and the segmented information is restored to the second image. The custom threshold 160 is used to judge whether there are noise spots in the sample. If it is greater than the threshold, it is directly judged as a defective product, otherwise step S9 is performed.
[0079] S9: In response to the vehicle headlight lens being qualified, grayscale processing is performed on the ROI image, and then horizontal and vertical gradient information are extracted to obtain a fourth image and a fifth image respectively.
[0080] Preferably, the gradient information extraction formula in step S9 is:
[0081]
[0082] Where, f x (x,y) represents the pixel value on the x-axis corresponding to the pixel point (x, y). Represents the pixel variable in the x-axis direction, f y (x,y) represents the pixel value on the y-axis corresponding to the pixel point (x, y). Indicates the pixel variable in the y-axis direction. Specifically, refer to Figure 9a The fourth image is shown as a schematic diagram, refer to Figure 10a The fifth image is shown as a schematic diagram. Specifically, we calculate the horizontal and vertical gradient information of the image. Gradient information can reflect the rate of change of the image grayscale. The principles for calculating the horizontal, vertical, left-angle 45°, and right-angle 45° gradients are the same. Taking the horizontal gradient information as an example, we will explain it in detail:
[0083] The gradient information is as follows:
[0084]
[0085]
[0086] S10: Perform median filtering on the fourth image and the fifth image respectively to remove random noise.
[0087] S11: By improving the large-scale thresholding method in OpenCV, threshold segmentation is performed on the fourth image and the fifth image after noise filtering based on the custom threshold obtained by the improved large-scale thresholding method to obtain the sixth image and the seventh image.
[0088] Preferably, reference Figure 9b The sixth image is shown as a schematic diagram, refer to Figure 10b The figure shows a schematic diagram of the seventh image. The step S11: by improving the thresholding method of the large law in OpenCV, based on the custom threshold value obtained by the improved thresholding method, the fourth image and the fifth image after the noise is filtered are threshold segmented to obtain the sixth image and the seventh image respectively, including: S1101: performing the operations of steps S1101-S1107 on the fourth image and the fifth image respectively; S1102: calculating the normalized histogram of the image, using P i , i=1,2,3,...255 represents each component of the histogram; S1103: calculate the cumulative sum P I (k), S1104: Calculate the cumulative mean m(k), m(k) = ∑ i k =1 iP i ; S1105: Calculate the global grayscale mean m G ; S1106: Calculate inter-class variance S1107: Obtaining a custom threshold k based on the inter-class variance * ,include: The maximum k value is the custom threshold k * If the maximum value is not unique, take the average value of each k value corresponding to each maximum value as the custom threshold k * ; S1108: Based on the custom threshold k * The fourth and fifth images after noise filtering are threshold segmented to obtain the sixth and seventh images. Since the grayscale value of most backgrounds in the gradient information image is 0, directly using the Otsu method in OpenCV to find the threshold for image segmentation will result in unsatisfactory segmentation results. Therefore, a method is proposed to remove a certain component and then find the class segmentation line.
[0089] S12: performing a sum operation on the sixth image and the seventh image to obtain an eighth image.
[0090] Preferably, reference Figure 11 The figure shows a schematic diagram of the eighth image. The eighth image can be obtained by adding the sixth image and the seventh image using the cv.add() function in OpenCV.
[0091] S13: In response to the vehicle headlight lens being qualified, grayscale processing is performed on the ROI image, and gradient information is extracted in the left oblique angle of 45° and the right oblique angle of 45° to obtain a ninth image and a tenth image respectively.
[0092] S14: Perform median filtering on the ninth image and the tenth image respectively to remove random noise.
[0093] S15: By improving the large-scale thresholding method in OpenCV, the ninth image and the tenth image after the noise is filtered out are threshold segmented based on the custom threshold obtained by the improved large-scale thresholding method to obtain the eleventh image and the twelfth image.
[0094] S16: performing a sum operation on the eleventh image and the twelfth image to obtain a thirteenth image.
[0095] Preferably, reference Figure 12a 、 12b , 13a, 13b, and 14 are respectively the ninth image schematic diagram, the eleventh image schematic diagram, the tenth image schematic diagram, the twelfth image schematic diagram, and the thirteenth image schematic diagram. The operation of steps S13-S16 is the same as that of steps S9-S12, so they are not repeated here.
[0096] S17: performing an AND operation on the thirteenth image and the eighth image to obtain a fourteenth image.
[0097] Preferably, reference Figure 15 , which is a schematic diagram of the fourteenth image. Specifically, the thirteenth image and the eighth image are added together by the cv.add() function in OpenCV to obtain the fourteenth image.
[0098] S18: morphologically processing the fourteenth image to merge objects with similar distances in the fourteenth image to obtain a fifteenth image.
[0099] Preferably, reference Figure 16 Shown is a schematic diagram of the fifteenth image.
[0100] S19: Processing the fifteenth image by using a custom mask to obtain a sixteenth image.
[0101] Preferably, reference Figure 17 Custom mask shapes are shown, see Figure 18 The figure shows a schematic diagram of the sixteenth image. The custom mask is added to the fifteenth image by using the cv.add() function in OpenCV to obtain the sixteenth image.
[0102] S20: Extracting ROI information retained in the sixteenth image.
[0103] S21: Determine whether the headlight lens is qualified based on the ROI information.
[0104] Preferably, reference Figure 19 The figure shows a schematic diagram of the results of a headlight lens defect inspection. Step S21: Determining the conformity of the headlight lens based on the ROI information includes: S2101: Counting the number of pixels occupied by each ROI information and comparing it with a preset pixel threshold; S2102: Determining the conformity of the headlight lens based on the comparison result. Step S2102: Determining the conformity of the headlight lens based on the comparison result includes: S21021: Determining the headlight lens as unqualified if the number of pixels occupied by each ROI information counted is greater than the preset pixel threshold; S21022: Determining the headlight lens as qualified if the number of pixels occupied by each ROI information counted is less than the preset pixel threshold.
[0105] The above embodiment obtains the ROI image by finding the maximum connected domain in the image. By performing subsequent operations only on the region of interest, the computational complexity of subsequent image processing can be effectively reduced and the accuracy of image processing can be improved. By performing binary segmentation on the three regions in the image using three thresholds and removing the halo, the halo caused by reflected light can be prevented from affecting subsequent image recognition and image processing. By summing the gradient information in the four directions of horizontal, vertical, left angle 45°, and right angle 45°, and performing edge detection on the image, the rate of change of the image grayscale can be effectively fed back. By eliminating a certain component to determine the class segmentation threshold, the grayscale value of most background in the gradient information image can be effectively avoided to be 0. If the Otsu method in OpenCV is directly used to determine the threshold for image segmentation, the segmentation effect will be unsatisfactory. By adopting traditional image processing methods, the problems of using deep learning models to process images in the prior art, such as the need for large data sets, the time and effort required to label samples, the time and effort required, the need to retrain the model when the background changes, and the low applicability can be effectively avoided.
[0106] Example 2
[0107] See also Figure 20, this embodiment provides a structural schematic diagram of a headlight lens defect detection system based on traditional image processing.
[0108] As an example, the system includes:
[0109] The image acquisition module 2010 is adapted to acquire the original image of the headlight lens through a camera.
[0110] The image preprocessing module 2011 is adapted to preprocess the original image to obtain a first image.
[0111] The ROI image generating module 2012 is adapted to extract a region of interest in the first image by using a local binarization method to generate a ROI image, wherein the ROI image only includes the headlight lens information.
[0112] The second image generating module 2013 is adapted to perform single-channel overlay processing on the ROI image to enhance the contrast of defects in the ROI image, thereby obtaining a second image.
[0113] The object segmentation module 2014 is adapted to perform object segmentation on the environment near the halo of the central light source of the second image by taking an average threshold of three regions through OpenCV.
[0114] The third image generation module 2015 is adapted to stitch the three segmented target areas together and remove the halo of the central light source of the second image to obtain a third image.
[0115] The adaptive segmentation module 2016 is adapted to adaptively segment the third image using an image binarization segmentation threshold algorithm, and restore the segmented information to the second image.
[0116] The first judgment module 2017 is adapted to preliminarily judge whether the headlight lens is qualified by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold.
[0117] The gradient information extraction module 2018 is adapted to, in response to the vehicle headlight lens being qualified, perform grayscale processing on the ROI image and extract horizontal and vertical gradient information to obtain a fourth image and a fifth image, respectively; and, in response to the vehicle headlight lens being qualified, perform grayscale processing on the ROI image and extract gradient information at a left oblique angle of 45° and a right oblique angle of 45° to obtain a ninth image and a tenth image, respectively.
[0118] The noise filtering module 2019 is adapted to perform median filtering on the fourth image and the fifth image to remove random noise respectively; and perform median filtering on the ninth image and the tenth image to remove random noise respectively.
[0119] The threshold segmentation module 2020 is suitable for improving the large-scale thresholding method in OpenCV, and performing threshold segmentation on the fourth image and the fifth image after the noise is filtered out based on the custom threshold value obtained based on the improved large-scale thresholding method to obtain the sixth image and the seventh image respectively; by improving the large-scale thresholding method in OpenCV, and performing threshold segmentation on the ninth image and the tenth image after the noise is filtered out based on the custom threshold value obtained based on the improved large-scale thresholding method to obtain the eleventh image and the twelfth image respectively.
[0120] The sum operation module 2021 is adapted to perform a sum operation on the sixth image and the seventh image to obtain an eighth image; perform a sum operation on the eleventh image and the twelfth image to obtain a thirteenth image; and perform a sum operation on the thirteenth image and the eighth image to obtain a fourteenth image.
[0121] The morphological processing module 2022 is adapted to perform morphological processing on the fourteenth image so as to merge objects with similar distances in the fourteenth image to obtain a fifteenth image.
[0122] The mask processing module 2023 is adapted to process the fifteenth image using a custom mask to obtain a sixteenth image.
[0123] The ROI information extraction module 2024 is adapted to extract the ROI information retained in the sixteenth image.
[0124] The second judgment module 2025 is adapted to judge whether the headlight lens is qualified based on the ROI information.
[0125] It is not difficult to find that this embodiment is a system embodiment corresponding to the first embodiment, and this embodiment can be implemented in conjunction with the first embodiment. The relevant technical details mentioned in the first embodiment are still valid in this embodiment, and to reduce repetition, they are not repeated here. Accordingly, the relevant technical details mentioned in this embodiment can also be applied to the first embodiment.
[0126] It is worth noting that all modules involved in this embodiment are logical units. In actual applications, a logical unit can be a physical unit, a part of a physical unit, or a combination of multiple physical units. Furthermore, to highlight the innovations of this invention, this embodiment does not include units that are not closely related to solving the technical problems proposed by this invention. However, this does not mean that other units do not exist in this embodiment.
[0127] Example 3
[0128] An embodiment of the present invention further provides a storage medium storing a method for detecting vehicle headlight lens defects based on traditional image processing. When executed by a processor, this program for detecting vehicle headlight lens defects based on traditional image processing implements the steps of the method described above. Because this storage medium incorporates all the technical solutions of all the aforementioned embodiments, it possesses at least all the beneficial effects provided by the technical solutions of these embodiments, and therefore will not be further elaborated upon here.
[0129] Example 4
[0130] See also Figure 21 An embodiment of the present invention further provides an electronic device, comprising: a memory and a processor; the memory stores at least one program instruction; the processor implements the vehicle headlight lens defect detection method based on traditional image processing provided in Example 1 by loading and executing the at least one program instruction.
[0131] The memory 702 and processor 701 are connected using a bus. The bus can include any number of interconnected buses and bridges, connecting various circuits of one or more processors 701 and memory 702. The bus can also connect various other circuits such as peripheral devices, voltage regulators, and power management circuits. These are all well known in the art and, therefore, are not described further herein. The bus interface provides an interface between the bus and the transceiver. The transceiver can be a single component or multiple components, such as multiple receivers and transmitters, providing a unit for communicating with various other devices over a transmission medium. Data processed by the processor 701 is transmitted over a wireless medium via an antenna. Furthermore, the antenna receives data and transmits it to the processor 701.
[0132] The processor 701 is responsible for managing the bus and general processing, and can also provide various functions, including timing, peripheral interfaces, voltage regulation, power management, and other control functions. The memory 702 can be used to store data used by the processor 701 when performing operations.
[0133] The above is only an embodiment of the present invention. Common knowledge such as the known specific structures and characteristics in the scheme is not described in detail here. Ordinary technicians in the field are aware of all common technical knowledge in the technical field of the invention before the application date or priority date, can obtain all existing technologies in the field, and have the ability to apply conventional experimental means before that date. Ordinary technicians in the field can improve and implement this scheme in combination with their own abilities under the inspiration given by this application. Some typical known structures or known methods should not become obstacles for ordinary technicians in the field to implement this application. It should be pointed out that for those skilled in the art, without departing from the structure of the present invention, several variations and improvements can be made, which should also be regarded as the scope of protection of the present invention. These will not affect the effect of the implementation of the present invention and the practicality of the patent. The scope of protection required by this application shall be based on the content of its claims, and the specific implementation methods and other records in the specification can be used to interpret the content of the claims.
Claims
1. A method for detecting defects in vehicle headlight lenses based on traditional image processing, characterized in that: The method comprises: S1: collects the original image of the headlight lens through the camera; S2: Preprocessing the original image to obtain a first image; S3: extracting a region of interest (ROI) from the first image using a local binarization method to generate an ROI image, where the ROI image only contains information about the headlight lens; S4: performing single-channel overlay processing on the ROI image to enhance the contrast of defects in the ROI image to obtain a second image; S5: Using OpenCV, take the average threshold of three regions to perform target segmentation on the environment near the halo of the central light source of the second image; S6: stitching the three segmented target areas and removing the halo of the central light source of the second image to obtain a third image; S7: Adaptively segmenting the third image using an image binarization segmentation threshold algorithm, and restoring the segmented information to the second image; S8: Preliminarily determining whether the headlight lens is qualified by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold; S9: In response to the vehicle headlight lens being qualified, grayscale processing is performed on the ROI image, and then horizontal and vertical gradient information are extracted to obtain a fourth image and a fifth image respectively; S10: performing median filtering on the fourth image and the fifth image respectively to remove random noise; S11: by improving the large-scale thresholding method in OpenCV, based on the custom threshold obtained by the improved large-scale thresholding method, the fourth image and the fifth image after the noise is filtered are threshold segmented to obtain the sixth image and the seventh image; S12: performing a sum operation on the sixth image and the seventh image to obtain an eighth image; S13: In response to the vehicle headlight lens being qualified, grayscale processing is performed on the ROI image, and gradient information is extracted in the directions of 45° left oblique angle and 45° right oblique angle, to obtain a ninth image and a tenth image respectively; S14: performing median filtering on the ninth image and the tenth image respectively to remove random noise; S15: by improving the thresholding method of the big law in OpenCV, based on the custom threshold value obtained by the improved big law thresholding method, the ninth image and the tenth image after the noise is filtered are threshold segmented to obtain the eleventh image and the twelfth image respectively; S16: performing a sum operation on the eleventh image and the twelfth image to obtain a thirteenth image; S17: performing a sum operation on the thirteenth image and the eighth image to obtain a fourteenth image; S18: morphologically processing the fourteenth image to merge objects with similar distances in the fourteenth image to obtain a fifteenth image; S19: performing sum processing on the fifteenth image using a custom mask to obtain a sixteenth image; S20: extracting ROI information retained in the sixteenth image; S21: Determine whether the headlight lens is qualified based on the ROI information.
2. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The step S2: pre-processing the original image to obtain the first image includes: S201: rotating the original image by a custom angle; S202: Manually positioning the rotated original image to obtain a first image.
3. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The step S3: extracting a region of interest in the first image using a local binarization method to generate an ROI image, wherein the ROI image only contains the headlight lens information, includes: finding the maximum connected domain based on the first image using OpenCV, and performing ROI extraction on the domain to obtain the ROI image.
4. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The step S6 of stitching the three segmented target areas and removing the halo of the central light source of the second image to obtain a third image includes: S601: using the existing OpenCV interface to obtain three target areas; S602: When calculating the first target area, the grayscale values corresponding to the second target area and the third target area are set to black; S603: the grayscale of the first target area minus the zero component is the first target area to be acquired; S604: when calculating the second target area, setting the grayscale values corresponding to the first target area and the third target area to black; S605: The grayscale of the second target area minus the zero component is the second target area to be obtained; S606: When calculating the third target area, the grayscale values corresponding to the first target area and the second target area are set to black; S607: The grayscale of the third target area except the zero component is the third target area to be obtained.
5. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The step S8 of preliminarily determining whether the headlight lens is a qualified product by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold value includes: S801: In response to the restored second image containing a pixel point whose pixel value is greater than a preset pixel threshold, directly determining that the headlight lens is a defective product; S802: In response to the pixel value corresponding to each pixel point in the restored second image being less than a preset pixel threshold, go to step S9.
6. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The gradient information extraction formula in step S9 is: Where, f x (x,y) represents the pixel value on the x-axis corresponding to the pixel point (x,y). Represents the pixel variable in the x-axis direction, f y (x,y) represents the pixel value on the y-axis corresponding to the pixel point (x,y). A variable representing pixels in the y-axis direction.
7. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The step S11: improving the thresholding method of the large law in OpenCV, and performing threshold segmentation on the fourth image and the fifth image after the noise is filtered out based on the custom threshold obtained by the improved thresholding method to obtain the sixth image and the seventh image respectively includes: S1101: Perform steps S1101 to S1107 on the fourth image and the fifth image respectively; S1102: Calculate the normalized histogram of the image using P i ,i=1,2,3,...255 represents each component of the histogram; S1103: Calculate cumulative sum P I (k), S1104: Calculate the cumulative mean m(k), S1105: Calculate the global grayscale mean m G ; S1106: Calculate inter-class variance S1107: Obtaining a custom threshold k based on the inter-class variance + ,include: The maximum k value is the custom threshold k * If the maximum value is not unique, take the average value of each k value corresponding to each maximum value as the custom threshold k * ; S1108: Based on the custom threshold k * The fourth image and the fifth image after noise filtering are respectively subjected to threshold segmentation to obtain the sixth image and the seventh image.
8. The vehicle lamp lens defect detection method based on traditional image processing according to claim 1, characterized in that: The step S21 of determining the qualification of the headlight lens based on the ROI information includes: S2101: Counting the number of pixels occupied by each ROI information and comparing it with a preset pixel threshold; S2102: Determine whether the headlight lens is qualified based on the comparison result.
9. The vehicle lamp lens defect detection method based on traditional image processing according to claim 8, characterized in that: The step S2102 of determining whether the headlight lens is qualified based on the comparison result includes: S21021: In response to the fact that the number of pixels occupied by each piece of ROI information counted is greater than a preset pixel threshold, determining that the headlight lens is unqualified; S21022: In response to the fact that the number of pixels occupied by each statistical ROI information is less than a preset pixel threshold, the headlight lens is determined to be qualified.
10. A vehicle lamp lens defect detection system based on traditional image processing, characterized in that: The system adopts the vehicle lamp lens defect detection method based on traditional image processing according to any one of claims 1 to 9, and the system includes: An image acquisition module, adapted to capture the original image of the headlight lens through a camera; An image preprocessing module, adapted to preprocess the original image to obtain a first image; a ROI image generating module adapted to extract a region of interest in the first image using a local binarization method to generate a ROI image, wherein the ROI image only includes information about the headlight lens; a second image generating module adapted to perform single-channel overlay processing on the ROI image to enhance the contrast of defects in the ROI image, thereby obtaining a second image; An object segmentation module is adapted to perform object segmentation on the environment near the halo of the central light source of the second image by taking an average threshold of three regions through OpenCV; Generate a third image module, which is suitable for stitching the three segmented target areas and removing the halo of the central light source of the second image to obtain the third image; An adaptive segmentation module, adapted to adaptively segment the third image using an image binarization segmentation threshold algorithm, and restore the segmented information to the second image; The first judgment module is adapted to preliminarily judge whether the headlight lens is a qualified product by comparing the pixel value corresponding to each pixel point in the restored second image with a preset pixel threshold; a gradient information extraction module, adapted to, in response to the vehicle headlight lens being qualified, perform grayscale processing on the ROI image and extract horizontal and vertical gradient information to obtain a fourth image and a fifth image, respectively; and, in response to the vehicle headlight lens being qualified, perform grayscale processing on the ROI image and extract gradient information at a left oblique angle of 45° and a right oblique angle of 45° to obtain a ninth image and a tenth image, respectively; a noise filtering module adapted to perform median filtering on the fourth image and the fifth image to remove random noise; and to perform median filtering on the ninth image and the tenth image to remove random noise; The threshold segmentation module is suitable for performing threshold segmentation on the fourth image and the fifth image after the noise is filtered out based on the custom threshold value obtained by improving the large-scale threshold method in OpenCV, thereby obtaining the sixth image and the seventh image; and for performing threshold segmentation on the ninth image and the tenth image after the noise is filtered out based on the custom threshold value obtained by improving the large-scale threshold method in OpenCV, thereby obtaining the eleventh image and the twelfth image; a sum operation module, adapted to perform a sum operation on the sixth image and the seventh image to obtain an eighth image; perform a sum operation on the eleventh image and the twelfth image to obtain a thirteenth image; and perform a sum operation on the thirteenth image and the eighth image to obtain a fourteenth image; a morphological processing module, adapted to perform morphological processing on the fourteenth image to merge objects with similar distances in the fourteenth image to obtain a fifteenth image; a mask processing module, adapted to process the fifteenth image using a custom mask to obtain a sixteenth image; an ROI information extraction module, adapted to extract the ROI information retained in the sixteenth image; The second judgment module is adapted to judge the qualification of the headlight lens based on the ROI information.
Citation Information
Patent Citations
A high-speed and accurate LED defect detection method based on machine vision
CN109215026A
SAR image target positioning method, system and device based on ROI region
CN110031843A