A defect detection method based on adaptive template comparison
By generating a template image and performing grayscale alignment and mesh domain division, combined with top-hat operations, the problems of uneven illumination and unclear image edges are solved, thereby improving the accuracy and robustness of defect detection.
Patent Information
- Application Number
- CN202410867577.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-30
- Publication Date
- 2026-11-06
- Estimated Expiration
- 2044-06-30
AI Technical Summary
Existing technologies suffer from uneven lighting and unclear image edges in defect detection, resulting in high false detection rates and size deviations, making them difficult to adapt to changing production scenarios.
By generating mean template maps and standard deviation template maps, image preprocessing and grayscale alignment are performed, grid domains are divided, and defects are identified using an adaptive template comparison method. Combined with top-hat operations, the main body and edges are segmented, and misaligned pixel areas are removed to improve detection accuracy.
It effectively improves the detection rate and reliability of defect detection, reduces false detections and detection size deviations, and adapts to changes in ambient light.
Smart Images

Figure CN119006360B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of image processing and automated detection technology, and in particular to a defect detection method based on adaptive template comparison. Background Technology
[0002] In industrial automated production, defect detection is a crucial step in ensuring product quality. Traditional manual inspection suffers from low efficiency and high false positive rates. In recent years, with the development of computer vision and deep learning technologies, deep learning-based defect detection methods have gradually become a research hotspot. However, these methods often require large amounts of labeled data for training and are difficult to adapt to changing scenarios. Therefore, this invention proposes a defect detection algorithm based on adaptive template comparison to address the aforementioned problems.
[0003] 1. In the existing template comparison algorithm, due to the weakening of the product line light source or the problem of the controller, uneven lighting occurs, resulting in uneven grayscale of the product image under the camera. When comparing templates, the difference from the standard image is greater than the set threshold, causing false detection of the product.
[0004] 2. In existing template comparison algorithms, due to inconsistent product heights or camera out-of-focus issues, the edges of the acquired images may be unclear, with many transitional pixels appearing at the edges. This causes errors in the positioning algorithm, resulting in changes in the corresponding coordinates. Consequently, the detected defects may contain pixels with transitional areas, leading to deviations in the geometric dimensions of the detected defects and causing false product detections. Summary of the Invention
[0005] The main technical problem solved by this invention is to provide a defect detection method based on adaptive template comparison. The method generates templates through image training and divides the templates into grid domains based on the grayscale of the image to be tested. Based on the identification of abnormal defects, it further performs anomaly cleaning, especially for the influence of ambient light, thereby effectively improving the defect detection rate and reliability.
[0006] To solve the above-mentioned technical problems, one technical solution adopted by the present invention is to provide a defect detection method based on adaptive template comparison, comprising the following steps:
[0007] The first step is to load the image training set, train and generate a mean template image based on the gray-level mean of the training set images, and train and obtain a standard deviation template image based on the gray-level standard deviation of the training set images.
[0008] The second step is image preprocessing, which involves position correction and cropping of the grayscale image of the product to be tested.
[0009] The third step is to align the grayscale, divide the grid, form a grayscale set based on the grid, stretch the grayscale of each grid in the mean template image to achieve the grayscale level of each grid in the grayscale image of the product under test, and highlight the defective pixels.
[0010] The fourth step is to identify anomalies using the formula t. u (x,y)=i(x,y)±max{a u ,b u v(x,y)} calculates the upper and lower grayscale thresholds t for normal pixels. u In the formula, i represents the grayscale value of a pixel in the mean template image; (x, y) represents the pixel coordinates; max{…} represents the maximum grayscale value; a u Indicates the first preset parameter; b u This represents the second preset parameter; v represents the grayscale value of the pixel in the standard deviation template image; by comparing thresholds, the abnormal region where the defective pixel is located is obtained;
[0011] The fifth step is to screen for anomalies, cap the abnormal areas, segment the main body and edges, and identify the misaligned pixel areas caused by the position correction error in the second step.
[0012] The sixth step is to identify defects and remove misaligned pixel areas from the abnormal areas to obtain the accurate location of the defects.
[0013] In a preferred embodiment of the present invention, in the first step, a batch of qualified product grayscale images without defects are used to form the image training set; the training method of the mean template image and the standard deviation template image is as follows: for all pixels of each image in the image training set, the mean and standard deviation of pixel grayscale at the same pixel coordinates in each image are calculated, thereby generating the mean template image and the standard deviation template image.
[0014] In a preferred embodiment of the present invention, the grayscale mean of each image in the image training set is controlled to the same grayscale level by a variable scaling method, so that the grayscale values of each image are aligned; the variable scaling method is to multiply the grayscale of all pixels in a single image by a variable scaling method, so that the grayscale mean of the single image is uniformly increased to a preset grayscale.
[0015] In a preferred embodiment of the present invention, in the second step, the grayscale image of the product to be tested is obtained in batches by a vision camera at a fixed position. The position correction method is: using the NCC cross-correlation calculation method and performing normalization processing based on the correlation coefficient to align the related pixels to the same coordinates. The cropping method is: cropping to the corresponding size based on the size of the mean template image.
[0016] In a preferred embodiment of the present invention, in the third step, the grid is divided on the grayscale image of the product under test and the mean template image respectively, forming grid fields of the same size; the method for stretching the grayscale of the grid is as follows: for each grayscale image of the product under test, the mean grayscale value of the image pixels in each grid field of the mean template image is multiplied by a variable factor one by one, and stretched one by one to the same level as the mean grayscale value of the image pixels in each grid field of the grayscale image of the product under test.
[0017] In a preferred embodiment of the present invention, in the fourth step, the defective pixel exhibits both bright area abnormalities and dark area abnormalities; the formula t u (x,y)=i(x,y)±max{a u ,b u v(x,y)} defines anomalies based on the grayscale range, thereby marking pixels whose grayscale values in the grayscale image of the product under test exceed the threshold as abnormal pixels.
[0018] In a preferred embodiment of the present invention, in the fifth step, the top-hat method is as follows: the grayscale image and the mean template image of the product to be tested are processed by the Sobel operator to obtain two grayscale gradient direction maps; for the main body and the edge, the misaligned pixel areas of the main body and the misaligned pixel areas of the edge are screened out by comparing the grayscale mean values of the two grayscale gradient direction maps.
[0019] The beneficial effects of the present invention are as follows: The present invention provides a defect detection method based on adaptive template comparison, which generates templates through image training and divides the templates into grid domains based on the grayscale of the image to be tested. In addition to abnormal defect identification, it further performs abnormal cleaning, especially for the influence of ambient light, thereby effectively improving the defect detection rate and reliability. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort, wherein:
[0021] Figure 1 This is a flowchart of a defect detection method based on adaptive template comparison according to the present invention;
[0022] Figure 2 This is a schematic diagram of template generation for a defect detection method based on adaptive template comparison according to the present invention;
[0023] Figure 3 This is a schematic diagram of mesh domain grayscale alignment for a defect detection method based on adaptive template comparison according to the present invention;
[0024] Figure 4 This is a schematic diagram of anomaly splitting in a defect detection method based on adaptive template comparison according to the present invention;
[0025] Figure 5 This is the grayscale gradient direction map of a defect detection method based on adaptive template comparison according to the present invention. Detailed Implementation
[0026] The technical solutions in the embodiments of the present invention will be clearly and completely described below. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0027] like Figure 1-5 As shown, embodiments of the present invention include:
[0028] A defect detection method based on adaptive template comparison includes the following steps:
[0029] The first step is to load the image training set, train and generate a mean template image based on the gray-level mean of the training set images, and train and obtain a standard deviation template image based on the gray-level standard deviation of the training set images.
[0030] The second step is image preprocessing, which involves position correction and cropping of the grayscale image of the product to be tested.
[0031] The third step is to align the grayscale, divide the grid, form a grayscale set based on the grid, stretch the grayscale of each grid in the mean template image to achieve the grayscale level of each grid in the grayscale image of the product under test, and highlight the defective pixels.
[0032] The fourth step is to identify anomalies using the formula t. u (x,y)=i(x,y)±max{a u ,b u v(x,y)} calculates the upper and lower grayscale thresholds t for normal pixels. u In the formula, i represents the grayscale value of a pixel in the mean template image; (x, y) represents the pixel coordinates; max{…} represents the maximum grayscale value; a u Indicates the first preset parameter; b u This represents the second preset parameter; v represents the grayscale value of the pixel in the standard deviation template image; by comparing thresholds, the abnormal region where the defective pixel is located is obtained;
[0033] The fifth step is to screen for anomalies, cap the abnormal areas, segment the main body and edges, and identify the misaligned pixel areas caused by the position correction error in the second step.
[0034] The sixth step is to identify defects and remove misaligned pixel areas from the abnormal areas to obtain the accurate location of the defects.
[0035] like Figure 2 As shown, in the first step, a batch of grayscale images of qualified products without defects is used to form the image training set; the training method for the mean template image and the standard deviation template image is as follows: for all pixels in each image in the image training set, the mean grayscale value of pixels at the same pixel coordinates in each image is calculated. and standard deviation This generates a mean template image and a standard deviation template image. Here, x1 represents the grayscale value of a pixel at the same pixel coordinate in the first image, x2 represents the grayscale value of a pixel at the same pixel coordinate in the second image, and so on.
[0036] Furthermore, the average grayscale value of each image in the image training set is controlled to the same grayscale level by a variable scaling method, so that the grayscale values of each image are aligned; the variable scaling method is to multiply the grayscale value of all pixels in a single image by a variable scaling method, so that the average grayscale value of a single image is uniformly increased to a preset grayscale value.
[0037] Furthermore, in the second step, the grayscale image of the product to be tested is obtained in batches by a vision camera at a fixed position. The position correction method is: using the NCC cross-correlation calculation method and performing normalization processing based on the correlation coefficient to align the relevant pixels to the same coordinates. The cropping method is: cropping the grayscale image of the product to be tested to the same size as the size of the mean template image.
[0038] Furthermore, in the third step, the grid is divided on the grayscale image of the product under test and the mean template image respectively, forming grid fields of the same size; the method for stretching the grid grayscale is as follows: for each grayscale image of the product under test, the mean grayscale value of the image pixels in each grid field of the mean template image is multiplied by a variable factor one by one, and stretched one by one to the same level as the mean grayscale value of the image pixels in each grid field of the grayscale image of the product under test.
[0039] like Figure 3 As shown, the mean template image and the grayscale image of the product under test are divided into multiple grid regions with a size of 32x32 pixels. If the size of the mean template image and the grayscale image of the product under test are both 64x64, then 2x2 grid regions are divided. The grayscale mean of the mean template image and the grayscale image of the product under test in each grid region is calculated (obtaining the grayscale set of the mean template image [10, 20; 30, 40] and the grayscale set of the product under test image [20, 40; 30, 20]). The grayscale mean of each grid region of the mean template image is stretched to the same level as the grayscale image of the product under test (stretched to [20, 40; 30, 20]).
[0040] In practice, uneven lighting often causes uneven grayscale in the grayscale image of the product under test. In this case, the grayscale of the product under test is [20, 40; 10, 20], and grayscale alignment is required. For the first grid area, the stretching ratio 20 / 10 = 2 should be calculated. Multiply the grayscale of each pixel in the first grid area of the mean template image by 2. Similarly, obtain the stretched grayscale values of all other grid areas. Stretch the mean template image from its original grayscale set [10, 20; 30, 40] to [20, 40; 10, 20] (consistent with the grayscale set of the product under test). After grayscale alignment, when the grayscale mean is stretched according to the ratio, the grayscale of defective pixels will also be stretched synchronously, thus highlighting them.
[0041] For example, in the first grid region, the mean grayscale value of the template image is 10. Assuming the grayscale value of a defect is 15, under uneven lighting conditions, the mean grayscale value of the first grid region in the grayscale image of the product under test would be 20, and the grayscale value of the defect would be 25. Without grayscale alignment, the entire grid region would be identified as a defect, resulting in numerous false positives. To avoid this, an affine transformation is needed to stretch the grayscale values. After grayscale alignment, only grayscale values greater than 25 will be identified as defects.
[0042] Furthermore, in the fourth step, the defective pixels exhibit both bright area anomalies and dark area anomalies; the formula t u (x,y)=i(x,y)±max{a u ,b u v(x,y)} defines anomalies based on the grayscale range, thereby marking pixels whose grayscale values in the grayscale image of the product under test exceed the threshold as abnormal pixels.
[0043] For example, through formula t u (x,y)=i(x,y)+max{a u ,b u Calculate the upper threshold of v(x,y):
[0044] Calculate t u (0,0) = the gray value of the pixel in the mean template (e.g., (0,0)) 5.5 + the maximum value of the two numbers inside the curly braces {parameter (e.g., set to 10), parameter (e.g., set to 5) x standard deviation; the gray value of the pixel in the template (e.g., (0,0)) (1.368)}.
[0045] Then t u (0,0) = 5.5 + take the maximum value of the two numbers inside the curly braces {10, 5 x 1.368 = 6.84};
[0046] Then t u(0,0) = 5.5 + 10 = 15.5;
[0047] This value is the upper limit threshold of the gray level of the image under test at the (0, 0) coordinate.
[0048] Therefore, if the gray value of the image under test is greater than 15.5 at the coordinate (0,0), then this pixel is judged as a defective pixel.
[0049] Similarly, using the formula t u (x,y)=i(x,y)-max{a u ,b u v(x,y)} calculates the lower threshold.
[0050] In the fourth step, as Figure 4 As shown in (a), the above method can obtain the complete set of abnormal coordinates on the grayscale image of the product under test. These abnormalities are usually connected in one area, which includes bright area abnormalities and / or dark area abnormalities, as well as a batch of other abnormalities (here, the out-of-focus outline W2 of the product edge is an example). These other abnormalities are mainly caused by the position correction in the second step. This abnormal pixel area forms a misaligned pixel area. That is to say, when the pixel coordinates are not completely aligned, misaligned coordinates will be generated, and misaligned coordinates will be incorrectly identified as abnormalities.
[0051] In the fifth step, in order to solve the above misalignment problem, it is necessary to separate these misaligned coordinates from the actual brightness and darkness defects.
[0052] First, perform the splitting process:
[0053] Create a rectangle R1 with a width of 2 and a height of 1 (the width can be adjusted according to the positioning accuracy, while the height is fixed at 1) and a rectangle R2 with a width of 1 and a height of 2 (the width is fixed at 1, but the height can be adjusted). Perform a region opening operation on the abnormal coordinate region using R1 and R2 consecutively as the region structure to obtain the main part W1 of the abnormal coordinate region (the actual brightness / darkness defect). Subtract the abnormal coordinate region from the main part to obtain the edge part W2 (misalignment coordinates). Figure 4 As shown in (b).
[0054] Next, the top-hat operation in step 5 is used for comparison. The gray-level gradient direction of all the split abnormal coordinate regions is compared with the gray-level gradient direction of the mean template image. The Sobel operator is used to process the gray-level image of the product under test and the mean template image respectively to obtain two gray-level gradient direction images. For the main part and the edge part respectively, by comparing the gray-level mean of the two gray-level gradient direction images, the misaligned pixel areas of the main part and the misaligned pixel areas of the edge part are screened out.
[0055] like Figure 5As shown, the Sobel operator is used to differentiate pixel values in the horizontal and vertical directions. For any pixel, the gray-level gradient value in the X direction is Gx, and the gray-level gradient value in the Y direction is Gy. Then the gradient angle value of the pixel is arctan(Gx / Gy). In this example, the gradient angle value is set in the range of 0 to 179°.
[0056] The preprocessed image U from the second step is processed using the Sobel operator to obtain the gray-level gradient direction map A of the image. The gray-level mean A1 of the main body is calculated, and the gray-level mean A2 of the edge part is calculated.
[0057] The mean template image is processed using the Sobel filtering method to obtain the gray-level gradient direction map B of the mean template image. The gray-level mean B1 of the main body is calculated, and the gray-level mean B2 of the edge part is calculated.
[0058] If A1-B1 is greater than a certain set coefficient (for example, 5), it is judged as an abnormal coordinate area (defect); if A1-B1 is less than a certain set coefficient, it is judged as a normal coordinate area (false detection).
[0059] Finally, the coordinates of areas determined to be normal are removed from the set of abnormal coordinates obtained in step four, resulting in a cleaned set of abnormal coordinates. This set of abnormal coordinates is the final output result of the defect location.
[0060] The beneficial effects of this invention are as follows:
[0061] (1) Template grayscale adaptation: The template image is divided into grids and adapted to the image to be tested, which can effectively handle the situation of uneven illumination and improve the accuracy and robustness of detection.
[0062] (2) The main body and edge parts are separated by the top cap operation to improve the accuracy of the detection results.
[0063] In summary, this invention provides a defect detection method based on adaptive template comparison. It generates templates through image training and divides the templates into grid domains based on the grayscale of the image under test. Furthermore, based on the identification of abnormal defects, it performs further anomaly cleaning, especially targeting the influence of ambient light, thereby effectively improving the defect detection rate and reliability.
[0064] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention specification, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.
Claims
1. A defect detection method based on adaptive template comparison, characterized in that, Includes the following steps: The first step is to load the image training set, train and generate a mean template image based on the gray-level mean of the training set images, and train and obtain a standard deviation template image based on the gray-level standard deviation of the training set images. The second step is image preprocessing, which involves position correction and cropping of the grayscale image of the product to be tested. The third step is to align the grayscale, divide the grid, form a grayscale set based on the grid, stretch the grayscale of each grid in the mean template image to achieve the grayscale level of each grid in the grayscale image of the product under test, and highlight the defective pixels. The fourth step is to identify anomalies using the formula t. u (x, y) = i(x, y) ± max{a u b u v(x, y)} calculates the upper and lower grayscale thresholds t for normal pixels. u In the formula, i represents the grayscale value of a pixel in the mean template image; (x, y) represents the pixel coordinates; max{…} represents the maximum grayscale value; a u Indicates the first preset parameter; b u This represents the second preset parameter; v represents the grayscale value of the pixel in the standard deviation template image; by comparing thresholds, the abnormal region where the defective pixel is located is obtained; The fifth step is to screen for anomalies, cap the abnormal areas, segment the main body and edges, and identify the misaligned pixel areas caused by the position correction error in the second step. The sixth step is to identify defects and remove misaligned pixel areas from the abnormal areas to obtain the accurate location of the defects.
2. The defect detection method based on adaptive template comparison according to claim 1, characterized in that, In the first step, a batch of grayscale images of qualified products without defects are used to form the image training set; The training method for the mean template image and the standard deviation template image is as follows: for all pixels in each image in the image training set, calculate the mean and standard deviation of pixel grayscale values at the same pixel coordinates in each image, thereby generating the mean template image and the standard deviation template image.
3. The defect detection method based on adaptive template comparison according to claim 2, characterized in that, The average grayscale value of each image in the image training set is controlled to the same grayscale level by a variable scaling method, so that the grayscale values of each image are aligned. The variable scaling method is to multiply the grayscale of all pixels in a single image by a variable scaling method, so that the average grayscale value of the single image is uniformly increased to a preset grayscale.
4. The defect detection method based on adaptive template comparison according to claim 1, characterized in that, In the second step, the grayscale image of the product to be tested is obtained in batches by a vision camera at a fixed position. The position correction method is to use the NCC cross-correlation calculation method and perform normalization processing based on the correlation coefficient to align the relevant pixels to the same coordinates. The cropping method is to crop the image to the corresponding size based on the size of the mean template image.
5. The defect detection method based on adaptive template comparison according to claim 1, characterized in that, In the third step, the grid is divided on the grayscale image of the product under test and the mean template image respectively, forming grid fields of the same size; the method of stretching the grid grayscale is as follows: for each grayscale image of the product under test, the mean grayscale value of the image pixels in each grid field of the mean template image is multiplied by a variable magnification one by one, and stretched one by one to the same level as the mean grayscale value of the image pixels in each grid field of the grayscale image of the product under test.
6. The defect detection method based on adaptive template comparison according to claim 1, characterized in that, In the fourth step, the defective pixels exhibit both bright area anomalies and dark area anomalies; the formula t u (x,y)=i(x,y)±max{a u ,b u v(x,y)} defines anomalies based on the grayscale range, thereby marking pixels whose grayscale values in the grayscale image of the product under test exceed the threshold as abnormal pixels.
7. The defect detection method based on adaptive template comparison according to claim 1, characterized in that, In the fifth step, the top-hat method is as follows: the grayscale image and the mean template image of the product to be tested are processed by the Sobel operator to obtain two grayscale gradient direction maps; for the main body and the edge, the misaligned pixel areas of the main body and the misaligned pixel areas of the edge are screened out by comparing the grayscale mean values of the two grayscale gradient direction maps respectively.
Citation Information
Patent Citations
Product edge defect detection method
CN112330678A
Railway fastener defect online detection and identification method based on machine vision
CN116757990A
