Defect detection method, device, electronic device, storage medium and program product

CN116228746BActive Publication Date: 2024-08-23MOORE THREADS TECH CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310449479.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-29
Publication Date
2024-08-23
Estimated Expiration
2042-12-29

Smart Images

  • Figure CN116228746B
    Figure CN116228746B
Patent Text Reader

Abstract

The present disclosure relates to the field of image data processing technology, and in particular to a defect detection method, device, electronic device, storage medium and program product. The method comprises: obtaining an image to be detected and a template image corresponding to the image to be detected; determining a candidate frame of defects in the image to be detected according to the image to be detected and the template image; and determining a defect detection result corresponding to the candidate frame at least according to difference information of an image block corresponding to the candidate frame before and after morphological transformation. The present disclosure uses the difference information of an image block corresponding to a candidate frame before and after morphological transformation to perform defect detection on the candidate frame, which can improve the accuracy of defect detection.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] This application is a divisional application of the Chinese patent application submitted to the China Patent Office on December 29, 2022, with application number 202211701346.5 and invention name "Defect detection method, device, electronic device, storage medium and program product". Technical Field

[0002] The present disclosure relates to the technical field of image data processing, and in particular to a defect detection method, device, electronic device, storage medium and program product. Background Art

[0003] Intelligent industrial quality inspection is an important issue in the field of computer vision and industrial quality inspection. How to improve the accuracy of product defect detection is a technical problem that needs to be solved urgently. Summary of the invention

[0004] The present disclosure provides a defect detection technology solution.

[0005] According to one aspect of the present disclosure, there is provided a defect detection method, comprising:

[0006] Acquire an image to be detected and a template image corresponding to the image to be detected;

[0007] Determining a candidate frame of a defect in the image to be detected according to the image to be detected and the template image;

[0008] The defect detection result corresponding to the candidate box is determined at least according to difference information of the image block corresponding to the candidate box before and after the morphological transformation.

[0009] According to one aspect of the present disclosure, a method for training a machine learning model for defect detection is provided, comprising:

[0010] Acquire a training image and a template image corresponding to the training image;

[0011] Determining a candidate frame of a defect in the training image according to the training image and the template image;

[0012] At least input difference information of the image block corresponding to the candidate box before and after morphological transformation into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model;

[0013] The machine learning model is trained according to the annotation information corresponding to the candidate box and the defect prediction result.

[0014] According to one aspect of the present disclosure, there is provided a defect detection device, comprising:

[0015] A first acquisition module, used to acquire an image to be detected and a template image corresponding to the image to be detected;

[0016] A first determination module, configured to determine a candidate frame of a defect in the image to be detected based on the image to be detected and the template image;

[0017] The second determination module is used to determine the defect detection result corresponding to the candidate box at least according to the difference information of the image block corresponding to the candidate box before and after the morphological transformation.

[0018] According to one aspect of the present disclosure, there is provided a training device for a machine learning model for defect detection, comprising:

[0019] A second acquisition module, used to acquire a training image and a template image corresponding to the training image;

[0020] A third determination module, configured to determine a candidate frame of a defect in the training image according to the training image and the template image;

[0021] A prediction module, used to input at least difference information of the image block corresponding to the candidate box before and after the morphological transformation into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model;

[0022] A training module is used to train the machine learning model according to the annotation information corresponding to the candidate box and the defect prediction result.

[0023] According to one aspect of the present disclosure, an electronic device is provided, comprising: one or more processors; a memory for storing executable instructions; wherein the one or more processors are configured to call the executable instructions stored in the memory to execute the above method.

[0024] According to one aspect of the present disclosure, a computer-readable storage medium is provided, on which computer program instructions are stored, and the computer program instructions implement the above method when executed by a processor.

[0025] According to one aspect of the present disclosure, a computer program product is provided, including a computer-readable code, or a non-volatile computer-readable storage medium carrying the computer-readable code. When the computer-readable code runs in an electronic device, a processor in the electronic device executes the above method.

[0026] In the embodiments of the present disclosure, an image to be detected and a template image corresponding to the image to be detected are obtained, and a candidate frame of defects in the image to be detected is determined based on the image to be detected and the template image, and a defect detection result corresponding to the candidate frame is determined based on at least difference information of an image block corresponding to the candidate frame before and after morphological transformation. Thus, defect detection of the candidate frame is performed using the difference information of the image block corresponding to the candidate frame before and after morphological transformation, thereby improving the accuracy of defect detection.

[0027] It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.

[0028] Further features and aspects of the present disclosure will become apparent from the following detailed description of exemplary embodiments with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] The drawings herein are incorporated into the specification and constitute a part of the specification. These drawings illustrate embodiments consistent with the present disclosure and are used to illustrate the technical solutions of the present disclosure together with the specification.

[0030] Figure 1 A flow chart of a defect detection method provided by an embodiment of the present disclosure is shown.

[0031] Figure 2 A flowchart of a method for training a machine learning model for defect detection provided by an embodiment of the present disclosure is shown.

[0032] Figure 3 A schematic diagram showing a template image in a training method for a machine learning model for defect detection provided in an embodiment of the present disclosure.

[0033] Figure 4 A schematic diagram showing training images and their annotated data in a training method for a machine learning model for defect detection provided in an embodiment of the present disclosure.

[0034] Figure 5 A block diagram of a defect detection device provided by an embodiment of the present disclosure is shown.

[0035] Figure 6 A block diagram of a training device for a machine learning model for defect detection provided by an embodiment of the present disclosure is shown.

[0036] Figure 7 A block diagram of an electronic device 1900 provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0037] Various exemplary embodiments, features and aspects of the present disclosure will be described in detail below with reference to the accompanying drawings. The same reference numerals in the accompanying drawings represent elements with the same or similar functions. Although various aspects of the embodiments are shown in the accompanying drawings, the drawings are not necessarily drawn to scale unless otherwise specified.

[0038] The word “exemplary” is used exclusively herein to mean “serving as an example, example, or illustration.” Any embodiment described herein as “exemplary” is not necessarily to be construed as preferred or advantageous over other embodiments.

[0039] The term "and / or" herein is only a description of the association relationship of the associated objects, indicating that there may be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the term "at least one" herein represents any combination of at least two of any one or more of a plurality of. For example, including at least one of A, B, and C can represent including any one or more elements selected from the set consisting of A, B, and C.

[0040] In addition, in order to better illustrate the present disclosure, numerous specific details are given in the following specific embodiments. It should be understood by those skilled in the art that the present disclosure can also be implemented without certain specific details. In some examples, methods, means, components and circuits well known to those skilled in the art are not described in detail in order to highlight the subject matter of the present disclosure.

[0041] The embodiments of the present disclosure provide a defect detection method, apparatus, electronic device, storage medium and program product, which obtain an image to be detected and a template image corresponding to the image to be detected, determine a candidate frame of defects in the image to be detected based on the image to be detected and the template image, and determine a defect detection result corresponding to the candidate frame based on at least difference information of an image block corresponding to the candidate frame before and after morphological transformation. Defect detection of the candidate frame can be performed using the difference information of the image block corresponding to the candidate frame before and after morphological transformation, thereby improving the accuracy of defect detection.

[0042] The defect detection method provided by the embodiment of the present disclosure is described in detail below with reference to the accompanying drawings.

[0043] Figure 1A flow chart of a defect detection method provided by an embodiment of the present disclosure is shown. In one possible implementation, the execution subject of the defect detection method may be a defect detection device. For example, the defect detection method may be executed by a terminal device or a server or other electronic device. The terminal device may be a user equipment (UE), a mobile device, a user terminal, a terminal, a cellular phone, a cordless phone, a personal digital assistant (PDA), a handheld device, a computing device, a vehicle-mounted device or a wearable device. In some possible implementations, the defect detection method may be implemented by a processor calling computer-readable instructions stored in a memory. For example Figure 1 As shown, the defect detection method includes steps S11 to S13.

[0044] In step S11, an image to be detected and a template image corresponding to the image to be detected are obtained.

[0045] In step S12, a candidate frame of a defect in the image to be detected is determined according to the image to be detected and the template image.

[0046] In step S13, the defect detection result corresponding to the candidate box is determined based on at least the difference information of the image block corresponding to the candidate box before and after the morphological transformation.

[0047] In the embodiments of the present disclosure, the image to be detected may represent an image corresponding to a target object to be subjected to defect detection. For example, an image of the target object to be subjected to defect detection may be acquired by performing image acquisition to obtain the image to be detected. The target object may be any object to be subjected to defect detection.

[0048] In a possible implementation, the image to be detected is an image to be detected corresponding to a printed circuit board. In this implementation, the printed circuit board can be a hard board PCB (Printed Circuit Board), or a FPC (Flexible Printed Circuit), etc., which is not limited here.

[0049] In this implementation, by acquiring an image to be detected corresponding to a printed circuit board and a template image corresponding to the image to be detected, a candidate frame of defects in the image to be detected is determined according to the image to be detected and the template image, and a defect detection result corresponding to the candidate frame is determined at least according to difference information of an image block corresponding to the candidate frame before and after morphological transformation. Thus, by using the difference information of an image block corresponding to the candidate frame before and after morphological transformation to perform defect detection on the candidate frame, the accuracy of defect detection on the printed circuit board can be improved.

[0050] In the embodiment of the present disclosure, the template image corresponding to the image to be detected can represent a defect-free image corresponding to the image to be detected. By comparing the image to be detected and the template image, a candidate frame of defects in the image to be detected can be determined. The candidate frame can represent an area in the image to be detected that may be a defect.

[0051] In a possible implementation, determining a candidate frame of a defect in the image to be detected based on the image to be detected and the template image includes: obtaining a difference image between the image to be detected and the template image; determining a contour in the difference image; and determining a candidate frame of the defect in the image to be detected based on the contour.

[0052] In this implementation, the pixel values ​​of the same pixel positions of the image to be detected and the template image can be compared to determine the difference image between the image to be detected and the template image. In one example, the image to be detected can be represented by img_test, the template image can be represented by img_temp, and the difference image can be represented by img_diff.

[0053] In this implementation, a contour search method such as findContours can be used to search for contours in the difference image to obtain contours in the difference image. After determining the contours in the difference image, a candidate frame of a defect in the image to be detected can be determined based on the contours in the difference image.

[0054] In this implementation, a difference image between the image to be detected and the template image is obtained, the contour in the difference image is determined, and based on the contour, the candidate frame of the defect in the image to be detected is determined, thereby being able to accurately determine the candidate frame of the defect in the image to be detected by traditional image processing methods.

[0055] As an example of this implementation, the step of obtaining the difference image between the image to be detected and the template image includes: performing blur operations on the image to be detected and the template image respectively to obtain a first blurred image corresponding to the image to be detected and a second blurred image corresponding to the template image; and determining the difference image between the image to be detected and the template image according to the first blurred image and the second blurred image. The first blurred image represents the blurred image corresponding to the image to be detected, and the second blurred image represents the blurred image corresponding to the template image.

[0056] In one example, a Gaussian blur operation can be performed on the image to be detected and the template image respectively to obtain a first blurred image corresponding to the image to be detected and a second blurred image corresponding to the template image. The Gaussian kernel of the Gaussian blur operation can be 5×5, 3×3 or 7×7, etc., which is not limited here. In one example, the first blurred image can be represented by img_test_gaussian, and the second blurred image can be represented by img_temp_gaussian.

[0057] In this example, by performing blurring operations on the image to be detected and the template image respectively, a first blurred image corresponding to the image to be detected and a second blurred image corresponding to the template image are obtained, and based on the first blurred image and the second blurred image, a difference image between the image to be detected and the template image is determined, thereby obtaining a smoother difference image.

[0058] In one example, the determining of the difference image between the image to be detected and the template image based on the first blurred image and the second blurred image includes: performing binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; and determining the difference image between the image to be detected and the template image based on the first binarized image and the second binarized image. The first binarized image represents the binarized image corresponding to the first blurred image, and the second binarized image represents the binarized image corresponding to the second blurred image. In one example, the first binarized image can be represented by img_test_bina, and the second binarized image can be represented by img_temp_bina.

[0059] In this example, the OTSU method or other methods can be used to perform binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image.

[0060] In one example, determining the difference image between the image to be detected and the template image based on the first binarized image and the second binarized image includes: for any pixel position, if the pixel value of the pixel position in the first binarized image and the second binarized image is different, then in the difference image between the image to be detected and the template image, the pixel value of the pixel position is 0; for any pixel position, if the pixel value of the pixel position in the first binarized image and the second binarized image is the same, then in the difference image, the pixel value of the pixel position is 255.

[0061] For example, for any pixel position, if the pixel values ​​of the pixel position in the first binarized image and the second binarized image are different (i.e., one is 0 and the other is 255), then in the difference image, the pixel value of the pixel position is 0 (i.e., black); for any pixel position, if the pixel values ​​of the pixel position in the first binarized image and the second binarized image are the same (i.e., both are 0 or both are 255), then in the difference image, the pixel value of the pixel position is 255 (i.e., white).

[0062] In one example, the following steps may be used to obtain a difference image img_diff: let img_diff = img_test_bina - img_temp_bina; let the pixel values ​​that are not 0 in img_diff become 255; let the value of each pixel position in img_diff become 255 minus the corresponding pixel value.

[0063] In this example, by performing binarization operations on the first blurred image and the second blurred image respectively, a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image are obtained, and based on the first binarized image and the second binarized image, a difference image between the image to be detected and the template image is determined, thereby reducing the interference of the intensity of ambient light for collecting the image to be detected on defect detection.

[0064] As another example of this implementation, obtaining the difference image between the image to be detected and the template image includes: performing a binarization operation on the image to be detected and the template image respectively to obtain a first binarized image corresponding to the image to be detected and a second binarized image corresponding to the template image; and determining the difference image between the image to be detected and the template image based on the first binarized image and the second binarized image.

[0065] As an example of this implementation, determining the contour in the difference image includes: performing a morphological operation on the difference image to obtain a de-interferenced image corresponding to the difference image; and searching for the contour in the de-interferenced image as the contour in the difference image. The de-interferenced image may represent an image corresponding to the difference image after interference is removed.

