An open defect detection method based on template matching
By using template matching and distortion correction methods, the problem of sample dependence in existing defect detection is solved, achieving efficient and accurate defect detection that is applicable to various production environments and products.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUAZHONG UNIV OF SCI & TECH
- Filing Date
- 2024-01-23
- Publication Date
- 2026-06-26
AI Technical Summary
Existing defect detection methods rely on a large number of defect samples and a complex training process, resulting in high hardware and computing power requirements, making them difficult to apply in efficient, high-speed production environments.
An open defect detection method based on template matching is adopted. By correcting the distortion of the template image and the actual image and registering them at the pixel level, combined with grid feature descriptors and breadth-first traversal algorithm, the automatic detection of defect regions is achieved.
It achieves efficient and accurate defect detection without relying on defect samples, and is applicable to various production environments and product types, improving detection speed and accuracy.
Smart Images

Figure CN118015310B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of industrial defect detection technology, and more specifically, relates to an open defect detection method based on template matching. Background Technology
[0002] In recent years, with the continuous development of industrial production, the requirements for product quality have become increasingly stringent. During the production process, the presence of defects can seriously affect product quality and performance, and may even lead to product scrapping and losses. Therefore, defect detection has become a very important aspect of industrial production.
[0003] Currently, defect detection mainly relies on manual visual inspection and AI-based automated inspection methods. Manual visual inspection is a traditional method that requires professionals to visually inspect products to determine if defects exist. However, manual visual inspection requires significant manpower and time, is easily affected by subjective factors, and is unsuitable for efficient, high-speed production environments. On the other hand, AI-based automated inspection methods utilize machine learning and deep learning algorithms to automatically identify and detect defects with high accuracy and efficiency. However, this method requires a large number of defect samples for training and involves a complex training process, placing high demands on hardware and computing power, thus limiting its application scope. Summary of the Invention
[0004] To address the shortcomings and improvement needs of existing technologies, this invention provides an open defect detection method based on template matching, which aims to avoid the problem of high dependence on defect samples in existing defect detection methods that use AI technology.
[0005] To achieve the above objectives, according to one aspect of the present invention, an open defect detection method based on template matching is provided, comprising:
[0006] S1. Obtain the actual image and template image of the product under test by line scanning imaging. By deleting the black background at the front end of the template image or the actual image, the head position of the product is aligned when the template image and the actual image are aligned by column. The template image is a line scanning imaging image of the product under test without defects.
[0007] S2. Determine the location of each local distortion in the middle of the product in the actual image obtained in S1, and based on the product area corresponding to each local distortion location, determine the corresponding position of that area in the template image obtained in S1. Based on the column number of that area in the template image, perform column number correction on the local distortion location in the actual image obtained in S1 to obtain the distortion-corrected actual image.
[0008] S3. Using the homography matrix, pixel-level registration is performed between the template image and the actual image after distortion correction for each part of the product to be tested.
[0009] S4. In the pixel-level registered actual image and template image, the local positions corresponding to each product area to be detected are uniformly divided into grids. The feature descriptors of each grid corresponding to each product area to be detected in the two images are calculated. By the similarity between the feature descriptors of each matching grid point pair, each abnormal grid in the actual image is determined. Based on each abnormal grid, a breadth-first traversal algorithm is used to determine each connected component, which is clustered as an abnormal grid to form a defect region.
[0010] Furthermore, prior to S1, the following is also included:
[0011] S0. Divide the actual image and template image of the product under test by line scanning imaging into sub-images according to the preset sub-image step size; compare the grayscale statistics of adjacent sub-images in the order of front to back and back to front of each image to determine the sub-images where the product head and product tail are located, and delete the sub-images before the sub-image where the product head is located and after the sub-image where the product tail is located in the image to form the trimmed actual image and template image.
[0012] The open defect detection method performs S1 based on the pruned actual image and the template image.
[0013] Furthermore, in step S0, the method for determining the subgraph containing the product head or the product tail is as follows:
[0014] S010. Traverse each image in front-to-back or back-to-front order, and process the sub-image F. i Subgraph F i-1 Convert to grayscale image, and count the frequency of occurrence of different grayscale values to obtain grayscale statistics H. i With H i-1 Calculate H i With H i-1 Similarity between them;
[0015] S020. If the similarity is greater than the threshold, continue traversing the next subgraph, based on subgraph F. i+1 Subgraph F i Execute S010; otherwise, execute S030.
[0016] S030, Detection Subgraph F i+1 Subgraph F i+2 Feature points and corresponding statistical counts FN i+1 and FN i+2 If FN i+1 and FN i+2 All are greater than the threshold T FNThen determine the subgraph F i If the sub-image contains the product header or product footer, delete sub-image F. i If all subgraphs before or after F have a black background, then continue iterating through the next subgraph, based on subgraph F. i+1 Subgraph F i Execute S010.
[0017] Furthermore, in S1, the method for aligning the product header position when the template image and the actual image are aligned by column is as follows:
[0018] S110. Using feature detection and feature matching algorithms, feature point matching is performed between the front part of the template image containing the product head and the front part of the actual image containing the product head. Feature points in the template image of each matched feature point pair are added to the feature point set S. T In the middle, feature points located in the actual image are placed into the feature point set S. D middle;
[0019] S120, take S T The feature point C with the smallest x-axis coordinate T and S D In and C T Matched feature points C D If C D In S D If the x-axis coordinate of the feature point pair is the smallest, then the difference between the two x-axis coordinates of this feature point pair is used as the product head offset between the template image and the actual image; otherwise, C is used separately. T and C D From S T and S D Delete, and repeat the step; where the x-axis is the scanning direction of the line scan imaging process;
[0020] S130. Delete the front black background portion of the template image or the actual image with a length equal to the product head offset, so as to achieve product head position alignment when the template image and the actual image are aligned.
[0021] Furthermore, S2 is implemented as follows:
[0022] S210. Divide the template image and the actual image obtained in S1 into local images according to the same local step size d and scanning step size s;
[0023] S220. Calculate the local image in the template image consisting of columns i to i+d. And a partial image composed of columns i to i+d in the actual image. The local similarity between the template image and the actual image is calculated. If the local similarity is greater than the threshold, it means that there is no distortion in the local position between the i-th column and the (i+d)-th column in the actual image. Let i = i+s, and repeat the calculation of local similarity. If the local similarity is not greater than the threshold, it means that there is distortion in the local position between the i-th column and the (i+d)-th column in the actual image. Correct the distortion in the local position, and then let i = i+s, and repeat the calculation of local similarity. This step S220 ends when the template image or the actual image is scanned.
[0024] Furthermore, the metric for measuring local similarity is:
[0025] In the formula, and The OTSU binarization algorithm was used to perform the following operations: and The resulting binarized local image is processed.
[0026] Furthermore, the method for correcting the distortion at this local location is as follows:
[0027] (1) Determine the starting position of the distortion in the actual image at this local location:
[0028] S221. Initialize the left boundary column L = is and the right boundary column R = i of the search interval;
[0029] S222. Let m = (L + R) / 2, calculate... and If the similarity is greater than the threshold, the left boundary L = m and the right boundary R = i are set, and the similarity calculation of this step is performed again; if the similarity is not greater than the threshold, the left boundary L = is and the right boundary R = m are set, and the similarity calculation of this step is performed again.
[0030] S223, Execute S222 until it shrinks to one column, and use that as the start position for the distortion;
[0031] (2) Determine the end position of the distortion in the actual image and the position of the product area corresponding to the end position in the template image:
[0032] The end point of the distortion in this local location in the actual image is fixed as end. D =i+s;
[0033] Search for the end on the template image D The corresponding product area locations are determined as follows: Calculate the values of p in ascending order of their absolute values. and The similarity, where p is an integer; the template image corresponding to the one with a similarity greater than a threshold is selected. The column is used as the end position of distortion, and the process ends. If none of them are greater than the threshold, then the th column in the template image with the highest similarity is selected. The column is the end position of the distortion. T ;
[0034] (3) Distortion correction:
[0035] From column start to column end in the actual image D Local positions between columns are stretched or squeezed along the x-axis to the number of columns end. T -start, where the x-axis is the scanning direction of the line scan imaging process.
[0036] Furthermore, S3 is implemented as follows:
[0037] The template image obtained in S1 and the actual image after distortion correction in S2 are divided according to the product area to be inspected;
[0038] Feature detection and feature matching algorithms are used to match feature points between each part of the template image to be detected and the corresponding part of the actual image; based on the coordinate correspondence between each matched feature point pair, a homography matrix is fitted.
[0039] Multiplying the homography matrix by the corresponding image portion of the component to be detected in the template image yields a new image portion of the component to be detected in the actual image, thus achieving pixel-level alignment between the actual image and the template image.
[0040] The present invention also provides a computer-readable storage medium comprising a stored computer program, wherein, when the computer program is executed by a processor, it controls the device where the storage medium is located to perform an open defect detection method based on template matching as described above.
[0041] In summary, the above-described technical solutions conceived in this invention can achieve the following beneficial effects:
[0042] This invention proposes a template-matching-based open defect detection method. This method addresses image distortion caused by the missynchronization of scanning speed and frequency during line scanning imaging, thereby enabling the comparison between the image to be detected and a template image, thus achieving open defect detection—a novel defect detection process. Specifically, the method first acquires the actual image of the object to be detected using line scanning imaging technology. The actual image is then preprocessed by performing fuzzy matching based on the template image. Next, precise registration is achieved through precise matching based on the template image. The template image is a standard, defect-free image used to compare with the actual image to identify defects. Template matching utilizes features such as grayscale, color, and texture to calculate the similarity between the actual image and the template image, thereby determining the presence of distortion. If the similarity is below a set threshold, distortion is identified and corrected. Subsequently, pixel-level registration of each detectable part of the product under test between the template image and the distortion-corrected actual image is performed using a homography matrix. Finally, defect detection is performed based on the precisely registered images. In the pixel-level registered actual image and template image, the local locations corresponding to each product region to be detected are uniformly divided into grids. The feature descriptors of each grid corresponding to each product region to be detected in the two images are calculated. By analyzing the similarity between the feature descriptors of each matching grid point pair, abnormal grids in the actual image are identified. Based on each abnormal grid, a breadth-first search algorithm is used to determine each connected component, which is then clustered as an abnormal grid to form a defect region. This invention achieves template matching through fuzzy registration, distortion correction, and other methods, followed by comparative detection. This enables open defect detection that is independent of defect samples, requires no training, and has strong generalization capabilities. It improves the accuracy and efficiency of detection and is an adaptive template matching method. This method can automatically adjust the feature weights and similarity thresholds of the template image according to different detection objects and environmental conditions, thereby adapting to different detection needs. Attached Figure Description
[0043] Figure 1 A flowchart of an open defect detection method based on template matching is provided in an embodiment of the present invention;
[0044] Figure 2 This is a schematic diagram of a regional connectivity algorithm provided in an embodiment of the present invention;
[0045] Figure 3 This is a schematic diagram of the fuzzy registration algorithm provided in an embodiment of the present invention;
[0046] Figure 4 This is a schematic diagram of the distortion correction algorithm provided in an embodiment of the present invention. Detailed Implementation
[0047] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention. Furthermore, the technical features involved in the various embodiments of this invention described below can be combined with each other as long as they do not conflict with each other.
[0048] Example 1
[0049] An open defect detection method based on template matching, such as Figure 1 As shown, it includes:
[0050] S1. Obtain the actual image and template image of the product under test by line scanning imaging. By deleting the black background at the front end of the template image or the actual image, the head position of the product is aligned when the template image and the actual image are aligned by column. The template image is a line scanning imaging image of the product under test without defects.
[0051] S2. Determine the location of each local distortion in the middle of the product in the actual image obtained in S1, and based on the product area corresponding to each local distortion location, determine the corresponding position of that area in the template image obtained in S1. Based on the column number of that area in the template image, perform column number correction on the local distortion location in the actual image obtained in S1 to obtain the distortion-corrected actual image.
[0052] S3. Using the homography matrix, pixel-level registration is performed between the template image and the actual image after distortion correction for each part of the product to be tested.
[0053] S4. In the pixel-level registered actual image and template image, the local positions corresponding to each product area to be detected are uniformly divided into grids. The feature descriptors of each grid corresponding to each product area to be detected in the two images are calculated. By the similarity between the feature descriptors of each matching grid point pair, each abnormal grid in the actual image is determined. Based on each abnormal grid, a breadth-first traversal algorithm is used to determine each connected component, which is clustered as an abnormal grid to form a defect region.
[0054] In step S4 of this embodiment, a grid is evenly distributed between the parts of the image to be detected and the template image to be detected. An algorithm such as SIFT is used to compare the feature descriptors f at corresponding positions on the grid. T and f D This is used to detect dissimilar grid point regions. The clustering task of dissimilar grid points is transformed into a graph connectivity problem, enabling the connection of dissimilar grid points to obtain defect locations. This embodiment transforms the grid point clustering problem into a graph connectivity problem, such as... Figure 2This process, which uses a breadth-first strategy for connectivity, can efficiently and accurately cluster dissimilar grid points, encompassing the areas where defects are located and accurately labeling them.
[0055] The comparison of feature descriptors can be achieved using Euclidean distance, which measures the similarity between two grid points. The calculation method is as follows:
[0056]
[0057] If the distance d is greater than the threshold T d If the grid point in the image to be detected is not similar to the modal image, then the grid point in the image to be detected will be marked as not similar to the modal image.
[0058] In step S4, dissimilar grid points are aggregated, transforming the problem into a graph connectivity problem. The connectivity relationship between grid points is defined as adjacency; that is, adjacent points are defined as points located at the top, bottom, left, right, top-left, top-right, bottom-left, and bottom-right. A simple breadth-first search algorithm can be used to cluster the grid points, achieving accurate and efficient result aggregation.
[0059] In summary, the method in this embodiment is based on template matching using fuzzy registration and distortion correction, and then uses mesh comparison and region connectivity to cluster and label defects. In other words, it performs comparative detection by matching with existing templates, effectively detecting defects in industrial production. This approach enables open defect detection that is computationally more efficient and has a high degree of recognition for occasional anomalies, without relying on defect samples. Compared with traditional manual visual inspection and AI-based automatic inspection methods, this method is simple, fast, and accurate, and is applicable to various production environments and product types.
[0060] Furthermore, by introducing new adaptive adjustment and fast matching algorithms at each step of the method, the accuracy and efficiency of detection can be improved, giving this invention broad application prospects and economic benefits. Specifically, the preferred embodiment of the method is as follows:
[0061] As a preferred embodiment, before S1 above, the method further includes:
[0062] S0. Divide the actual image of the product under test and the template image of the line scan imaging into sub-images according to the preset sub-image step size M, and set the i-th sub-image as F. i The grayscale statistics of adjacent sub-images are compared sequentially from front to back and from back to front for each image to determine the sub-images containing the product head and product tail. Sub-images before the sub-image containing the product head and after the sub-image containing the product tail are deleted to form the cropped actual image and template image.
[0063] The open defect detection method in this embodiment performs the above S1 based on the trimmed actual image and the template image.
[0064] By dividing the image into sub-images, the background sub-image is directly filtered using the grayscale histogram, achieving large-scale background removal. S1 above essentially performs blur registration between the actual image and the template image, such as... Figure 2 As shown, executing S0 before executing S1 can reduce the image size and improve the execution efficiency of S1 and its subsequent operations.
[0065] As a preferred implementation, in the above S0, the method for determining the sub-image containing the product head or the product tail is as follows:
[0066] S010. Traverse each image in front-to-back or back-to-front order, and process the sub-image F. i Subgraph F i-1 Convert to grayscale image, and count the frequency of occurrence of different grayscale values to obtain grayscale statistics H. i With H i-1 Calculate H i With H i-1 Similarity between them;
[0067] S020. If the similarity is greater than the threshold, continue traversing the next subgraph, based on subgraph F. i+1 Subgraph F i Execute S010; otherwise, execute S030.
[0068] S030, Detection Subgraph F i+1 Subgraph F i+2 Feature points and corresponding statistical counts FN i+1 and FN i+2 If FN i+1 and FN i+2 All are greater than the threshold T FN Then determine the subgraph F i If the sub-image contains the product header or product footer, delete sub-image F. i If all subgraphs before or after F have a black background, then continue iterating through the next subgraph, based on subgraph F. i+1 Subgraph F i Execute S010.
[0069] After determining the location of the sub-image where the car's front appears, it's necessary to further remove the black background within the sub-image area to align the front position. To avoid errors caused by subsequent distortion accumulation, it's necessary to select feature points from the front for matching as much as possible. Let the superscript T represent the template image-related data, and the superscript D represent the actual image-related data.
[0070] As a preferred embodiment, in the above S1, the alignment of the product header position when the template image and the actual image are aligned by column is achieved as follows:
[0071] S110. Using feature detection and feature matching algorithms, feature point matching is performed between the front part of the template image containing the product head and the front part of the actual image containing the product head. Feature points in the template image of each matched feature point pair are added to the feature point set S. T In the middle, feature points located in the actual image are placed into the feature point set S. D middle;
[0072] S120, take S T The feature point C with the smallest x-axis coordinate T and S D In and C T Matched feature points C D If C D In S D If the x-axis coordinate of the feature point pair is the smallest, then the difference between the two x-axis coordinates of this feature point pair is used as the product head offset between the template image and the actual image; otherwise, C is used separately. T and C D From S T and S D Delete, and repeat the step; where the x-axis is the scanning direction of the line scan imaging process;
[0073] S130. Delete the front black background portion of the template image or the actual image with a length equal to the product head offset, so as to achieve product head position alignment when the template image and the actual image are aligned.
[0074] It should be noted that this specific step can be performed after S0. In this case, the aforementioned image front portion containing the product head can be exemplarily selected as the image portion within a range of five sub-images starting from the sub-image where the vehicle head is located. Preferably, when removing the black background within the sub-images, to prevent errors caused by cumulative distortion, the offset is calculated using the matching feature point closest to the vehicle head position, thereby removing the black background in front of the vehicle head. Figure 3 As shown, the black background is filtered by comparing the similarity of the foreground and background sub-images at the front of the car. The number of feature points detected after the front of the car is further confirmed to determine the position of the sub-image where the front of the car appears. Then, a feature detection algorithm is used to match feature points as close as possible to the front of the car to calculate the offset of the front of the car, thereby removing the black background within the sub-image range and achieving complete alignment of the front of the car. Alternatively, this specific step can also be executed directly when the method directly executes S1. However, regardless of the method, to avoid errors caused by the accumulation of subsequent distortion, it is necessary to select the foreground feature points for matching as much as possible.
[0075] Due to hardware limitations in line scan imaging, even when the template image and the actual image are aligned, distortion will occur in the middle of the product after the head position is aligned, resulting in misalignment of the middle of the product. Therefore, the method in this embodiment needs to execute the above-mentioned S2. As a preferred implementation, the above-mentioned S2 is implemented as follows:
[0076] S210. Divide the template image and the actual image obtained in S1 into local images according to the same local step size d and scanning step size s;
[0077] S220. Calculate the local image in the template image consisting of columns i to i+d. And a partial image composed of columns i to i+d in the actual image. The local similarity between the template image and the actual image is calculated. If the local similarity is greater than the threshold, it means that there is no distortion in the local position between the i-th column and the (i+d)-th column in the actual image. Let i = i+s, and repeat the calculation of local similarity. If the local similarity is not greater than the threshold, it means that there is distortion in the local position between the i-th column and the (i+d)-th column in the actual image. Correct the distortion in the local position, and then let i = i+s, and repeat the calculation of local similarity. This step S220 ends when the template image or the actual image is scanned.
[0078] In other words, by using step-size scanning and local similarity comparison, the location of local distortion is detected and corrected accordingly, so as to make the distortion of the image to be detected consistent with that of the template image, so that the two are aligned and the distortion correction is achieved.
[0079] As a preferred embodiment, the above-mentioned measure of local similarity is: In the formula, and They are respectively and Binarized local image obtained using the OTSU binarization algorithm.
[0080] The similarity criterion is to first perform binarization using OTSU, and then calculate the average difference. The OTSU binarization algorithm has the advantages of fast computation speed and automatic selection of the optimal threshold, requiring no manual intervention. In addition, OTSU assumes that the image has a bimodal gray-level distribution, which is suitable for the need to separate the foreground and background in this scenario.
[0081] As a preferred embodiment, the above-described method for correcting distortion at this local location is as follows:
[0082] (1) Determine the starting position of the distortion in the actual image at this local location:
[0083] S221. Initialize the left boundary column L = is and the right boundary column R = i of the search interval;
[0084] S222. Let m = (L + R) / 2, calculate... and If the similarity is greater than the threshold, the left boundary L = m and the right boundary R = i are set, and the similarity calculation of this step is performed again; if the similarity is not greater than the threshold, the left boundary L = is and the right boundary R = m are set, and the similarity calculation of this step is performed again.
[0085] S223, Execute S222 until it shrinks to one column, and use that as the start position for the distortion;
[0086] (2) Determine the end position of the distortion in the actual image and the position of the product area corresponding to the end position in the template image:
[0087] The end point of the distortion in this local location in the actual image is fixed as end. D =i+s;
[0088] Search for the end on the template image D The corresponding product area locations are determined as follows: Calculate the values of p in ascending order of their absolute values. and The similarity, where p is an integer; the template image corresponding to the one with a similarity greater than a threshold is selected. The column is used as the end position of distortion, and the process ends. If none of them are greater than the threshold, then the th column in the template image with the highest similarity is selected. The column is the end position of the distortion. T ;
[0089] (3) Distortion correction:
[0090] From column start to column end in the actual image D Local positions between columns are stretched or squeezed along the x-axis to the number of columns end. T -start, where the x-axis is the scanning direction of the line scan imaging process.
[0091] In this preferred embodiment, a binary search algorithm is used to locate the starting position of the distortion. Specifically, as follows: Figure 4 As shown, in step 3 of confirming distortion, a binary search algorithm is first used on the left side to calculate the start position of the distortion. Then, a fixed segment is used on the right side to gradually determine the end position of the distortion, achieving highly efficient distortion correction. Furthermore, locating the end position of the distortion is more difficult. To reduce complexity, the end position of the distortion in the actual image is fixed as 'end'. D=i+s, searching for the corresponding distortion end position on the template image. That is, by balancing the computational cost, fixing the distortion end position of the image to be detected, and probing to determine the distortion end position of the template image, efficient distortion correction can be achieved.
[0092] Furthermore, the actual image and the template image are cut into equal segments, that is, cut according to the parts to be inspected of the product under test. To prevent residual distortion from affecting defect detection, before defect detection, the parts to be inspected in the actual image and the template image need to be precisely registered using a homography matrix to achieve pixel-level alignment. As a preferred implementation, the above S3 is implemented as follows:
[0093] The template image obtained in S1 and the actual image after distortion correction in S2 are divided according to the product area to be inspected;
[0094] Feature detection and feature matching algorithms are used to match feature points between each part of the template image to be detected and the corresponding part of the actual image; based on the coordinate correspondence between each matched feature point pair, a homography matrix is fitted.
[0095] Based on the homography matrix, pixel-level alignment between the actual image and the template image is achieved, and the alignment method is as follows:
[0096] P′=M·P D .
[0097] In other words, the actual image and the template image are divided equally according to the product parts. The coordinate correspondence between the matched feature points is fitted to obtain the homography matrix M, and the part image of the actual image is aligned with the template image.
[0098] Example 2
[0099] A computer-readable storage medium includes a stored computer program, wherein when the computer program is executed by a processor, it controls the device where the storage medium is located to perform an open defect detection method based on template matching as described above.
[0100] The relevant technical solutions are the same as in Embodiment 1, and will not be repeated here.
[0101] In summary, this invention relates to an open defect detection method based on template matching. It addresses image distortion caused by the missynchronization of scanning speed and frequency during line scanning imaging through fuzzy and precise matching. The method then compares the actual image with a template image to ultimately detect open defects. The invention comprises two parts: template matching and defect detection. Template matching aligns the distortions in the actual image with those in the template image, ensuring that corresponding areas are captured correctly. Defect detection, based on template matching, compares the aligned actual image with the template image to detect areas of significant difference, which are identified as defect regions. Compared to AI-based methods, the template matching-based open defect detection method proposed in this invention does not rely on defect samples and requires no cumbersome training. It only needs to adjust a few hyperparameters, thus having a wider range of applications.
[0102] Those skilled in the art will readily understand that the above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. An open defect detection method based on template matching, characterized in that, include: S1. Obtain the actual image and template image of the product under test by line scanning imaging. By deleting the black background at the front end of the template image or the actual image, the head position of the product is aligned when the template image and the actual image are aligned by column. The template image is a line scanning imaging image of the product under test without defects. S2. Determine the location of each local distortion in the middle of the product in the actual image obtained in S1, and based on the product area corresponding to each local distortion location, determine the corresponding position of that area in the template image obtained in S1. Based on the column number of that area in the template image, perform column number correction on the local distortion location in the actual image obtained in S1 to obtain the distortion-corrected actual image. S3. Using the homography matrix, pixel-level registration is performed between the template image and the actual image after distortion correction for each part of the product to be tested. S4. In the pixel-level registered actual image and template image, the local positions corresponding to each product area to be detected are uniformly divided into grids. The feature descriptors of each grid corresponding to each product area to be detected in the two images are calculated. By the similarity between the feature descriptors of each matching grid point pair, each abnormal grid in the actual image is determined. Based on each abnormal grid, a breadth-first traversal algorithm is used to determine each connected component, which is clustered as an abnormal grid to form a defect region. The method for correcting distortion at this local location is as follows: (1) Determine the starting position of the distortion in the actual image at this local location: S221. Initialize the left boundary column of the search interval. Right boundary column ; S222, Order ,calculate and If the similarity is greater than a threshold, then a left boundary is set. Right boundary Then, re-perform the similarity calculation for this step; if the similarity is not greater than the threshold, set the left boundary. Right boundary And then re-perform the similarity calculation in this step; where, This represents a local position in the template image consisting of columns m to m+d. This represents a local location in the actual image consisting of columns m to m+d. Indicates the column number of the image. Indicates the scan step size of the search interval; S223, Execute S222 until it shrinks to one column, and use that as the start position for the distortion; (2) Determine the end position of the distortion in the actual image and the position of the product area corresponding to the end position in the template image: The end position of the distortion in this local location in the actual image is fixed as ; Searching for the template image The corresponding product area locations are determined as follows: Calculate the values of p in ascending order of their absolute values. and The similarity, where p is an integer; the template image corresponding to the one with a similarity greater than a threshold is selected. As the end point of distortion, the process ends. If none of the values are greater than the threshold, then the template image with the highest similarity is selected. As the end of the distortion ; (3) Distortion correction: Move the starting column in the actual image to the... Local positions between columns are stretched or squeezed along the x-axis to a number of columns. -start, where the x-axis is the scanning direction of the line scan imaging process.
2. The open defect detection method according to claim 1, characterized in that, Before S1, it also includes: S0. Divide the actual image and template image of the product under test by line scanning imaging into sub-images according to the preset sub-image step size; compare the grayscale statistics of adjacent sub-images in the order of front to back and back to front of each image to determine the sub-images where the product head and product tail are located, and delete the sub-images before the sub-image where the product head is located and after the sub-image where the product tail is located in the image to form the trimmed actual image and template image. The open defect detection method performs S1 based on the pruned actual image and the template image.
3. The open defect detection method according to claim 2, characterized in that, In step S0, the method for determining the subgraph containing the product head or the product tail is as follows: S010. Traverse each image in front-to-back or back-to-front order, and sub-images Hezi Diagram Convert to grayscale image, and count the frequency of occurrence of different grayscale values to obtain grayscale statistics. and ,calculate and Similarity between them; S020. If the similarity is greater than the threshold, continue traversing the next subgraph, based on the subgraph. Hezi Diagram Execute S010; otherwise, execute S030. S030, Detection Subgraph Hezi Diagram Feature points and corresponding statistical counts and ,if and All are greater than the threshold Then determine the subgraph The sub-image containing the product header or footer should be deleted. If all subimages before or after it have a black background, then iterate through the next subimage, based on the subimage. Hezi Diagram Execute S010.
4. The open defect detection method according to claim 1, characterized in that, In step S1, the method for aligning the product header position when the template image and the actual image are aligned by column is as follows: S110. Using feature detection and feature matching algorithms, feature point matching is performed between the front part of the template image containing the product head and the front part of the actual image containing the product head. Feature points in the template image of each matched feature point pair are added to a feature point set. In the middle, feature points located in the actual image are placed into the feature point set. middle; S120, take The feature point with the smallest x-axis coordinate and Zhongyu Matching feature points ,like exist If the x-axis coordinate of the feature point pair is the smallest, then the difference between the two x-axis coordinates of this feature point pair is used as the product head offset between the template image and the actual image; otherwise, the offset is calculated separately. and from and Delete it, and repeat the step; where the x-axis is the scanning direction of the line scan imaging process; S130. Delete the front black background portion of the template image or the actual image with a length equal to the product head offset, so as to achieve product head position alignment when the template image and the actual image are aligned.
5. The open defect detection method according to claim 1, characterized in that, The implementation of S2 is as follows: S210. Divide the template image and the actual image obtained in S1 into local images according to the same local step size d and scanning step size s; S220. Calculate the partial image composed of columns i to i+d in the template image. And a partial image consisting of columns i to i+d in the actual image. Local similarity between them; If the local similarity is greater than the threshold, it means that there is no distortion in the local positions between the i-th column and the (i+d)-th column in the actual image. Repeat the calculation of local similarity; If the local similarity is not greater than a threshold, it indicates that there is distortion at a local location between columns i and (i+d) in the actual image. The distortion at this local location is then corrected, and then... The calculation of local similarity is repeated; this step S220 ends when the template image or the actual image is scanned.
6. The open defect detection method according to claim 5, characterized in that, The metric for measuring local similarity is: ; In the formula, and The OTSU binarization algorithm was used to perform the following operations: and The resulting binarized local image is processed.
7. The open defect detection method according to claim 1, characterized in that, The implementation of S3 is as follows: The template image obtained in S1 and the actual image after distortion correction in S2 are divided according to the product area to be inspected; Feature detection and feature matching algorithms are used to match feature points between each part of the template image to be detected and the corresponding part of the actual image; based on the coordinate correspondence between each matched feature point pair, a homography matrix is fitted. Multiplying the homography matrix by the corresponding image portion of the component to be detected in the template image yields a new image portion of the component to be detected in the actual image, thus achieving pixel-level alignment between the actual image and the template image.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein, when the computer program is executed by a processor, it controls the device where the storage medium is located to perform an open defect detection method based on template matching as described in any one of claims 1 to 7.