[0066] In this example, a dilation operation and / or an erode operation may be performed on the difference image to obtain a de-interferenced image corresponding to the difference image. In one example, a dilation operation with a kernel size of 3×3, an erode operation with a kernel size of 3×3, an erode operation with a kernel size of 3×3, an erode operation with a kernel size of 7×3, a dilation operation with a kernel size of 7×3, an erode operation with a kernel size of 3×7, and a dilation operation with a kernel size of 3×7 may be performed on the difference image in sequence to obtain a de-interferenced image corresponding to the difference image.

[0067] In this example, by performing morphological operations on the difference image, a detached image corresponding to the difference image is obtained, and contours in the detached image are found as contours in the difference image. Thus, by performing morphological operations on the difference image, horizontal and vertical interference lines in the difference image can be removed, thereby improving the accuracy of the found contours.

[0068] As an example of this implementation method, determining a candidate frame of the defect in the image to be detected based on the contour includes: filtering the contours in the difference image that meet preset conditions; and determining the candidate frame of the defect in the image to be detected based on the contours remaining after filtering in the difference image.

[0069] In this example, if any contour in the difference image satisfies a preset condition, it can be determined that the contour belongs to a preset candidate frame that does not belong to a defect, so that the contour can be filtered. The preset condition can be set based on experience and is not limited here.

[0070] In one example, the preset conditions include at least one of the following: the area of ​​the area enclosed by the outline is smaller than a first preset area; the area of ​​the area enclosed by the outline is larger than a second preset area, wherein the second preset area is larger than the first preset area; the aspect ratio of the enclosing rectangle of the outline is smaller than a first preset threshold; the aspect ratio of the enclosing rectangle of the outline is larger than a second preset threshold, wherein the second preset threshold is larger than the first preset threshold; the average pixel value within the enclosing rectangle of the outline is greater than the preset pixel value.

[0071] Among them, the enclosing rectangle of any contour can be the circumscribed rectangle of the contour, and the sides of the enclosing rectangle of the contour are parallel to the preset coordinate axis (e.g., the xy axis). In some application scenarios, the enclosing rectangle can also be called a bounding box. For example, the enclosing rectangle of the contour can be represented by (x, y, w, h), where (x, y) is the coordinate of the upper left corner of the enclosing rectangle, w is the width of the enclosing rectangle, and h is the height of the enclosing rectangle.

[0072] For example, the first preset area AREA_TH_LOW = 20, and the second preset area AREA_TH_HIGH = 20000. If the area of ​​a region enclosed by any contour is less than 20 or greater than 20000, the contour may be filtered.

[0073] For another example, the first preset threshold is 0.1, and the second preset threshold is 10. If the aspect ratio of the enclosing rectangle of any contour is less than 0.1 or greater than 10, the contour may be filtered.

[0074] For another example, the preset pixel value PIXEL_MEAN_TH=240. If the average pixel value in the enclosing rectangle of any contour is greater than 240, the contour may be filtered.

[0075] In one example, determining a candidate frame of a defect in the image to be detected based on the contours remaining after filtering in the difference image includes: for any contour remaining after filtering in the difference image, determining an enlarged rectangle corresponding to the enclosing rectangle of the contour as the candidate frame of the defect in the image to be detected, wherein the enlarged rectangle coincides with the geometric center of the enclosing rectangle, the width of the enlarged rectangle is a first preset multiple of the width of the enclosing rectangle, and the height of the enlarged rectangle is a second preset multiple of the height of the enclosing rectangle, wherein both the first preset multiple and the second preset multiple are greater than 1.

[0076] The first preset multiple and the second preset multiple can be the same or different. For example, the first preset multiple and the second preset multiple are both 2, the enclosing rectangle of the outline is (x, y, w, h), and the enlarged rectangle corresponding to the enclosing rectangle of the outline is (xw / 2, yh / 2, 2×w, 2×h).

[0077] In the above example, by filtering the contours that meet the preset conditions in the difference image and determining the candidate boxes of the defects in the image to be detected based on the contours remaining after filtering in the difference image, the interference of abnormal contours in the difference image on defect detection can be reduced.

[0078] As another example of this implementation, candidate frames of defects may be determined according to contours in the difference image, that is, in this example, the found contours may not be filtered.

[0079] In another possible implementation, determining a candidate box of a defect in the image to be detected based on the image to be detected and the template image includes: inputting the image to be detected and the template image into a pre-trained second neural network, and determining the candidate box of the defect in the image to be detected via the second neural network. The second neural network is used to determine the candidate box of the defect in the image to be detected based on the image to be detected and the template image.

[0080] In the disclosed embodiment, the image block corresponding to the candidate box may include at least one of the following: a first image block of the candidate box on the template image, a second image block of the candidate box on the difference image, and a third image block of the candidate box on the image to be detected. Among them, the first image block, the second image block, and the third image block can be determined from the template image, the difference image, and the image to be detected, respectively, according to the position of the candidate box on the image to be detected. That is, the first image block can represent the image block of the candidate box on the template image, the second image block can represent the image block of the candidate box on the difference image, and the third image block can represent the image block of the candidate box on the image to be detected.

[0081] Accordingly, the defect detection result corresponding to the candidate box can be determined based on at least one of the difference information of the first image block before and after the morphological transformation, the difference information of the second image block before and after the morphological transformation, and the difference information of the third image block before and after the morphological transformation.

[0082] In a possible implementation, the image block corresponding to the candidate box includes: a first image block of the candidate box on the template image, and a second image block of the candidate box on the difference image, wherein the difference image represents a difference image between the image to be detected and the template image; the step of determining the defect detection result corresponding to the candidate box at least based on the difference information of the image block corresponding to the candidate box before and after the morphological transformation includes: determining the defect detection result corresponding to the candidate box at least based on the difference information of the first image block before and after the morphological transformation, and the difference information of the second image block before and after the morphological transformation. In one example, the first image block can be represented by temp_img, and the second image block can be represented by test_img.

[0083] In this example, by performing defect detection on the candidate frame at least based on difference information of the first image block before and after morphological transformation and difference information of the second image block before and after morphological transformation, the accuracy of the defect detection result corresponding to the candidate frame can be improved.

[0084] As an example of this implementation, the defect detection result corresponding to the candidate box is determined at least based on the difference information of the first image block before and after the morphological transformation, and the difference information of the second image block before and after the morphological transformation, including: obtaining a first binarized image block corresponding to the first image block, and a second binarized image block corresponding to the second image block; performing a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block; performing a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block; and determining the defect detection result corresponding to the candidate box based on a first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block, and a second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block.

[0085] The first binary image block represents the binary image block corresponding to the first image block, and the second binary image block represents the binary image block corresponding to the second image block. In one example, the first binary image block can be represented by temp_img_bina, and the second binary image block can be represented by test_img_bina.

[0086] In this example, a dilation operation and / or an erosion operation may be performed on the first binary image block to obtain a first morphologically transformed image block corresponding to the first binary image block; and a dilation operation and / or an erosion operation may be performed on the second binary image block to obtain a second morphologically transformed image block corresponding to the second binary image block. The first morphologically transformed image block may represent an image block obtained by performing a morphological transformation on the first binary image block, and the second morphologically transformed image block may represent an image block obtained by performing a morphological transformation on the second binary image block.

[0087] By comparing the first binarized image block with the first morphologically transformed image block pixel by pixel, a first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block can be determined, wherein the first number of pixels represents the number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block. By comparing the second binarized image block with the second morphologically transformed image block pixel by pixel, a second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block can be determined, wherein the second number of pixels represents the number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block.

[0088] In this example, the defect detection result corresponding to the candidate box is determined based on the first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block, and the second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block, thereby improving the accuracy of the defect detection result corresponding to the candidate box.

[0089] In one example, obtaining a first binary image block corresponding to the first image block and a second binary image block corresponding to the second image block includes: performing blurring operations on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; performing binarization operations on the first blurred image block and the second blurred image block, respectively, to obtain a first binary image block corresponding to the first image block and a second binary image block corresponding to the second image block.

[0090] In this example, the first image block and the second image block can be converted into grayscale images respectively to obtain a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block. The first grayscale image block can represent the grayscale image corresponding to the first image block, and the second grayscale image block can represent the grayscale image corresponding to the second image block. In one example, the first grayscale image block can be represented by temp_img_gray, and the second grayscale image block can be represented by test_img_gray.

[0091] In this example, the first grayscale image block and the second grayscale image block can be blurred respectively to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block. The first blurred image block can represent an image obtained after the blur operation is performed on the first image block, and the second blurred image block can represent an image obtained after the blur operation is performed on the second image block. In an example, the first blurred image block can be represented by temp_img_blur, and the second blurred image block can be represented by test_img_blur. In an example, the first grayscale image block and the second grayscale image block can be Gaussian blurred respectively to obtain a first blurred image block and a second blurred image block. The Gaussian kernel of the Gaussian blur operation can be 5×5, 3×3 or 7×7, etc., which is not limited here.

[0092] In an example, the OTSU method or other methods may be used to perform binarization operations on the first blurred image block and the second blurred image block respectively to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0093] In this example, by performing blur processing before binarization, a smoother processing result can be obtained.

[0094] In one example, the performing a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block includes: performing a morphological operation on the first binarized image block based on kernels of at least two sizes to obtain at least two first morphologically transformed image blocks corresponding to the first binarized image block; the performing a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block includes: performing a morphological operation on the second binarized image block based on kernels of at least two sizes to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block; the performing a morphological operation on the second binarized image block based on kernels of at least two sizes to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block; The method comprises: for the at least two first morphologically transformed image blocks, respectively determining the number of pixels with different pixel values ​​between the first binarized image block and the first morphologically transformed image block, to obtain at least two first pixel numbers; for the at least two second morphologically transformed image blocks, respectively determining the number of pixels with different pixel values ​​between the second binarized image block and the second morphologically transformed image block, to obtain at least two second pixel numbers; and determining the defect detection result corresponding to the candidate box according to the at least two first pixel numbers and the at least two second pixel numbers.

[0095] For example, the size of the kernel may include at least two of 3, 5, 7, 9, 11, etc., which are not limited here. In this example, based on any size, the first binary image block and the second binary image block may be respectively subjected to dilation and / or erosion operations to obtain the corresponding first morphologically transformed image block and the second morphologically transformed image block.

[0096] In this example, a morphological operation is performed on the first binarized image block based on kernels of at least two sizes to obtain at least two first morphologically transformed image blocks corresponding to the first binarized image block; a morphological operation is performed on the second binarized image block based on kernels of at least two sizes to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block; for the at least two first morphologically transformed image blocks, the number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block is determined respectively to obtain at least two first pixel numbers; for the at least two second morphologically transformed image blocks, the number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block is determined respectively to obtain at least two second pixel numbers; the morphological difference information thus determined can more accurately reflect the defect features in the candidate frame; and the defect detection result corresponding to the candidate frame is determined according to the at least two first pixel numbers and the at least two second pixel numbers, so that a more accurate defect detection result can be determined for the candidate frame.

[0097] As an example of this implementation, the first morphologically transformed image block includes a first dilated image block and a first eroded image block, and the second morphologically transformed image block includes a second dilated image block and a second eroded image block; the performing of a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block includes: performing a dilation operation on the first binarized image block to obtain a first dilated image block corresponding to the first binarized image block; performing an erosion operation on the first binarized image block to obtain a first eroded image block corresponding to the first binarized image block; the performing of a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block includes: performing a dilation operation on the second binarized image block to obtain a second dilated image block corresponding to the second binarized image block; performing an erosion operation on the second binarized image block to obtain a second eroded image block corresponding to the second binarized image block.

[0098] In this example, by performing dilation and erosion operations on the first binary image block and the second binary image block respectively, and determining difference information based on the corresponding dilated image block and eroded image block respectively, the accuracy of defect detection for the candidate frame can be further improved.

[0099] In one example, the kernel size list kernel_size_list=[3, 5, 7, 9, 11] may be set.

[0100] Each value in the kernel size list can be used as the kernel size of the dilation operation to obtain the dilation difference feature v_ki_d under each kernel size. For example, when the kernel size is 3, v_ki_d=v_k3_d, when the kernel size is 5, v_ki_d=v_k5_d, and so on. The first binary image block can be dilated with a kernel size of i to obtain a first dilated image block; the second binary image block can be dilated with a kernel size of i to obtain a second dilated image block; the dilation difference feature v_ki_d can be determined based on the first number of pixels n1 with different pixel values ​​between the first binary image block and the first dilated image block, and the second number of pixels n2 with different pixel values ​​between the second binary image block and the second dilated image block. For example, if n2 is equal to 0, v_ki_d=[n1,n2,1]; if n2 is not equal to 0, v_ki_d=[n1,n2,n1 / n2].

[0101] Each value in the kernel size list can be used as the kernel size of the erosion operation to obtain the erosion difference feature v_ki_e under each kernel size. For example, when the kernel size is 3, v_ki_e=v_k3_e, when the kernel size is 5, v_ki_e=v_k5_e, and so on. The first binary image block can be eroded with a kernel size of i to obtain a first eroded image block; the second binary image block can be eroded with a kernel size of i to obtain a second eroded image block; the erosion difference feature v_ki_e can be determined according to the first number of pixels n1 with different pixel values ​​between the first binary image block and the first eroded image block, and the second number of pixels n2 with different pixel values ​​between the second binary image block and the second eroded image block. For example, if n2 is equal to 0, v_ki_e=[n1,n2,1]; if n2 is not equal to 0, v_ki_e=[n1,n2,n1 / n2].

[0102] After determining the dilation difference feature v_dilate = [v_k3_d, v_k5_d, v_k7_d, v_k9_d, v_k11_d] and the erosion difference feature v_erode = [v_k3_e, v_k5_e, v_k7_e, v_k9_e, v_k11_e], the defect detection result corresponding to the candidate box can be determined according to the morphological difference feature v5 = [v_dilate, v_erode].

[0103] In a possible implementation, the defect detection result corresponding to the candidate box is determined at least based on the difference information of the first image block before and after the morphological transformation, and the difference information of the second image block before and after the morphological transformation, including: performing grayscale statistics on the first image block and the second image block, respectively, to obtain a first grayscale statistical result corresponding to the first image block and a second grayscale statistical result corresponding to the second image block; and determining the defect detection result corresponding to the candidate box based at least on the difference information of the first image block before and after the morphological transformation, the difference information of the second image block before and after the morphological transformation, the first grayscale statistical result, and the second grayscale statistical result.

[0104] In this implementation, the first image block and the second image block can be converted into grayscale images respectively to obtain a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block. In an example, the first grayscale image block can be represented by temp_img_gray, and the second grayscale image block can be represented by test_img_gray.

[0105] In this implementation, the first grayscale statistical result may include the number of pixels of some or all grayscale values ​​in the first image block. For example, the grayscale histogram of the first grayscale image block may be counted to obtain the first grayscale statistical result. In one example, the grayscale histogram of the first grayscale image block may be represented by temp_hist.

[0106] The second grayscale statistics result may include the number of pixels of some or all grayscale values ​​in the second image block. For example, the grayscale histogram of the second grayscale image block may be counted to obtain the second grayscale statistics result. In one example, the grayscale histogram of the second grayscale image block may be represented by test_hist.

[0107] In this implementation, the defect detection result corresponding to the candidate box is determined by combining the first grayscale statistical result corresponding to the first image block and the second grayscale statistical result corresponding to the second image block, so that the accuracy of the defect detection result corresponding to the candidate box can be improved.

[0108] As an example of this implementation method, the first grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the first image block; the second grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the second image block.

[0109] In an example, the number of pixels with a grayscale value of 0 in the first image block can be represented by temp_hist[0], the number of pixels with a grayscale value of 255 in the first image block can be represented by temp_hist

[255] , the number of pixels with a grayscale value of 0 in the second image block can be represented by test_hist[0], and the number of pixels with a grayscale value of 255 in the second image block can be represented by test_hist

[255] . The defect detection result corresponding to the candidate box can be determined by combining the grayscale feature v1 = [temp_hist[0], temp_hist

[255] , test_hist[0], test_hist

[255] ].

[0110] In this example, the defect detection result corresponding to the candidate frame is determined by combining the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the first image block, as well as the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the second image block. Thus, the number of pixels with the most significant grayscale values ​​in the first image block and the second image block is used to assist in defect detection of the candidate frame, thereby improving the accuracy of the defect detection result corresponding to the candidate frame.

[0111] In a possible implementation, determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, and difference information of the second image block before and after the morphological transformation, includes: obtaining first contour information corresponding to the first image block, and second contour information corresponding to the second image block; determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, the first contour information, and the second contour information.

[0112] In this implementation, the first contour information may represent information about a contour in the first image block, and the second contour information may represent information about a contour in the second image block.

[0113] In this implementation, a contour search method such as findContours can be used to perform contour search on the first image block and the second image block respectively, to determine the contour in the first image block and the contour in the second image block, thereby obtaining first contour information corresponding to the first image block and second contour information corresponding to the second image block.

[0114] In an example, the first contour information may be represented by v_c_temp, and the second contour information may be represented by v_c_test. The defect detection result corresponding to the candidate box may be determined in combination with the contour feature v2=[v_c_test, v_c_temp].

[0115] In this implementation, the defect detection result corresponding to the candidate box is determined by combining the first contour information corresponding to the first image block and the second contour information corresponding to the second image block, thereby improving the accuracy of the defect detection result corresponding to the candidate box.

[0116] As an example of this implementation method, obtaining first contour information corresponding to the first image block and second contour information corresponding to the second image block includes: determining the first contour information corresponding to the first image block according to the contour in a first binarized image block corresponding to the first image block; and determining the second contour information corresponding to the second image block according to the contour in a second binarized image block corresponding to the second image block.

[0117] In one example, a blurring operation may be performed on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; a binarization operation may be performed on the first blurred image block and the second blurred image block, respectively, to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0118] In one example, the first grayscale image block and the second grayscale image block may be subjected to Gaussian blur operations respectively to obtain a first blurred image block and a second blurred image block, wherein the Gaussian kernel of the Gaussian blur operation may be 5×5, 3×3 or 7×7, etc., which is not limited here.

[0119] In an example, the OTSU method or other methods may be used to perform binarization operations on the first blurred image block and the second blurred image block respectively to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0120] In one example, a contour search method such as findContours may be used to search for contours on the first binary image block and the second binary image block to obtain contours in the first binary image block and contours in the second binary image block.

[0121] In an example, the first blurred image block can be represented by temp_img_blur, the second blurred image block can be represented by test_img_blur, the first binarized image block can be represented by temp_img_bina, the second binarized image block can be represented by test_img_bina, the contour in the first binarized image block can be represented by temp_img_contours, and the contour in the second binarized image block can be represented by test_img_contours.

[0122] In this example, first contour information corresponding to the first image block is determined based on the contour in the first binary image block corresponding to the first image block, and second contour information corresponding to the second image block is determined based on the contour in the second binary image block corresponding to the second image block, thereby enabling contour search to be performed more accurately.

[0123] In one example, the first contour information includes: geometric information of the largest N contours in the first binary image block, and the number of contours in the first binary image block, where N is an integer greater than or equal to 1; the second contour information includes: geometric information of the largest N contours in the second binary image block, and the number of contours in the second binary image block.

[0124] In this example, the largest N contours in the first binary image block can be determined by sorting according to the area size of the areas enclosed by the contours in the first binary image block. The largest N contours in the second binary image block can be determined by sorting according to the area size of the areas enclosed by the contours in the second binary image block.

[0125] For example, N is equal to 2. Of course, those skilled in the art can flexibly set the size of N according to the requirements of actual application scenarios, and no limitation is made here.

[0126] For example, the first contour information may be v_c_temp=[temp_count, v_c_temp_v1, v_c_temp_v2], where temp_count represents the number of contours in the first binary image block, v_c_temp_v1 represents the geometric information of the largest contour in the first binary image block, and v_c_temp_v2 represents the geometric information of the second largest contour in the first binary image block. The second contour information may be v_c_test=[test_count, v_c_test_v1, v_c_test_v2], where test_count represents the number of contours in the second binary image block, v_c_test_v1 represents the geometric information of the largest contour in the second binary image block, and v_c_test_v2 represents the geometric information of the second largest contour in the second binary image block.

[0127] In this example, by combining the geometric information of the largest N contours in the first binary image block, the number of contours in the first binary image block, the geometric information of the largest N contours in the second binary image block, and the number of contours in the second binary image block, more accurate defect detection can be achieved for the candidate box.

[0128] In one example, the geometric information of the contour includes at least one of the following: the area of ​​the contour, the enclosing rectangle of the contour, the central moment of the contour, the position of the geometric center of the contour, the perimeter of the contour, the non-convexity of the contour, the minimum circumscribed rectangle of the contour, the minimum circumscribed circle of the contour, the fitted ellipse of the contour, and the fitted rectangle of the contour. The fitted ellipse of the contour may represent an ellipse obtained by performing ellipse fitting on the contour. The fitted rectangle of the contour may represent a rectangle obtained by performing straight line fitting on the contour.

[0129] Taking the contour with the largest area in the second binary image block as an example, the area of ​​the contour can be represented by test_c_area1, the enclosing rectangle of the contour can be represented by (a1_x, a1_y, a1_w, a1_h), the central moment of the contour can be represented by M1, the position of the geometric center of the contour can be represented by (c1_x, c1_y), the perimeter of the contour can be represented by perimeter1, the non-convexity of the contour can be represented by is_convex1, the minimum circumscribed rectangle of the contour can be represented by (a1_xr, a1_yr, a1_wr, a1_hr), the minimum circumscribed circle of the contour can be represented by (cr1_x, cr1_y, cr1_r), the fitted ellipse of the contour can be (e11, e12, e13, e14, e15), and the fitted rectangle of the contour can be represented by (l11, l12, l13, l14).

[0130] In a possible implementation, determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, and difference information of the second image block before and after the morphological transformation, includes: obtaining the width and height of the first image block; determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, the width and the height.

[0131] In one example, the width of the first image block can be represented by wt, the height of the first image block can be represented by ht, and the aspect ratio of the first image block can be represented by wt / ht. The defect detection result corresponding to the candidate box can be determined in combination with the size feature v3=[wt,ht,wt / ht] of the first image block.

[0132] In this implementation, the defect detection result corresponding to the candidate frame is determined by combining the width and height of the first image block, thereby improving the accuracy of the defect detection result corresponding to the candidate frame.

[0133] In another possible implementation, the determining the defect detection result corresponding to the candidate box at least based on the difference information of the first image block before and after the morphological transformation, and the difference information of the second image block before and after the morphological transformation, includes: obtaining the width and height of the second image block; determining the defect detection result corresponding to the candidate box at least based on the difference information of the first image block before and after the morphological transformation, the difference information of the second image block before and after the morphological transformation, the width and the height.

[0134] In a possible implementation, determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, and difference information of the second image block before and after the morphological transformation, includes: obtaining gradient information of the first image block, and gradient information of the second image block; determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, gradient information of the first image block, and gradient information of the second image block.

[0135] In one example, the gradient information of the first image block may be a Histogram of Oriented Gradient (HOG) of the first image block, the gradient information of the second image block may be a Histogram of Oriented Gradient of the second image block, the gradient information of the first image block may be represented by temp_hog_vec, and the gradient information of the second image block may be represented by test_hog_vec. The gradient feature v4 = [temp_hog_vec, test_hog_vec] may be combined to determine the defect detection result corresponding to the candidate box.

[0136] In this implementation, the defect detection result corresponding to the candidate box is determined by combining the gradient information of the first image block and the gradient information of the second image block, thereby improving the accuracy of the defect detection result corresponding to the candidate box.

[0137] In a possible implementation, the determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, and difference information of the second image block before and after the morphological transformation, includes: obtaining a difference image block between a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block; obtaining feature information of the difference image block; and determining the defect detection result corresponding to the candidate box at least based on difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, and feature information of the difference image block.

[0138] In one example, a blurring operation may be performed on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; a binarization operation may be performed on the first blurred image block and the second blurred image block, respectively, to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0139] In one example, the first grayscale image block and the second grayscale image block may be subjected to Gaussian blur operations respectively to obtain a first blurred image block and a second blurred image block, wherein the Gaussian kernel of the Gaussian blur operation may be 5×5, 3×3 or 7×7, etc., which is not limited here.

[0140] In one example, the first binary image block can be represented by temp_img_bina, the second binary image block can be represented by test_img_bina, the difference image block can be represented by diff_img, and the difference image block can be determined according to diff_img=test_img_bina-temp_img_bina. The difference image block can also be called a difference matrix, etc., which is not limited here.

[0141] In this implementation, defect detection is performed on the candidate frame by combining the feature information of the difference image block between the first binarized image block corresponding to the first image block and the second binarized image block corresponding to the second image block, thereby making it possible to utilize the difference information between the first binarized image block and the second binarized image block to improve the accuracy of the defect detection result corresponding to the candidate frame.

[0142] As an example of this implementation, the feature information of the difference image block includes: the number of pixels whose pixel values ​​are not 0 in the difference image block.

[0143] In one example, the pixel values ​​that are not 0 in the difference image block can be changed to 1, and then the pixel values ​​in the difference image block are accumulated to determine the number of pixels whose pixel values ​​are not 0 in the difference image block. According to this example, the difference information between the first binary image block and the second binary image block can be determined quickly and effectively.

[0144] In an example, the characteristic information of the difference image block includes: the number of pixels whose pixel values ​​are not 0 in each row of pixels of the difference image block, and the number of pixels whose pixel values ​​are not 0 in each column of pixels of the difference image block.

[0145] In this example, for each row in the difference image block, the number of pixels whose pixel values ​​are not 0 can be determined respectively, and for each column in the difference image block, the number of pixels whose pixel values ​​are not 0 can be determined respectively. According to the number of pixels whose pixel values ​​are not 0 in each row of pixels in the difference image block and the number of pixels whose pixel values ​​are not 0 in each column of pixels in the difference image block, the difference statistical feature v6=[diff_project_y, diff_project_x] can be obtained, where diff_project_y and diff_project_x are both vectors, and the number of elements in diff_project_y is equal to the number of rows of the difference image block, and the number of elements in diff_project_x is equal to the number of columns of the difference image block.

[0146] According to this example, the accuracy of defect detection on the candidate frame can be further improved.

[0147] In another example, the number of pixels whose pixel values ​​are not 0 in the difference image block includes: the number of pixels whose pixel values ​​are not 0 in each row of pixels of the difference image block.

[0148] In another example, the number of pixels whose pixel values ​​are not 0 in the difference image block includes: the number of pixels whose pixel values ​​are not 0 in each column of pixels of the difference image block.

[0149] In a possible implementation, the determining the defect detection result corresponding to the candidate box at least based on the difference information of the first image block before and after the morphological transformation, and the difference information of the second image block before and after the morphological transformation, includes: performing feature extraction on the first image block through a pre-trained first neural network to obtain a first depth feature corresponding to the first image block; performing feature extraction on the second image block through the first neural network to obtain a second depth feature corresponding to the second image block; determining the defect detection result corresponding to the candidate box based on at least the difference information of the first image block before and after the morphological transformation, the difference information of the second image block before and after the morphological transformation, the first depth feature and the second depth feature.

[0150] In this implementation, the first neural network may be a deep learning model. For example, the first neural network may adopt a network structure such as LeNet5, AlexNet, etc. The first neural network may be pre-trained using a data set such as MINIST.

[0151] In one example, a first grayscale image block corresponding to the first image block can be input into a pre-trained first neural network, and feature extraction is performed on the first grayscale image block through the first neural network to obtain a first depth feature corresponding to the first image block; a second grayscale image block corresponding to the second image block can be input into the first neural network, and feature extraction is performed on the second grayscale image block through the first neural network to obtain a second depth feature corresponding to the second image block.

[0152] For example, the first grayscale image block temp_img_gray may be subjected to a preprocessing operation of the LeNet5 model to obtain a first preprocessing feature temp_img_pre corresponding to the first grayscale image block temp_img_gray; the second grayscale image block test_img_gray may be subjected to a preprocessing operation of the LeNet5 model to obtain a second preprocessing feature test_img_pre corresponding to the second grayscale image block test_img_gray. The first preprocessing feature temp_img_pre may be inferred by the LeNet5 model, and the 84-dimensional feature vector output by the penultimate layer of the LeNet5 model may be used as the first depth feature, or the 10-dimensional feature vector output by the last layer of the LeNet5 model may be used as the first depth feature. The second preprocessing feature test_img_pre may be inferred by the LeNet5 model, and the 84-dimensional feature vector output by the penultimate layer of the LeNet5 model may be used as the second depth feature, or the 10-dimensional feature vector output by the last layer of the LeNet5 model may be used as the second depth feature.

[0153] In another example, the first image block can be input into a pre-trained first neural network, and feature extraction is performed on the first image block through the first neural network to obtain a first depth feature corresponding to the first image block; the second image block can be input into the first neural network, and feature extraction is performed on the second image block through the first neural network to obtain a second depth feature corresponding to the second image block.

[0154] In an example, the first depth feature may be represented by v_temp_deep, the second depth feature may be represented by v_test_deep, and the defect detection result corresponding to the candidate box may be determined in combination with the depth feature v_deep=[v_test_deep, v_temp_deep].

[0155] In this implementation, features of the first image block and the second image block are extracted through a first neural network to obtain a first depth feature corresponding to the first image block and a second depth feature corresponding to the second image block, thereby effectively supplementing features not considered by human prior knowledge, thereby further improving the accuracy of defect detection.

[0156] In a possible implementation, the defect detection result corresponding to the candidate box may be information that can represent the defect type of the candidate box. For example, the defect type may include open circuit, short circuit, copper, pin-hole, mouse bite, spur, and non-defect. Of course, the number of defect types may be more or less, and is not limited here. By converting the defect detection problem into a defect classification problem, the overall logic is simpler.

[0157] In another possible implementation, the defect detection result corresponding to the candidate box may be information that can indicate whether the candidate box is a defect.

[0158] In a possible implementation, the step of determining the defect detection result corresponding to the candidate frame based on at least the difference information of the image block corresponding to the candidate frame before and after the morphological transformation includes: at least inputting the difference information of the image block corresponding to the candidate frame before and after the morphological transformation into a pre-trained machine learning model, and obtaining the defect detection result corresponding to the candidate frame via the machine learning model. In this implementation, by performing defect detection on the candidate frame based on the difference information of the image block corresponding to the candidate frame before and after the morphological transformation using a pre-trained machine learning model, the accuracy and speed of defect detection on the candidate frame can be improved.

[0159] In a possible implementation, after determining the defect detection result corresponding to the candidate frame, the method further includes: in response to the defect detection result indicating that the candidate frame is defective, outputting the defect type of the candidate frame and the position information of the candidate frame.

[0160] In another possible implementation, after determining the defect detection result corresponding to the candidate box, the method further includes: in response to the defect detection result indicating that the candidate box is a defect, outputting position information of the candidate box.

[0161] The defect detection method provided by the embodiment of the present disclosure is described below through a specific application scenario. In this application scenario, an image to be detected corresponding to the PCB and a template image corresponding to the image to be detected are obtained.

[0162] In this application scenario, a candidate frame of a defect in the image to be detected can be determined according to the image to be detected and the template image.

[0163] Traditional image processing methods can be used to extract manual features v_traditional = [v1, v2, v3, v4, v5, v6] from the candidate boxes.

[0164] Among them, v1 is the grayscale feature, v1 = [temp_hist[0], temp_hist

[255] , test_hist[0], test_hist

[255] ];

[0165] v2 is the contour feature, v2 = [v_c_test, v_c_temp];

[0166] v3 is the size characteristic, v3 = [wt, ht, wt / ht];

[0167] v4 is the gradient feature, v4 = [temp_hog_vec, test_hog_vec];

[0168] v5 is the morphological difference feature, v5 = [v_dilate, v_erode];

[0169] v6 is the difference statistical feature, v6 = [diff_project_y, diff_project_x].

[0170] A pre-trained first neural network may be used to extract a deep feature v_deep=[v_test_deep, v_temp_deep] for the candidate box.

[0171] The manual features v_traditional and the deep features v_deep can be input into the pre-trained machine learning model to obtain the defect detection results corresponding to the candidate boxes.

[0172] Figure 2 A flow chart of a method for training a machine learning model for defect detection provided by an embodiment of the present disclosure is shown. In one possible implementation, the execution subject of the method for training a machine learning model for defect detection may be a training device for a machine learning model for defect detection. For example, the method for training a machine learning model for defect detection may be executed by a terminal device or a server or other electronic device. The terminal device may be a user device, a mobile device, a user terminal, a terminal, a cellular phone, a cordless phone, a personal digital assistant, a handheld device, a computing device, a vehicle-mounted device, or a wearable device. In some possible implementations, the method for training a machine learning model for defect detection may be implemented by a processor calling computer-readable instructions stored in a memory. As Figure 2 As shown, the defect detection method includes steps S21 to S24.

[0173] In step S21, a training image and a template image corresponding to the training image are obtained.

[0174] In step S22, a candidate frame of a defect in the training image is determined according to the training image and the template image.

[0175] In step S23, at least the difference information of the image block corresponding to the candidate box before and after the morphological transformation is input into the machine learning model, and the defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0176] In step S24, the machine learning model is trained according to the annotation information corresponding to the candidate box and the defect prediction result.

[0177] In the disclosed embodiments, training images, annotation data of training images, and template images corresponding to the training images can be obtained from datasets such as deepPCB. Figure 3 A schematic diagram showing a template image in a training method for a machine learning model for defect detection provided in an embodiment of the present disclosure. Figure 4 A schematic diagram showing a training image and its annotated data in a training method for a machine learning model for defect detection provided by an embodiment of the present disclosure. Figure 4 In the example shown, the training image includes multiple defect frames, and the defect types of the multiple defect frames are open circuit, short circuit, false copper, missing hole, mouse bite or stray. Of course, the machine learning model can be used to detect more or fewer types of defects, which is not limited here.

[0178] In a possible implementation, the data set may be divided into a training set and a test set according to a preset ratio, for example, the preset ratio may be 8:2.

[0179] As an example of this implementation, before dividing the data set into a training set and a test set according to a preset ratio, the method further includes: randomly shuffling the data set.

[0180] In the disclosed embodiment, the template image corresponding to the training image may represent a defect-free image corresponding to the training image. By comparing the training image and the template image, a candidate box of defects in the training image may be determined. The candidate box may represent an area in the training image that may be a defect.

[0181] In a possible implementation, determining a candidate frame of a defect in the training image based on the training image and the template image includes: obtaining a difference image between the training image and the template image; determining a contour in the difference image; and determining a candidate frame of the defect in the training image based on the contour.

[0182] In this implementation, the pixel values ​​of the same pixel positions of the training image and the template image can be compared to determine the difference image between the training image and the template image. In one example, the training image can be represented by img_test, the template image can be represented by img_temp, and the difference image can be represented by img_diff.

[0183] In this implementation, a contour search method such as findContours can be used to search for contours in the difference image to obtain contours in the difference image. After determining the contours in the difference image, a candidate box of a defect in the training image can be determined based on the contours in the difference image.

[0184] In this implementation, a difference image between the training image and the template image is obtained, the contour in the difference image is determined, and based on the contour, the candidate frame of the defect in the training image is determined, thereby being able to accurately determine the candidate frame of the defect in the training image through traditional image processing methods.

[0185] As an example of this implementation, obtaining the difference image between the training image and the template image includes: performing blur operations on the training image and the template image respectively to obtain a first blur image corresponding to the training image and a second blur image corresponding to the template image; and determining the difference image between the training image and the template image according to the first blur image and the second blur image. The first blur image represents the blur image corresponding to the training image, and the second blur image represents the blur image corresponding to the template image.

[0186] In one example, a Gaussian blur operation can be performed on the training image and the template image respectively to obtain a first blurred image corresponding to the training image and a second blurred image corresponding to the template image. The Gaussian kernel of the Gaussian blur operation can be 5×5, 3×3 or 7×7, etc., which is not limited here. In one example, the first blurred image can be represented by img_test_gaussian, and the second blurred image can be represented by img_temp_gaussian.

[0187] In this example, by performing blurring operations on the training image and the template image respectively, a first blurred image corresponding to the training image and a second blurred image corresponding to the template image are obtained, and based on the first blurred image and the second blurred image, a difference image between the training image and the template image is determined, thereby obtaining a smoother difference image.

[0188] In one example, the determining the difference image between the training image and the template image according to the first blurred image and the second blurred image includes: performing binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; and determining the difference image between the training image and the template image according to the first binarized image and the second binarized image. The first binarized image represents the binarized image corresponding to the first blurred image, and the second binarized image represents the binarized image corresponding to the second blurred image. In one example, the first binarized image can be represented by img_test_bina, and the second binarized image can be represented by img_temp_bina.

[0189] In this example, the OTSU method or other methods can be used to perform binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image.

[0190] In one example, determining the difference image between the training image and the template image based on the first binarized image and the second binarized image includes: for any pixel position, if the pixel value of the pixel position in the first binarized image and the second binarized image is different, then in the difference image between the training image and the template image, the pixel value of the pixel position is 0; for any pixel position, if the pixel value of the pixel position in the first binarized image and the second binarized image is the same, then in the difference image, the pixel value of the pixel position is 255.

[0191] For example, for any pixel position, if the pixel values ​​of the pixel position in the first binarized image and the second binarized image are different (i.e., one is 0 and the other is 255), then in the difference image, the pixel value of the pixel position is 0 (i.e., black); for any pixel position, if the pixel values ​​of the pixel position in the first binarized image and the second binarized image are the same (i.e., both are 0 or both are 255), then in the difference image, the pixel value of the pixel position is 255 (i.e., white).

[0192] In one example, the following steps may be used to obtain a difference image img_diff: let img_diff = img_test_bina - img_temp_bina; let the pixel values ​​that are not 0 in img_diff become 255; let the value of each pixel position in img_diff become 255 minus the corresponding pixel value.

[0193] In this example, by performing binarization operations on the first blurred image and the second blurred image respectively, a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image are obtained, and based on the first binarized image and the second binarized image, a difference image between the training image and the template image is determined, thereby reducing the interference of the intensity of ambient light for collecting the training image on defect detection.

[0194] As another example of this implementation, obtaining the difference image between the training image and the template image includes: performing a binarization operation on the training image and the template image respectively to obtain a first binarized image corresponding to the training image and a second binarized image corresponding to the template image; and determining the difference image between the training image and the template image based on the first binarized image and the second binarized image.

[0195] As an example of this implementation, determining the contour in the difference image includes: performing a morphological operation on the difference image to obtain a de-interferenced image corresponding to the difference image; and searching for the contour in the de-interferenced image as the contour in the difference image.

[0196] In this example, a dilation operation and / or an erode operation may be performed on the difference image to obtain a de-interferenced image corresponding to the difference image. In one example, a dilation operation with a kernel size of 3×3, an erode operation with a kernel size of 3×3, an erode operation with a kernel size of 3×3, an erode operation with a kernel size of 7×3, a dilation operation with a kernel size of 7×3, an erode operation with a kernel size of 3×7, and a dilation operation with a kernel size of 3×7 may be performed on the difference image in sequence to obtain a de-interferenced image corresponding to the difference image.

[0197] In this example, by performing morphological operations on the difference image, a detached image corresponding to the difference image is obtained, and contours in the detached image are found as contours in the difference image. Thus, by performing morphological operations on the difference image, horizontal and vertical interference lines in the difference image can be removed, thereby improving the accuracy of the found contours.

[0198] As an example of this implementation, determining a candidate box for the defect in the training image based on the contour includes: filtering contours in the difference image that meet preset conditions; and determining a candidate box for the defect in the training image based on contours remaining after filtering in the difference image.

[0199] In this example, if any contour in the difference image satisfies a preset condition, it can be determined that the contour belongs to a preset candidate frame that does not belong to a defect, so that the contour can be filtered. The preset condition can be set based on experience and is not limited here.

[0200] In one example, the preset conditions include at least one of the following: the area of ​​the area enclosed by the outline is smaller than a first preset area; the area of ​​the area enclosed by the outline is larger than a second preset area, wherein the second preset area is larger than the first preset area; the aspect ratio of the enclosing rectangle of the outline is smaller than a first preset threshold; the aspect ratio of the enclosing rectangle of the outline is larger than a second preset threshold, wherein the second preset threshold is larger than the first preset threshold; the average pixel value within the enclosing rectangle of the outline is greater than the preset pixel value.

[0201] Among them, the enclosing rectangle of any contour can be the circumscribed rectangle of the contour, and the sides of the enclosing rectangle of the contour are parallel to the preset coordinate axis (e.g., the xy axis). In some application scenarios, the enclosing rectangle can also be called a bounding box. For example, the enclosing rectangle of the contour can be represented by (x, y, w, h), where (x, y) is the coordinate of the upper left corner of the enclosing rectangle, w is the width of the enclosing rectangle, and h is the height of the enclosing rectangle.

[0202] For example, the first preset area AREA_TH_LOW = 20, and the second preset area AREA_TH_HIGH = 20000. If the area of ​​a region enclosed by any contour is less than 20 or greater than 20000, the contour may be filtered.

[0203] For another example, the first preset threshold is 0.1, and the second preset threshold is 10. If the aspect ratio of the enclosing rectangle of any contour is less than 0.1 or greater than 10, the contour may be filtered.

[0204] For another example, the preset pixel value PIXEL_MEAN_TH=240. If the average pixel value in the enclosing rectangle of any contour is greater than 240, the contour may be filtered.

[0205] In one example, determining a candidate frame of a defect in the training image based on the contours remaining after filtering in the difference image includes: for any contour remaining after filtering in the difference image, determining an enlarged rectangle corresponding to the enclosing rectangle of the contour as a candidate frame of the defect in the training image, wherein the enlarged rectangle coincides with the geometric center of the enclosing rectangle, the length of the enlarged rectangle is a first preset multiple of the length of the enclosing rectangle, and the width of the enlarged rectangle is a second preset multiple of the width of the enclosing rectangle, wherein both the first preset multiple and the second preset multiple are greater than 1.

[0206] The first preset multiple and the second preset multiple can be the same or different. For example, the first preset multiple and the second preset multiple are both 2, the enclosing rectangle of the outline is (x, y, w, h), and the enlarged rectangle corresponding to the enclosing rectangle of the outline is (xw / 2, yh / 2, 2×w, 2×h).

[0207] In the above example, by filtering the contours that meet the preset conditions in the difference image and determining the candidate boxes of the defects in the training image based on the contours remaining after filtering in the difference image, the interference of abnormal contours in the difference image on defect detection can be reduced.

[0208] As another example of this implementation, candidate frames of defects may be determined according to contours in the difference image, that is, in this example, the found contours may not be filtered.

[0209] In another possible implementation, determining a candidate box of a defect in the training image based on the training image and the template image includes: inputting the training image and the template image into a pre-trained second neural network, and determining the candidate box of the defect in the training image via the second neural network. The second neural network is used to determine the candidate box of the defect in the training image based on the training image and the template image.

[0210] In the embodiment of the present disclosure, the image block corresponding to the candidate box may include at least one of the following: the first image block of the candidate box on the template image, the second image block of the candidate box on the difference image, and the third image block of the candidate box on the training image. Accordingly, the defect prediction result corresponding to the candidate box may be determined based on at least one of the difference information of the first image block before and after the morphological transformation, the difference information of the second image block before and after the morphological transformation, and the difference information of the third image block before and after the morphological transformation.

[0211] In a possible implementation, the image block corresponding to the candidate box includes: a first image block of the candidate box on the template image, and a second image block of the candidate box on the difference image, wherein the difference image represents the difference image between the training image and the template image; at least inputting the difference information of the image block corresponding to the candidate box before and after the morphological transformation into the machine learning model, and obtaining the defect prediction result corresponding to the candidate box via the machine learning model, includes: determining the morphological difference feature corresponding to the candidate box according to the difference information of the first image block before and after the morphological transformation and the difference information of the second image block before and after the morphological transformation; at least inputting the morphological difference feature into the machine learning model, and obtaining the defect prediction result corresponding to the candidate box via the machine learning model. In one example, the first image block can be represented by temp_img, and the second image block can be represented by test_img.

[0212] As an example of this implementation, the determining the morphological difference features corresponding to the candidate box according to the difference information of the first image block before and after the morphological transformation and the difference information of the second image block before and after the morphological transformation includes: obtaining a first binarized image block corresponding to the first image block, and a second binarized image block corresponding to the second image block; performing a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block; performing a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block; and determining the morphological difference features corresponding to the candidate box according to a first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block, and a second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block.

[0213] In an example, the first binarized image block may be represented by temp_img_bina, and the second binarized image block may be represented by test_img_bina.

[0214] In this example, a dilation operation and / or an erosion operation may be performed on the first binary image block to obtain a first morphologically transformed image block corresponding to the first binary image block; and a dilation operation and / or an erosion operation may be performed on the second binary image block to obtain a second morphologically transformed image block corresponding to the second binary image block. By comparing the first binary image block with the first morphologically transformed image block pixel by pixel, the first number of pixels having different pixel values ​​between the first binary image block and the first morphologically transformed image block may be determined, wherein the first number of pixels represents the number of pixels having different pixel values ​​between the first binary image block and the first morphologically transformed image block. By comparing the second binary image block with the second morphologically transformed image block pixel by pixel, the second number of pixels having different pixel values ​​between the second binary image block and the second morphologically transformed image block may be determined, wherein the second number of pixels represents the number of pixels having different pixel values ​​between the second binary image block and the second morphologically transformed image block.

[0215] In this example, the morphological difference features corresponding to the candidate box are determined based on a first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block, and a second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block, thereby improving the accuracy of the defect prediction results corresponding to the candidate box.

[0216] In one example, obtaining a first binary image block corresponding to the first image block and a second binary image block corresponding to the second image block includes: performing blurring operations on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; performing binarization operations on the first blurred image block and the second blurred image block, respectively, to obtain a first binary image block corresponding to the first image block and a second binary image block corresponding to the second image block.

[0217] In this example, the first image block and the second image block can be converted into grayscale images respectively to obtain a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block. In one example, the first grayscale image block can be represented by temp_img_gray, and the second grayscale image block can be represented by test_img_gray.

[0218] In this example, the first grayscale image block and the second grayscale image block can be blurred respectively to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block. In one example, the first blurred image block can be represented by temp_img_blur, and the second blurred image block can be represented by test_img_blur. In one example, the first grayscale image block and the second grayscale image block can be Gaussian blurred respectively to obtain a first blurred image block and a second blurred image block. The Gaussian kernel of the Gaussian blur operation can be 5×5, 3×3 or 7×7, etc., which is not limited here.

[0219] In an example, the OTSU method or other methods may be used to perform binarization operations on the first blurred image block and the second blurred image block respectively to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0220] In this example, by performing blur processing before binarization, a smoother processing result can be obtained.

[0221] In one example, the performing a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block includes: performing a morphological operation on the first binarized image block based on kernels of at least two sizes to obtain at least two first morphologically transformed image blocks corresponding to the first binarized image block; the performing a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block includes: performing a morphological operation on the second binarized image block based on kernels of at least two sizes to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block; the performing a morphological operation on the second binarized image block based on kernels of at least two sizes to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block; The morphological difference feature corresponding to the candidate frame is determined based on the first number of pixels with different pixel values ​​and the second number of pixels with different pixel values ​​between the second binarized image block and the second morphologically transformed image block, including: for the at least two first morphologically transformed image blocks, respectively determining the number of pixels with different pixel values ​​between the first binarized image block and the first morphologically transformed image block, to obtain at least two first pixel numbers; for the at least two second morphologically transformed image blocks, respectively determining the number of pixels with different pixel values ​​between the second binarized image block and the second morphologically transformed image block, to obtain at least two second pixel numbers; and determining the morphological difference feature corresponding to the candidate frame according to the at least two first pixel numbers and the at least two second pixel numbers.

[0222] For example, the size of the kernel may include at least two of 3, 5, 7, 9, 11, etc., which are not limited here. In this example, based on any size, the first binary image block and the second binary image block may be respectively subjected to dilation and / or erosion operations to obtain the corresponding first morphologically transformed image block and the second morphologically transformed image block.

[0223] The morphological difference features determined according to this example can more accurately reflect the defect features in the candidate frame, and according to this example, a more accurate defect prediction result can be determined for the candidate frame.

[0224] As an example of this implementation, the first morphologically transformed image block includes a first dilated image block and a first eroded image block, and the second morphologically transformed image block includes a second dilated image block and a second eroded image block; the performing of a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block includes: performing a dilation operation on the first binarized image block to obtain a first dilated image block corresponding to the first binarized image block; performing an erosion operation on the first binarized image block to obtain a first eroded image block corresponding to the first binarized image block; the performing of a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block includes: performing a dilation operation on the second binarized image block to obtain a second dilated image block corresponding to the second binarized image block; performing an erosion operation on the second binarized image block to obtain a second eroded image block corresponding to the second binarized image block.

[0225] In this example, by performing dilation and erosion operations on the first binary image block and the second binary image block respectively, and determining difference information based on the corresponding dilated image block and eroded image block respectively, the accuracy of defect prediction for the candidate frame can be further improved.

[0226] In one example, the kernel size list kernel_size_list=[3, 5, 7, 9, 11] may be set.

[0227] Each value in the kernel size list can be used as the kernel size of the dilation operation to obtain the dilation difference feature v_ki_d under each kernel size. For example, when the kernel size is 3, v_ki_d=v_k3_d, when the kernel size is 5, v_ki_d=v_k5_d, and so on. The first binary image block can be dilated with a kernel size of i to obtain a first dilated image block; the second binary image block can be dilated with a kernel size of i to obtain a second dilated image block; the dilation difference feature v_ki_d can be determined based on the first number of pixels n1 with different pixel values ​​between the first binary image block and the first dilated image block, and the second number of pixels n2 with different pixel values ​​between the second binary image block and the second dilated image block. For example, if n2 is equal to 0, v_ki_d=[n1,n2,1]; if n2 is not equal to 0, v_ki_d=[n1,n2,n1 / n2].

[0228] Each value in the kernel size list can be used as the kernel size of the erosion operation to obtain the erosion difference feature v_ki_e under each kernel size. For example, when the kernel size is 3, v_ki_e=v_k3_e, when the kernel size is 5, v_ki_e=v_k5_e, and so on. The first binary image block can be eroded with a kernel size of i to obtain a first eroded image block; the second binary image block can be eroded with a kernel size of i to obtain a second eroded image block; the erosion difference feature v_ki_e can be determined according to the first number of pixels n1 with different pixel values ​​between the first binary image block and the first eroded image block, and the second number of pixels n2 with different pixel values ​​between the second binary image block and the second eroded image block. For example, if n2 is equal to 0, v_ki_e=[n1,n2,1]; if n2 is not equal to 0, v_ki_e=[n1,n2,n1 / n2].

[0229] After determining the dilation difference feature v_dilate = [v_k3_d, v_k5_d, v_k7_d, v_k9_d, v_k11_d] and the erosion difference feature v_erode = [v_k3_e, v_k5_e, v_k7_e, v_k9_e, v_k11_e], the defect prediction result corresponding to the candidate box can be determined according to the morphological difference feature v5 = [v_dilate, v_erode].

[0230] In a possible implementation, at least the morphological difference feature is input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model, including: performing grayscale statistics on the first image block and the second image block, respectively, to obtain a first grayscale statistical result corresponding to the first image block and a second grayscale statistical result corresponding to the second image block; determining the grayscale feature corresponding to the candidate box based on the first grayscale statistical result and the second grayscale statistical result; at least the morphological difference feature and the grayscale feature are input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0231] In this implementation, the first image block and the second image block can be converted into grayscale images respectively to obtain a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block. In an example, the first grayscale image block can be represented by temp_img_gray, and the second grayscale image block can be represented by test_img_gray.

[0232] In this implementation, the first grayscale statistical result may include the number of pixels of some or all grayscale values ​​in the first image block. For example, the grayscale histogram of the first grayscale image block may be counted to obtain the first grayscale statistical result. In one example, the grayscale histogram of the first grayscale image block may be represented by temp_hist.

[0233] The second grayscale statistics result may include the number of pixels of some or all grayscale values ​​in the second image block. For example, the grayscale histogram of the second grayscale image block may be counted to obtain the second grayscale statistics result. In one example, the grayscale histogram of the second grayscale image block may be represented by test_hist.

[0234] As an example of this implementation method, the first grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the first image block; the second grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the second image block.

[0235] In an example, the number of pixels with a grayscale value of 0 in the first image block can be represented by temp_hist[0], the number of pixels with a grayscale value of 255 in the first image block can be represented by temp_hist

[255] , the number of pixels with a grayscale value of 0 in the second image block can be represented by test_hist[0], and the number of pixels with a grayscale value of 255 in the second image block can be represented by test_hist

[255] . The defect prediction result corresponding to the candidate box can be determined by combining the grayscale feature v1=[temp_hist[0], temp_hist

[255] , test_hist[0], test_hist

[255] ].

[0236] In this example, the defect prediction result corresponding to the candidate box is determined by combining the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the first image block, as well as the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the second image block. Thus, the number of pixels with the most significant grayscale values ​​in the first image block and the second image block is used to assist in defect detection of the candidate box, thereby improving the accuracy of the defect prediction result corresponding to the candidate box.

[0237] In a possible implementation, at least the morphological difference feature is input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model, including: obtaining first contour information corresponding to the first image block, and second contour information corresponding to the second image block; determining contour features corresponding to the candidate box based on the first contour information and the second contour information; at least the morphological difference feature and the contour feature are input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0238] In this implementation, a contour search method such as findContours may be used to perform contour search on the first image block and the second image block respectively to obtain first contour information corresponding to the first image block and second contour information corresponding to the second image block.

[0239] In one example, the first contour information may be represented by v_c_temp, and the second contour information may be represented by v_c_test. The defect prediction result corresponding to the candidate box may be determined in combination with the contour feature v2=[v_c_test, v_c_temp].

[0240] In this implementation, the defect prediction result corresponding to the candidate box is determined by combining the first contour information corresponding to the first image block and the second contour information corresponding to the second image block, thereby improving the accuracy of the defect prediction result corresponding to the candidate box.

[0241] As an example of this implementation method, obtaining first contour information corresponding to the first image block and second contour information corresponding to the second image block includes: determining the first contour information corresponding to the first image block according to the contour in a first binarized image block corresponding to the first image block; and determining the second contour information corresponding to the second image block according to the contour in a second binarized image block corresponding to the second image block.

[0242] In one example, a blurring operation may be performed on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; a binarization operation may be performed on the first blurred image block and the second blurred image block, respectively, to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0243] In one example, the first grayscale image block and the second grayscale image block may be subjected to Gaussian blur operations respectively to obtain a first blurred image block and a second blurred image block, wherein the Gaussian kernel of the Gaussian blur operation may be 5×5, 3×3 or 7×7, etc., which is not limited here.

[0244] In an example, the OTSU method or other methods may be used to perform binarization operations on the first blurred image block and the second blurred image block respectively to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0245] In one example, a contour search method such as findContours may be used to search for contours on the first binary image block and the second binary image block to obtain contours in the first binary image block and contours in the second binary image block.

[0246] In an example, the first blurred image block can be represented by temp_img_blur, the second blurred image block can be represented by test_img_blur, the first binarized image block can be represented by temp_img_bina, the second binarized image block can be represented by test_img_bina, the contour in the first binarized image block can be represented by temp_img_contours, and the contour in the second binarized image block can be represented by test_img_contours.

[0247] In this example, first contour information corresponding to the first image block is determined based on the contour in the first binary image block corresponding to the first image block, and second contour information corresponding to the second image block is determined based on the contour in the second binary image block corresponding to the second image block, thereby enabling contour search to be performed more accurately.

[0248] In one example, the first contour information includes: geometric information of the largest N contours in the first binary image block, and the number of contours in the first binary image block, where N is an integer greater than or equal to 1; the second contour information includes: geometric information of the largest N contours in the second binary image block, and the number of contours in the second binary image block.

[0249] In this example, the largest N contours in the first binary image block can be determined by sorting according to the area size of the areas enclosed by the contours in the first binary image block. The largest N contours in the second binary image block can be determined by sorting according to the area size of the areas enclosed by the contours in the second binary image block.

[0250] For example, N is equal to 2. Of course, those skilled in the art can flexibly set the size of N according to the requirements of actual application scenarios, and no limitation is made here.

[0251] For example, the first contour information can be v_c_temp=[temp_count, v_c_temp_v1, v_c_temp_v2], where temp_count represents the number of contours in the first binary image block, v_c_temp_v1 represents the geometric information of the largest contour in the first binary image block, and v_c_temp_v2 represents the geometric information of the second largest contour in the first binary image block. The second contour information v_c_test=[test_count, v_c_test_v1, v_c_test_v2], where test_count represents the number of contours in the second binary image block, v_c_test_v1 represents the geometric information of the largest contour in the second binary image block.

[0252] In this example, by combining the geometric information of the largest N contours in the first binary image block, the number of contours in the first binary image block, the geometric information of the largest N contours in the second binary image block, and the number of contours in the second binary image block, more accurate defect detection can be achieved for the candidate box.

[0253] In one example, the geometric information of the contour includes at least one of the following: the area of ​​the contour, the enclosing rectangle of the contour, the central moment of the contour, the position of the geometric center of the contour, the perimeter of the contour, the non-convexity of the contour, the minimum circumscribed rectangle of the contour, the minimum circumscribed circle of the contour, the fitted ellipse of the contour, and the fitted rectangle of the contour. The fitted ellipse of the contour may represent an ellipse obtained by performing ellipse fitting on the contour. The fitted rectangle of the contour may represent a rectangle obtained by performing straight line fitting on the contour.

[0254] Taking the contour with the largest area in the second binary image block as an example, the area of ​​the contour can be represented by test_c_area1, the enclosing rectangle of the contour can be represented by (a1_x, a1_y, a1_w, a1_h), the central moment of the contour can be represented by M1, the position of the geometric center of the contour can be represented by (c1_x, c1_y), the perimeter of the contour can be represented by perimeter1, the non-convexity of the contour can be represented by is_convex1, the minimum circumscribed rectangle of the contour can be represented by (a1_xr, a1_yr, a1_wr, a1_hr), the minimum circumscribed circle of the contour can be represented by (cr1_x, cr1_y, cr1_r), the fitted ellipse of the contour can be (e11, e12, e13, e14, e15), and the fitted rectangle of the contour can be represented by (l11, l12, l13, l14).

[0255] In a possible implementation, at least the morphological difference feature is input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model, including: obtaining the width and height of the first image block; determining the size feature corresponding to the candidate box based on the width and the height; at least the morphological difference feature and the size feature are input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0256] In one example, the width of the first image block can be represented by wt, the height of the first image block can be represented by ht, and the aspect ratio of the first image block can be represented by wt / ht. The defect prediction result corresponding to the candidate box can be determined in combination with the size feature v3=[wt,ht,wt / ht] of the first image block.

[0257] In this implementation, the defect prediction result corresponding to the candidate box is determined by combining the width and height of the first image block, thereby improving the accuracy of the defect prediction result corresponding to the candidate box.

[0258] In a possible implementation, at least the morphological difference feature is input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model, including: obtaining gradient information of the first image block and gradient information of the second image block; determining the gradient feature corresponding to the candidate box based on the gradient information of the first image block and the gradient information of the second image block; at least the morphological difference feature and the gradient feature are input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0259] In one example, the gradient information of the first image block may be a Histogram of Oriented Gradient (HOG) of the first image block, the gradient information of the second image block may be a Histogram of Oriented Gradient of the second image block, the gradient information of the first image block may be represented by temp_hog_vec, and the gradient information of the second image block may be represented by test_hog_vec. The defect prediction result corresponding to the candidate box may be determined in combination with the gradient feature v4=[temp_hog_vec,test_hog_vec].

[0260] In this implementation, the defect prediction result corresponding to the candidate box is determined by combining the gradient information of the first image block and the gradient information of the second image block, thereby improving the accuracy of the defect prediction result corresponding to the candidate box.

[0261] In a possible implementation, at least the morphological difference feature is input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model, including: obtaining a difference image block between a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block; determining a difference statistical feature corresponding to the candidate box based on the difference image block; and at least the morphological difference feature and the difference statistical feature are input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0262] In one example, a blurring operation may be performed on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; a binarization operation may be performed on the first blurred image block and the second blurred image block, respectively, to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0263] In one example, the first grayscale image block and the second grayscale image block may be subjected to Gaussian blur operations respectively to obtain a first blurred image block and a second blurred image block, wherein the Gaussian kernel of the Gaussian blur operation may be 5×5, 3×3 or 7×7, etc., which is not limited here.

[0264] In one example, the first binary image block can be represented by temp_img_bina, the second binary image block can be represented by test_img_bina, the difference image block can be represented by diff_img, and the difference image block can be determined according to diff_img=test_img_bina-temp_img_bina. The difference image block can also be called a difference matrix, etc., which is not limited here.

[0265] In this implementation, defect detection is performed on the candidate frame by combining the feature information of the difference image block between the first binarized image block corresponding to the first image block and the second binarized image block corresponding to the second image block, thereby making it possible to utilize the difference information between the first binarized image block and the second binarized image block to improve the accuracy of the defect prediction result corresponding to the candidate frame.

[0266] As an example of this implementation, the feature information of the difference image block includes: the number of pixels whose pixel values ​​are not 0 in the difference image block.

[0267] In one example, the pixel values ​​that are not 0 in the difference image block may be changed to 1, and then the pixel values ​​in the difference image block may be accumulated to determine the number of pixels whose pixel values ​​are not 0 in the difference image block.

[0268] According to this example, the difference information between the first binarized image block and the second binarized image block can be determined quickly and effectively.

[0269] In one example, the number of pixels whose pixel values ​​are not 0 in the difference image block includes: the number of pixels whose pixel values ​​are not 0 in each row of pixels in the difference image block, and the number of pixels whose pixel values ​​are not 0 in each column of pixels in the difference image block.

[0270] In this example, for each row in the difference image block, the number of pixels whose pixel values ​​are not 0 can be determined respectively, and for each column in the difference image block, the number of pixels whose pixel values ​​are not 0 can be determined respectively. According to the number of pixels whose pixel values ​​are not 0 in each row of pixels in the difference image block and the number of pixels whose pixel values ​​are not 0 in each column of pixels in the difference image block, the difference statistical feature v6=[diff_project_y, diff_project_x] can be obtained, where diff_project_y and diff_project_x are both vectors, and the number of elements in diff_project_y is equal to the number of rows of the difference image block, and the number of elements in diff_project_x is equal to the number of columns of the difference image block.

[0271] According to this example, the accuracy of defect detection on the candidate frame can be further improved.

[0272] In another example, the number of pixels whose pixel values ​​are not 0 in the difference image block includes: the number of pixels whose pixel values ​​are not 0 in each row of pixels of the difference image block.

[0273] In another example, the number of pixels whose pixel values ​​are not 0 in the difference image block includes: the number of pixels whose pixel values ​​are not 0 in each column of pixels of the difference image block.

[0274] In one possible implementation, at least the morphological difference feature is input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model, including: performing feature extraction on the first image block through a pre-trained first neural network to obtain a first depth feature corresponding to the first image block; performing feature extraction on the second image block through the first neural network to obtain a second depth feature corresponding to the second image block; at least the morphological difference feature, the first depth feature, and the second depth feature are input into a machine learning model, and a defect prediction result corresponding to the candidate box is obtained through the machine learning model.

[0275] In this implementation, the first neural network may be a deep learning model. For example, the first neural network may adopt a network structure such as LeNet5, AlexNet, etc. The first neural network may be pre-trained using a data set such as MINIST.

[0276] In one example, a first grayscale image block corresponding to the first image block can be input into a pre-trained first neural network, and feature extraction is performed on the first grayscale image block through the first neural network to obtain a first depth feature corresponding to the first image block; a second grayscale image block corresponding to the second image block can be input into the first neural network, and feature extraction is performed on the second grayscale image block through the first neural network to obtain a second depth feature corresponding to the second image block.

[0277] For example, the first grayscale image block temp_img_gray may be subjected to a preprocessing operation of the LeNet5 model to obtain a first preprocessing feature temp_img_pre corresponding to the first grayscale image block temp_img_gray; the second grayscale image block test_img_gray may be subjected to a preprocessing operation of the LeNet5 model to obtain a second preprocessing feature test_img_pre corresponding to the second grayscale image block test_img_gray. The first preprocessing feature temp_img_pre may be inferred by the LeNet5 model, and the 84-dimensional feature vector output by the penultimate layer of the LeNet5 model may be used as the first depth feature, or the 10-dimensional feature vector output by the last layer of the LeNet5 model may be used as the first depth feature. The second preprocessing feature test_img_pre may be inferred by the LeNet5 model, and the 84-dimensional feature vector output by the penultimate layer of the LeNet5 model may be used as the second depth feature, or the 10-dimensional feature vector output by the last layer of the LeNet5 model may be used as the second depth feature.

[0278] In another example, the first image block can be input into a pre-trained first neural network, and feature extraction is performed on the first image block through the first neural network to obtain a first depth feature corresponding to the first image block; the second image block can be input into the first neural network, and feature extraction is performed on the second image block through the first neural network to obtain a second depth feature corresponding to the second image block.

[0279] In an example, the first depth feature can be represented by v_temp_deep, the second depth feature can be represented by v_test_deep, and the defect prediction result corresponding to the candidate box can be determined by combining the depth feature v_deep=[v_test_deep, v_temp_deep].

[0280] In this implementation, features of the first image block and the second image block are extracted through a first neural network to obtain a first depth feature corresponding to the first image block and a second depth feature corresponding to the second image block, thereby effectively supplementing features not considered by human prior knowledge, thereby further improving the accuracy of defect detection.

[0281] In the disclosed embodiment, the defect frame of the training image can be obtained from the annotation data of the training image. For any candidate frame of the training image, if the intersections of the candidate frame and each defect frame of the training image are empty, the annotation information corresponding to the candidate frame can be determined as non-defective; if the candidate frame only has an intersection with one defect frame of the training image, the defect type corresponding to the defect frame can be determined as the annotation information corresponding to the candidate frame; if the candidate frame has an intersection with at least two defect frames of the training image, the defect type corresponding to the defect frame with the largest intersection with the candidate frame among the at least two defect frames can be determined as the annotation information corresponding to the candidate frame.

[0282] In one possible implementation, the machine learning model may be a machine learning model based on ensemble learning. For example, the machine learning model may be a machine learning model based on a random forest algorithm, XGBoost, CatBoost, etc., which is not limited here. Taking the random forest algorithm as an example, K-fold cross validation (e.g., 5-fold cross validation) may be used to perform a model search on a training set (including features corresponding to the candidate box and annotation information corresponding to the candidate box) to obtain the optimal algorithm parameter para_opt, and then the algorithm parameter para_opt is fixed to train the machine learning model on the entire data set (including the training set and the test set). For example, para_opt = {'criterion':'gini', 'min_samples_leaf':1, 'min_samples_split':6, 'n_estimators':100}. In this implementation, by adopting a machine learning model based on ensemble learning, the speed of defect detection can be improved.

[0283] In another possible implementation, the machine learning model may be a deep learning model.

[0284] The following describes a specific application scenario to illustrate the training method of the machine learning model for defect detection provided by the embodiment of the present disclosure. In the application scenario, a training image corresponding to the PCB and a template image corresponding to the training image are obtained.

[0285] In this application scenario, a candidate box of a defect in a training image may be determined according to the training image and the template image.

[0286] Traditional image processing methods can be used to extract manual features v_traditional = [v1, v2, v3, v4, v5, v6] from the candidate boxes.

[0287] Among them, v1 is the grayscale feature, v1 = [temp_hist[0], temp_hist

[255] , test_hist[0], test_hist

[255] ];

[0288] v2 is the contour feature, v2 = [v_c_test, v_c_temp];

[0289] v3 is the size characteristic, v3 = [wt, ht, wt / ht];

[0290] v4 is the gradient feature, v4 = [temp_hog_vec, test_hog_vec];

[0291] v5 is the morphological difference feature, v5 = [v_dilate, v_erode];

[0292] v6 is the difference statistical feature, v6 = [diff_project_y, diff_project_x].

[0293] A pre-trained first neural network may be used to extract a deep feature v_deep=[v_test_deep, v_temp_deep] for the candidate box.

[0294] The manual feature v_traditional and the deep feature v_deep may be input into the machine learning model to obtain the defect prediction result corresponding to the candidate box; the machine learning model may be trained according to the annotation information corresponding to the candidate box and the defect prediction result.

[0295] It can be understood that the above-mentioned various method embodiments mentioned in the present disclosure can be combined with each other to form a combined embodiment without violating the principle logic. Due to space limitations, the present disclosure will not repeat them. It can be understood by those skilled in the art that in the above-mentioned method of the specific implementation method, the specific execution order of each step should be determined according to its function and possible internal logic.

[0296] In addition, the present disclosure also provides a defect detection device, a training device for a machine learning model for defect detection, an electronic device, a computer-readable storage medium, and a computer program product, all of which can be used to implement any defect detection method or training method for a machine learning model for defect detection provided by the present disclosure. The corresponding technical solutions and technical effects can be found in the corresponding records in the method part and will not be repeated here.

[0297] Figure 5 FIG. 1 is a block diagram of a defect detection device provided by an embodiment of the present disclosure. Figure 5 As shown, the defect detection device comprises:

[0298] A first acquisition module 51 is used to acquire an image to be detected and a template image corresponding to the image to be detected;

[0299] A first determining module 52, configured to determine a candidate frame of a defect in the image to be detected according to the image to be detected and the template image;

[0300] The second determination module 53 is used to determine the defect detection result corresponding to the candidate box at least according to the difference information of the image block corresponding to the candidate box before and after the morphological transformation.

[0301] In one possible implementation,

[0302] The image blocks corresponding to the candidate frame include: a first image block of the candidate frame on the template image, and a second image block of the candidate frame on a difference image, wherein the difference image represents a difference image between the image to be detected and the template image;

[0303] The second determination module 53 is used to determine the defect detection result corresponding to the candidate frame at least based on the difference information of the first image block before and after the morphological transformation, and the difference information of the second image block before and after the morphological transformation.

[0304] In a possible implementation, the second determining module 53 is used to:

[0305] Obtaining a first binarized image block corresponding to the first image block, and a second binarized image block corresponding to the second image block;

[0306] Performing a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block;

[0307] Performing a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block;

[0308] The defect detection result corresponding to the candidate frame is determined according to a first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block, and a second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block.

[0309] In a possible implementation, the second determining module 53 is used to:

[0310] Performing blur operations on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block;

[0311] A binarization operation is performed on the first blurred image block and the second blurred image block respectively to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0312] In a possible implementation, the second determining module 53 is used to:

[0313] Based on kernels of at least two sizes, a morphological operation is performed on the first binarized image block to obtain at least two first morphologically transformed image blocks corresponding to the first binarized image block;

[0314] Based on kernels of at least two sizes, a morphological operation is performed on the second binarized image block to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block;

[0315] For the at least two first morphologically transformed image blocks, the number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block is determined respectively, and at least two first pixel numbers are obtained; for the at least two second morphologically transformed image blocks, the number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block is determined respectively, and at least two second pixel numbers are obtained; and the defect detection result corresponding to the candidate frame is determined according to the at least two first pixel numbers and the at least two second pixel numbers.

[0316] In one possible implementation,

[0317] The first morphological transformation image block includes a first dilated image block and a first eroded image block, and the second morphological transformation image block includes a second dilated image block and a second eroded image block;

[0318] The second determination module 53 is used to: perform a dilation operation on the first binarized image block to obtain a first dilated image block corresponding to the first binarized image block; perform an erosion operation on the first binarized image block to obtain a first eroded image block corresponding to the first binarized image block; perform a dilation operation on the second binarized image block to obtain a second dilated image block corresponding to the second binarized image block; perform an erosion operation on the second binarized image block to obtain a second eroded image block corresponding to the second binarized image block.

[0319] In a possible implementation, the second determining module 53 is used to:

[0320] Performing grayscale statistics on the first image block and the second image block respectively to obtain a first grayscale statistical result corresponding to the first image block and a second grayscale statistical result corresponding to the second image block;

[0321] The defect detection result corresponding to the candidate frame is determined based on at least difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, the first grayscale statistical result, and the second grayscale statistical result.

[0322] In one possible implementation,

[0323] The first grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the first image block;

[0324] The second grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the second image block.

[0325] In a possible implementation, the second determining module 53 is used to:

[0326] Obtaining first contour information corresponding to the first image block, and second contour information corresponding to the second image block;

[0327] The defect detection result corresponding to the candidate frame is determined at least based on difference information of the first image block before and after morphological transformation, difference information of the second image block before and after morphological transformation, the first contour information, and the second contour information.

[0328] In one possible implementation,

[0329] The first contour information includes: geometric information of the largest N contours in the first binary image block, and the number of contours in the first binary image block, where N is an integer greater than or equal to 1;

[0330] The second contour information includes: geometric information of the largest N contours in the second binary image block, and the number of contours in the second binary image block.

[0331] In a possible implementation, the second determining module 53 is used to:

[0332] Obtaining the width and height of the first image block;

[0333] The defect detection result corresponding to the candidate frame is determined at least based on difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, the width and the height.

[0334] In a possible implementation, the second determining module 53 is used to:

[0335] Obtaining gradient information of the first image block and gradient information of the second image block;

[0336] The defect detection result corresponding to the candidate box is determined based on at least difference information of the first image block before and after morphological transformation, difference information of the second image block before and after morphological transformation, gradient information of the first image block, and gradient information of the second image block.

[0337] In a possible implementation, the second determining module 53 is used to:

[0338] Obtain a difference image block between a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block;

[0339] Obtaining feature information of the difference image block;

[0340] The defect detection result corresponding to the candidate frame is determined at least based on difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, and feature information of the difference image block.

[0341] In a possible implementation manner, the feature information of the difference image block includes:

[0342] The number of pixels whose pixel values ​​are not 0 in each row of pixels of the difference image block, and the number of pixels whose pixel values ​​are not 0 in each column of pixels of the difference image block.

[0343] In a possible implementation, the first determining module 52 is used to:

[0344] Obtaining a difference image between the image to be detected and the template image;

[0345] determining contours in the difference image;

[0346] According to the outline, a candidate frame of the defect in the image to be detected is determined.

[0347] In a possible implementation, the first determining module 52 is used to:

[0348] Performing blur operations on the image to be detected and the template image respectively to obtain a first blurred image corresponding to the image to be detected and a second blurred image corresponding to the template image;

[0349] A difference image between the image to be detected and the template image is determined according to the first blurred image and the second blurred image.

[0350] In a possible implementation, the first determining module 52 is used to:

[0351] Performing a binarization operation on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image;

[0352] A difference image between the image to be detected and the template image is determined according to the first binarized image and the second binarized image.

[0353] In a possible implementation, the first determining module 52 is used to:

[0354] Performing morphological operations on the difference image to obtain a de-interferenced image corresponding to the difference image;

[0355] Find the contour in the de-interference image as the contour in the difference image.

[0356] In a possible implementation, the first determining module 52 is used to:

[0357] Filtering contours that meet preset conditions in the difference image;

[0358] A candidate frame of the defect in the image to be detected is determined according to the contour remaining in the difference image after filtering.

[0359] In a possible implementation, the second determining module 53 is used to:

[0360] Extracting features from the first image block using a pre-trained first neural network to obtain a first depth feature corresponding to the first image block;

[0361] Performing feature extraction on the second image block by using the first neural network to obtain a second depth feature corresponding to the second image block;

[0362] The defect detection result corresponding to the candidate box is determined based on at least difference information of the first image block before and after the morphological transformation, difference information of the second image block before and after the morphological transformation, the first depth feature, and the second depth feature.

[0363] In a possible implementation, the second determining module 53 is used to:

[0364] At least the difference information of the image block corresponding to the candidate box before and after the morphological transformation is input into a pre-trained machine learning model, and the defect detection result corresponding to the candidate box is obtained through the machine learning model.

[0365] In a possible implementation manner, the image to be detected is an image to be detected corresponding to a printed circuit board.

[0366] Figure 6 A block diagram of a training device for a machine learning model for defect detection provided by an embodiment of the present disclosure is shown. Figure 6 As shown, the training device of the machine learning model for defect detection includes:

[0367] A second acquisition module 61 is used to acquire a training image and a template image corresponding to the training image;

[0368] A third determination module 62, configured to determine a candidate frame of a defect in the training image according to the training image and the template image;

[0369] A prediction module 63, configured to input at least difference information of the image block corresponding to the candidate box before and after the morphological transformation into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model;

[0370] The training module 64 is used to train the machine learning model according to the annotation information corresponding to the candidate box and the defect prediction result.

[0371] In a possible implementation, the third determination module 62 is used to: obtain a difference image between the training image and the template image; determine a contour in the difference image; and determine a candidate frame of a defect in the training image according to the contour.

[0372] In a possible implementation, the third determination module 62 is used to: perform blur operations on the training image and the template image respectively to obtain a first blurred image corresponding to the training image and a second blurred image corresponding to the template image; and determine a difference image between the training image and the template image according to the first blurred image and the second blurred image. The first blurred image represents the blurred image corresponding to the training image, and the second blurred image represents the blurred image corresponding to the template image.

[0373] In a possible implementation, the third determination module 62 is used to: perform binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; and determine a difference image between the training image and the template image based on the first binarized image and the second binarized image.

[0374] In a possible implementation, the third determination module 62 is used to: for any pixel position, if the pixel value of the pixel position in the first binarized image and the second binarized image is different, then in the difference image between the training image and the template image, the pixel value of the pixel position is 0; for any pixel position, if the pixel value of the pixel position in the first binarized image and the second binarized image is the same, then in the difference image, the pixel value of the pixel position is 255.

[0375] In a possible implementation, the third determination module 62 is used to: perform binarization operations on the training image and the template image respectively to obtain a first binarized image corresponding to the training image and a second binarized image corresponding to the template image; and determine a difference image between the training image and the template image based on the first binarized image and the second binarized image.

[0376] In a possible implementation, the third determination module 62 is used to: perform morphological operations on the difference image to obtain a de-interferenced image corresponding to the difference image; and search for contours in the de-interferenced image as contours in the difference image.

[0377] In a possible implementation, the third determination module 62 is used to: filter contours that meet preset conditions in the difference image; and determine candidate frames of defects in the training image according to contours remaining after filtering in the difference image.

[0378] In a possible implementation, the preset condition includes at least one of the following: the area of ​​the area enclosed by the outline is smaller than a first preset area; the area of ​​the area enclosed by the outline is larger than a second preset area, wherein the second preset area is larger than the first preset area; the aspect ratio of the enclosing rectangle of the outline is smaller than a first preset threshold; the aspect ratio of the enclosing rectangle of the outline is larger than a second preset threshold, wherein the second preset threshold is larger than the first preset threshold; the average pixel value within the enclosing rectangle of the outline is larger than the preset pixel value.

[0379] In one possible implementation, the third determination module 62 is used to: for any contour remaining after filtering in the difference image, determine the enlarged rectangle corresponding to the enclosing rectangle of the contour as a candidate frame of the defect in the training image, wherein the enlarged rectangle coincides with the geometric center of the enclosing rectangle, the length of the enlarged rectangle is a first preset multiple of the length of the enclosing rectangle, and the width of the enlarged rectangle is a second preset multiple of the width of the enclosing rectangle, wherein the first preset multiple and the second preset multiple are both greater than 1.

[0380] In a possible implementation, the third determination module 62 is used to: input the training image and the template image into a pre-trained second neural network, and determine the candidate box of the defect in the training image via the second neural network. The second neural network is used to determine the candidate box of the defect in the training image based on the training image and the template image.

[0381] In a possible implementation, the image blocks corresponding to the candidate box include: a first image block of the candidate box on the template image, and a second image block of the candidate box on the difference image, wherein the difference image represents the difference image between the training image and the template image; the prediction module 63 is used to: determine the morphological difference features corresponding to the candidate box based on the difference information of the first image block before and after the morphological transformation and the difference information of the second image block before and after the morphological transformation; at least input the morphological difference features into a machine learning model, and obtain the defect prediction result corresponding to the candidate box through the machine learning model.

[0382] In a possible implementation, the prediction module 63 is used to: obtain a first binarized image block corresponding to the first image block, and a second binarized image block corresponding to the second image block; perform a morphological operation on the first binarized image block to obtain a first morphologically transformed image block corresponding to the first binarized image block; perform a morphological operation on the second binarized image block to obtain a second morphologically transformed image block corresponding to the second binarized image block; and determine the morphological difference features corresponding to the candidate box based on a first number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block, and a second number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block.

[0383] In a possible implementation, the prediction module 63 is used to: perform a blurring operation on a first grayscale image block corresponding to the first image block and a second grayscale image block corresponding to the second image block, respectively, to obtain a first blurred image block corresponding to the first image block and a second blurred image block corresponding to the second image block; perform a binarization operation on the first blurred image block and the second blurred image block, respectively, to obtain a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block.

[0384] In a possible implementation, the prediction module 63 is used to: perform a morphological operation on the first binarized image block based on kernels of at least two sizes to obtain at least two first morphologically transformed image blocks corresponding to the first binarized image block; perform a morphological operation on the second binarized image block based on kernels of at least two sizes to obtain at least two second morphologically transformed image blocks corresponding to the second binarized image block; for the at least two first morphologically transformed image blocks, respectively determine the number of pixels having different pixel values ​​between the first binarized image block and the first morphologically transformed image block to obtain at least two first pixel numbers; for the at least two second morphologically transformed image blocks, respectively determine the number of pixels having different pixel values ​​between the second binarized image block and the second morphologically transformed image block to obtain at least two second pixel numbers; and determine the morphological difference features corresponding to the candidate box based on the at least two first pixel numbers and the at least two second pixel numbers.

[0385] In a possible implementation, the first morphological transformation image block includes a first dilated image block and a first eroded image block, and the second morphological transformation image block includes a second dilated image block and a second eroded image block; the prediction module 63 is used to: perform a dilation operation on the first binarized image block to obtain a first dilated image block corresponding to the first binarized image block; perform an erosion operation on the first binarized image block to obtain a first eroded image block corresponding to the first binarized image block; perform a dilation operation on the second binarized image block to obtain a second dilated image block corresponding to the second binarized image block; perform an erosion operation on the second binarized image block to obtain a second eroded image block corresponding to the second binarized image block.

[0386] In a possible implementation, the prediction module 63 is used to: perform grayscale statistics on the first image block and the second image block, respectively, to obtain a first grayscale statistical result corresponding to the first image block and a second grayscale statistical result corresponding to the second image block; determine the grayscale features corresponding to the candidate box based on the first grayscale statistical result and the second grayscale statistical result; at least input the morphological difference features and the grayscale features into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model.

[0387] In a possible implementation, the first grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the first image block; the second grayscale statistical result includes: the number of pixels with a grayscale value of 0 and the number of pixels with a grayscale value of 255 in the second image block.

[0388] In one possible implementation, the prediction module 63 is used to: obtain first contour information corresponding to the first image block, and second contour information corresponding to the second image block; determine contour features corresponding to the candidate box based on the first contour information and the second contour information; at least input the morphological difference features and the contour features into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model.

[0389] In a possible implementation, the prediction module 63 is used to: determine first contour information corresponding to the first image block based on a contour in a first binary image block corresponding to the first image block; and determine second contour information corresponding to the second image block based on a contour in a second binary image block corresponding to the second image block.

[0390] In a possible implementation, the first contour information includes: geometric information of the largest N contours in the first binary image block, and the number of contours in the first binary image block, where N is an integer greater than or equal to 1; the second contour information includes: geometric information of the largest N contours in the second binary image block, and the number of contours in the second binary image block.

[0391] In a possible implementation, the geometric information of the contour includes at least one of the following: the area of ​​the contour, the enclosing rectangle of the contour, the central moment of the contour, the position of the geometric center of the contour, the perimeter of the contour, the non-convexity of the contour, the minimum circumscribed rectangle of the contour, the minimum circumscribed circle of the contour, the fitted ellipse of the contour, and the fitted rectangle of the contour. Among them, the fitted ellipse of the contour can represent an ellipse obtained by performing ellipse fitting on the contour. The fitted rectangle of the contour can represent a rectangle obtained by performing straight line fitting on the contour.

[0392] In one possible implementation, the prediction module 63 is used to: obtain the width and height of the first image block; determine the size features corresponding to the candidate box based on the width and the height; input at least the morphological difference features and the size features into a machine learning model, and obtain the defect prediction result corresponding to the candidate box through the machine learning model.

[0393] In one possible implementation, the prediction module 63 is used to: obtain gradient information of the first image block and gradient information of the second image block; determine the gradient features corresponding to the candidate box based on the gradient information of the first image block and the gradient information of the second image block; input at least the morphological difference features and the gradient features into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model.

[0394] In one possible implementation, the prediction module 63 is used to: obtain a difference image block between a first binarized image block corresponding to the first image block and a second binarized image block corresponding to the second image block; determine the difference statistical features corresponding to the candidate box based on the difference image block; input at least the morphological difference features and the difference statistical features into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model.

[0395] In a possible implementation manner, the feature information of the difference image block includes: the number of pixels whose pixel values ​​are not 0 in the difference image block.

[0396] In a possible implementation, the number of pixels with non-zero pixel values ​​in the difference image block includes: the number of pixels with non-zero pixel values ​​in each row of pixels in the difference image block, and the number of pixels with non-zero pixel values ​​in each column of pixels in the difference image block.

[0397] In one possible implementation, the prediction module 63 is used to: perform feature extraction on the first image block through a pre-trained first neural network to obtain a first depth feature corresponding to the first image block; perform feature extraction on the second image block through the first neural network to obtain a second depth feature corresponding to the second image block; at least input the morphological difference feature, the first depth feature and the second depth feature into a machine learning model, and obtain a defect prediction result corresponding to the candidate box through the machine learning model.

[0398] In some embodiments, the functions or modules included in the device provided by the embodiments of the present disclosure can be used to execute the method described in the above method embodiments. Its specific implementation and technical effects can refer to the description of the above method embodiments. For the sake of brevity, they will not be repeated here.

[0399] The present disclosure also provides a computer-readable storage medium having computer program instructions stored thereon, wherein the computer program instructions implement the above method when executed by a processor. The computer-readable storage medium may be a non-volatile computer-readable storage medium, or may be a volatile computer-readable storage medium.

[0400] The embodiment of the present disclosure further provides a computer program, including a computer-readable code. When the computer-readable code is executed in an electronic device, a processor in the electronic device executes the above method.

[0401] The embodiments of the present disclosure also provide a computer program product, including a computer-readable code, or a non-volatile computer-readable storage medium carrying the computer-readable code. When the computer-readable code runs in an electronic device, a processor in the electronic device executes the above method.

[0402] An embodiment of the present disclosure also provides an electronic device, comprising: one or more processors; a memory for storing executable instructions; wherein the one or more processors are configured to call the executable instructions stored in the memory to execute the above method.

[0403] The electronic device may be provided as a terminal, a server, or a device in other forms.

[0404] Figure 7 1 is a block diagram of an electronic device 1900 provided in an embodiment of the present disclosure. For example, the electronic device 1900 may be provided as a server. Figure 7 , the electronic device 1900 includes a processing component 1922, which further includes one or more processors, and a memory resource represented by a memory 1932 for storing instructions executable by the processing component 1922, such as an application. The application stored in the memory 1932 may include one or more modules, each corresponding to a set of instructions. In addition, the processing component 1922 is configured to execute instructions to perform the above method.

[0405] The electronic device 1900 may also include a power supply component 1926 configured to perform power management of the electronic device 1900, a wired or wireless network interface 1950 configured to connect the electronic device 1900 to a network, and an input / output interface 1958 (I / O interface). The electronic device 1900 may operate based on an operating system stored in the memory 1932, such as a Microsoft Server operating system (Windows Server 2003). TM ), a graphical user interface operating system launched by Apple (MacOS X TM ), a multi-user, multi-process computer operating system (Unix TM ), a free and open source Unix-like operating system (Linux TM ), an open source Unix-like operating system (FreeBSD TM ) or similar.

[0406] In an exemplary embodiment, a non-volatile computer-readable storage medium is also provided, such as a memory 1932 including computer program instructions, which can be executed by the processing component 1922 of the electronic device 1900 to perform the above method.

[0407] The present disclosure may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present disclosure.

[0408] A computer-readable storage medium may be a tangible device that can hold and store instructions used by an instruction execution device. A computer-readable storage medium may be, for example, but not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination of the foregoing. More specific examples of computer-readable storage media (a non-exhaustive list) include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, such as a punch card or a raised structure in a groove on which instructions are stored, and any suitable combination of the foregoing. As used herein, a computer-readable storage medium is not to be interpreted as a transient signal per se, such as a radio wave or other freely propagating electromagnetic wave, an electromagnetic wave propagating through a waveguide or other transmission medium (e.g., a light pulse through a fiber optic cable), or an electrical signal transmitted through a wire.

[0409] The computer-readable program instructions described herein can be downloaded from a computer-readable storage medium to each computing / processing device, or downloaded to an external computer or external storage device via a network, such as the Internet, a local area network, a wide area network, and / or a wireless network. The network can include copper transmission cables, optical fiber transmissions, wireless transmissions, routers, firewalls, switches, gateway computers, and / or edge servers. The network adapter card or network interface in each computing / processing device receives the computer-readable program instructions from the network and forwards the computer-readable program instructions for storage in the computer-readable storage medium in each computing / processing device.

[0410] The computer program instructions for performing the operation of the present disclosure may be assembly instructions, instruction set architecture (ISA) instructions, machine instructions, machine-related instructions, microcode, firmware instructions, state setting data, or source code or object code written in any combination of one or more programming languages, including object-oriented programming languages, such as Smalltalk, C++, etc., and conventional procedural programming languages, such as "C" language or similar programming languages. Computer-readable program instructions may be executed completely on a user's computer, partially on a user's computer, as an independent software package, partially on a user's computer, partially on a remote computer, or completely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., using an Internet service provider to connect via the Internet). In some embodiments, an electronic circuit, such as a programmable logic circuit, a field programmable gate array (FPGA), or a programmable logic array (PLA), may be customized by utilizing the state information of the computer-readable program instructions, and the electronic circuit may execute the computer-readable program instructions, thereby realizing various aspects of the present disclosure.

[0411] Various aspects of the present disclosure are described herein with reference to the flowcharts and / or block diagrams of the methods, devices (systems) and computer program products according to the embodiments of the present disclosure. It should be understood that each box in the flowchart and / or block diagram and the combination of each box in the flowchart and / or block diagram can be implemented by computer-readable program instructions.

[0412] These computer-readable program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, thereby producing a machine, so that when these instructions are executed by the processor of the computer or other programmable data processing device, a device that implements the functions / actions specified in one or more boxes in the flowchart and / or block diagram is generated. These computer-readable program instructions can also be stored in a computer-readable storage medium, and these instructions cause the computer, programmable data processing device, and / or other equipment to work in a specific manner, so that the computer-readable medium storing the instructions includes a manufactured product, which includes instructions for implementing various aspects of the functions / actions specified in one or more boxes in the flowchart and / or block diagram.

[0413] Computer-readable program instructions may also be loaded onto a computer, other programmable data processing apparatus, or other device so that a series of operating steps are performed on the computer, other programmable data processing apparatus, or other device to produce a computer-implemented process, thereby causing the instructions executed on the computer, other programmable data processing apparatus, or other device to implement the functions / actions specified in one or more boxes in the flowchart and / or block diagram.

[0414] The flow chart and block diagram in the accompanying drawings show the possible architecture, function and operation of the system, method and computer program product according to multiple embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a part of a module, program segment or instruction, and a part of the module, program segment or instruction includes one or more executable instructions for realizing the specified logical function. In some alternative implementations, the function marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous square boxes can actually be executed substantially in parallel, and they can sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or action, or can be implemented with a combination of special hardware and computer instructions.

[0415] The computer program product may be implemented in hardware, software or a combination thereof. In one optional embodiment, the computer program product is embodied as a computer storage medium, and in another optional embodiment, the computer program product is embodied as a software product, such as a software development kit (SDK) and the like.

[0416] The above description of various embodiments tends to emphasize the differences between the various embodiments. The same or similar aspects can be referenced to each other, and for the sake of brevity, they will not be repeated herein.

[0417] If the technical solution of the embodiments of the present disclosure involves personal information, the product using the technical solution of the embodiments of the present disclosure has clearly informed the personal information processing rules and obtained the individual's voluntary consent before processing the personal information. If the technical solution of the embodiments of the present disclosure involves sensitive personal information, the product using the technical solution of the embodiments of the present disclosure has obtained the individual's separate consent before processing the sensitive personal information, and at the same time meets the requirement of "explicit consent". For example, at personal information collection devices such as cameras, clear and prominent signs are set to inform that the personal information collection scope has been entered and personal information will be collected. If the individual voluntarily enters the collection scope, it is deemed that he or she agrees to collect his or her personal information; or on the device for processing personal information, when the personal information processing rules are notified by obvious signs / information, the individual's authorization is obtained through pop-up information or by asking the individual to upload his or her personal information; the personal information processing rules may include information such as the personal information processor, the purpose of personal information processing, the processing method, and the type of personal information processed.

[0418] The embodiments of the present disclosure have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and changes will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or improvements to the technology in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.

Claims

1. A defect detection method, characterized in that: include: Acquire an image to be detected and a template image corresponding to the image to be detected; Obtaining a difference image between the image to be detected and the template image; determining contours in the difference image; Determining a candidate frame of a defect in the image to be detected according to the contour; Determine a defect detection result corresponding to the candidate frame at least according to difference information of a first image block of the candidate frame on the template image before and after morphological transformation, and difference information of a second image block of the candidate frame on the difference image before and after morphological transformation; Wherein, the obtaining of the difference image between the image to be detected and the template image includes: performing blurring operation on the image to be detected and the template image respectively to obtain a first blurred image corresponding to the image to be detected and a second blurred image corresponding to the template image; performing binarization operation on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; and determining the difference image between the image to be detected and the template image according to the difference between the first binarized image and the second binarized image.

2. The method according to claim 1, characterized in that: Determining the contour in the difference image includes: Performing morphological operations on the difference image to obtain a de-interferenced image corresponding to the difference image; Find the contour in the de-interference image as the contour in the difference image.

3. The method according to claim 1, characterized in that The step of determining a candidate frame of a defect in the image to be detected according to the contour comprises: Filtering contours that meet preset conditions in the difference image; A candidate frame of the defect in the image to be detected is determined according to the contour remaining in the difference image after filtering.

4. The method according to any one of claims 1 to 3, characterized in that The step of determining the defect detection result corresponding to the candidate frame at least based on difference information between a first image block of the candidate frame on the template image before and after morphological transformation, and difference information between a second image block of the candidate frame on the difference image before and after morphological transformation, comprises: At least the difference information of the first image block before and after the morphological transformation and the difference information of the second image block before and after the morphological transformation are input into a pre-trained machine learning model, and the defect detection result corresponding to the candidate box is obtained through the machine learning model.

5. The method according to any one of claims 1 to 3, characterized in that: The image to be detected is the image to be detected corresponding to the printed circuit board.

6. A method for training a machine learning model for defect detection, characterized in that: include: Acquire a training image and a template image corresponding to the training image; Obtaining a difference image between the training image and the template image; determining contours in the difference image; Determining a candidate frame of a defect in the training image according to the contour; At least inputting difference information of a first image block of the candidate frame on the template image before and after morphological transformation and difference information of a second image block of the candidate frame on the difference image before and after morphological transformation into a machine learning model, and obtaining a defect prediction result corresponding to the candidate frame through the machine learning model; Training the machine learning model according to the annotation information corresponding to the candidate box and the defect prediction result; Wherein, obtaining the difference image between the training image and the template image includes: performing blurring operation on the training image and the template image respectively to obtain a first blurred image corresponding to the training image and a second blurred image corresponding to the template image; performing binarization operation on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; and determining the difference image between the training image and the template image according to the difference between the first binarized image and the second binarized image.

7. A defect detection device, characterized in that: include: A first acquisition module, used to acquire an image to be detected and a template image corresponding to the image to be detected; A first determination module is used to obtain a difference image between the image to be detected and the template image, determine a contour in the difference image, and determine a candidate frame of a defect in the image to be detected according to the contour; A second determination module is used to determine a defect detection result corresponding to the candidate frame based on at least difference information between a first image block of the candidate frame on the template image before and after morphological transformation, and difference information between a second image block of the candidate frame on the difference image before and after morphological transformation; Wherein, the first determining module is specifically used for: Perform blurring operations on the image to be detected and the template image respectively to obtain a first blurred image corresponding to the image to be detected and a second blurred image corresponding to the template image; perform binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; determine a difference image between the image to be detected and the template image according to a difference between the first binarized image and the second binarized image.

8. A training device for a machine learning model for defect detection, characterized in that: include: A second acquisition module, used to acquire a training image and a template image corresponding to the training image; A third determination module is used to obtain a difference image between the training image and the template image, determine a contour in the difference image, and determine a candidate frame of the defect in the training image based on the contour; A prediction module, configured to input at least difference information of a first image block of the candidate frame on the template image before and after morphological transformation and difference information of a second image block of the candidate frame on the difference image before and after morphological transformation into a machine learning model, and obtain a defect prediction result corresponding to the candidate frame through the machine learning model; A training module, used to train the machine learning model according to the annotation information corresponding to the candidate box and the defect prediction result; Wherein, the third determination module is specifically used for: Perform blurring operations on the training image and the template image respectively to obtain a first blurred image corresponding to the training image and a second blurred image corresponding to the template image; perform binarization operations on the first blurred image and the second blurred image respectively to obtain a first binarized image corresponding to the first blurred image and a second binarized image corresponding to the second blurred image; determine a difference image between the training image and the template image according to a difference between the first binarized image and the second binarized image.

9. An electronic device, characterized in that: include: one or more processors; a memory for storing executable instructions; The one or more processors are configured to call executable instructions stored in the memory to execute the method according to any one of claims 1 to 6.

10. A computer-readable storage medium having computer program instructions stored thereon, characterized in that: When the computer program instructions are executed by a processor, the method according to any one of claims 1 to 6 is implemented.