Disclosure of Invention
In view of the above, the present invention provides a method, an apparatus and a system for detecting a PCB, which are used to solve the problems of low efficiency, high missing rate, strong subjectivity and easy external interference of PCB detection in the existing manual detection mode, and the technical scheme is as follows:
a PCB board detection method comprises the following steps:
acquiring a PCB image of a PCB to be detected;
carrying out alignment pretreatment on the PCB image;
matching the preprocessed PCB image with a pre-established standard template, and determining a to-be-detected area of the preprocessed PCB image when a matching condition is met;
extracting the characteristics of the components in the region to be detected;
and classifying and identifying the characteristics of the components by using a pre-trained Support Vector Machine (SVM), and judging whether the specified components exist in the region to be detected or not according to a classification and identification result.
Preferably, the aligning the PCB image comprises:
acquiring a PCB object by the PCB image and a pre-constructed background image by using a background difference method;
binarizing the PCB object to obtain a foreground object, and performing morphological processing on the foreground object;
determining edge pixels from the foreground object subjected to morphological processing, and determining a straight line by using the Haff transform and combining the edge pixels;
and acquiring the direct slope, and rotating the image according to the slope of the straight line.
Preferably, the preprocessed PCB image is matched with the standard template, and when a matching condition is satisfied, determining the to-be-detected region of the preprocessed PCB image is specifically:
and determining the to-be-detected region of the preprocessed PCB image by utilizing a Sequential Similarity Detection Algorithm (SSDA).
Preferably, the extracting the component characteristics of the region to be detected includes:
and extracting the color characteristic, the shape characteristic and the texture characteristic of the component of the region to be detected.
Preferably, the extracting shape characteristics of the component of the region to be detected specifically includes:
and extracting the contour of the component by adopting an 8-neighborhood tracking algorithm.
Preferably, the extracting the texture features of the component in the region to be detected specifically includes:
and combining the gray level and gradient information of the image, and extracting the texture characteristics of the component from the gray level-gradient co-occurrence matrix.
A PCB board inspection device includes: the device comprises an acquisition unit, a preprocessing unit, a matching unit, a determining unit, an extracting unit, a classifying unit and a judging unit;
the acquisition unit is used for acquiring a PCB image of the PCB to be detected;
the preprocessing unit is used for carrying out alignment preprocessing on the PCB image acquired by the acquisition unit;
the matching unit is used for matching the PCB image preprocessed by the preprocessing unit with a pre-established standard template;
the determining unit is used for determining the to-be-detected area of the preprocessed PCB image when the matching condition is met;
the extraction unit is used for extracting the characteristics of the components in the region to be detected determined by the determination unit;
the classification unit is used for classifying and recognizing the features of the components extracted by the extraction unit by using a pre-trained Support Vector Machine (SVM) to obtain a classification recognition result;
and the judging unit is used for judging whether the specified component exists in the area to be detected or not according to the classification and identification result of the classifying unit.
Preferably, the preprocessing unit includes: the system comprises a first acquisition subunit, a first image processing subunit, a determination subunit, a second acquisition subunit and a second image processing subunit;
the first acquiring subunit is configured to acquire the PCB object from the PCB image and a pre-constructed background image by using a background subtraction method;
the first image processing subunit is configured to perform binarization on the PCB board object acquired by the first acquiring subunit to obtain a foreground object, and perform morphological processing on the foreground object;
the determining subunit is configured to determine edge pixels from the foreground object processed by the first image processing subunit, and determine a straight line by using hough transform in combination with the edge pixels;
the second obtaining subunit is configured to obtain the direct slope determined by the determining subunit;
and the second image processing subunit is used for rotating the image according to the slope of the straight line acquired by the second acquisition subunit.
A PCB board detecting system comprises the PCB board detecting device.
According to the PCB detection method, device and system, the PCB image of the PCB subjected to alignment pretreatment is matched with the pre-established standard template to determine the region to be detected, then the characteristics of the components in the region to be detected are extracted, the characteristics of the components are classified and recognized by using a pre-trained Support Vector Machine (SVM), and whether the specified components exist in the region to be detected is judged according to the classification and recognition results. The PCB detection method, the device and the system can position the detection area and judge whether the designated component exists in the area to be detected. The PCB detection method, the PCB detection device and the PCB detection system are simple to implement, high in detection efficiency and low in false detection rate, and the problems that a traditional manual detection mode is low in efficiency, high in omission factor and strong in subjectivity, and PCB detection is prone to external interference are solved.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Example one
An embodiment of the present invention provides a PCB detection method, and fig. 1 is a schematic flow chart of the method, where the method includes:
s100: and acquiring a PCB image of the PCB to be detected.
S101: and carrying out alignment pretreatment on the acquired PCB image.
S102: and matching the preprocessed PCB image with a pre-established standard template, and determining the to-be-detected area of the preprocessed PCB image when the matching condition is met.
S103: and extracting the characteristics of the components in the region to be detected.
S104: and classifying and identifying the characteristics of the components by using a pre-trained Support Vector Machine (SVM), and judging whether the specified components exist in the region to be detected or not according to a classification and identification result.
The PCB detection method provided by the embodiment of the invention can position the detection area and judge whether the designated component exists in the area to be detected. The detection method provided by the invention is simple to realize, high in detection efficiency and low in false detection rate, and solves the problems that the traditional manual detection mode is low in efficiency, high in omission ratio and strong in subjectivity, and the PCB detection is easily interfered by the outside.
Example two
The embodiment of the invention provides a PCB detection method, FIG. 2 is a flow diagram of the method, and the method can include:
s200: and acquiring a PCB image of the PCB to be detected.
S201: and carrying out alignment pretreatment on the PCB image.
When a PCB image of a PCB is shot, the shot PCB has certain inclination on the image due to various reasons, the inclination angle is possibly larger, and the alignment pretreatment of the image is to enable the edge of the PCB to be in a vertical state or a horizontal state as much as possible, namely, the edge of the PCB is consistent with the sampling state of a standard template.
In this embodiment, step S201 may include:
s201 a: and acquiring the PCB object by using the PCB image and a pre-constructed background image by using a background difference method.
Wherein, the background image can be constructed by the following modes: continuously shooting 10 images at the position where the PCB is to be placed, and then averaging the 10 shot images to obtain a background image. And (4) making difference between the PCB image and the background image constructed in the manner on gray level to obtain the PCB object.
S201 b: and binarizing the PCB object to obtain a foreground object, and performing morphological processing on the foreground object.
An image generally includes: in this embodiment, the foreground object is extracted by performing binarization processing on the image, which specifically includes: a threshold T is set to divide the data of the image into pixel groups larger than T and pixel groups smaller than T, and T =100 is set in this embodiment. After the image is binarized, the foreground object is extracted, but the foreground object still has many flaws due to the influence of noise, dust and the like, and the morphological processing is to solve the problem. Basic operations of the binary image morphological processing include erosion, expansion, opening operation, closing operation, hit and miss, skeleton extraction and the like.
Corrosion is the most basic morphological operation, and its function is to eliminate the boundary points of the object, and make the boundary shrink inwards, so as to remove the object smaller than the structural elements. Thus, by selecting the structural elements with different sizes, objects with different sizes can be removed.
The expansion is just opposite to the erosion, and is a process of expanding the boundary points of the binary object, combining all background points contacting with the object into the object and expanding the boundary to the outside. If the distance between two objects is relatively close, the dilation operation may connect the two objects together, and dilation is effective in filling up the holes in the segmented object.
The binary morphological expansion and corrosion can be converted into the logic operation of a set, the algorithm is simple, the method is suitable for parallel processing, the hardware implementation is easy, and the method is suitable for image segmentation, thinning, skeleton extraction, edge extraction and shape analysis of the binary image.
In this embodiment, a 3 × 3 structure is adopted, and the processing of the binarized PCB object is realized by performing two times of etching and two times of expansion.
S201 c: and determining edge pixels from the foreground object subjected to morphological processing, and determining a straight line by using the Haff transform and combining the edge pixels.
In this embodiment, the image alignment pre-processing is implemented by using this feature, taking into account the rectilinear nature of the PCB board edges.
S201 d: acquiring a direct slope, and rotating the image according to the slope of the straight line.
S202: and matching the preprocessed PCB image with a pre-created standard template.
S203: and judging whether the matching condition is met, if so, executing the step S104 and the subsequent steps, and otherwise, executing the step S209.
S204: and determining the area to be detected of the preprocessed PCB image.
In this embodiment, the preprocessed PCB image may be matched with a pre-created standard template through a Sequential Similarity Detection Algorithm (SSDA), that is, the positioning of the area to be detected is realized through the SSDA, specifically: selecting pixels at each position of the image subjected to alignment preprocessing in a random non-repeating sequence, accumulating the difference between the standard template and the image subjected to alignment preprocessing on the pixels, if the accumulation is larger than a set threshold value, indicating that the position is a non-matching position, stopping the operation, and testing the next position until the best matching position is found, wherein the best matching position is the area to be detected.
The basic principles of SSDA are described below:
and the SSDA sets a calculation method of the threshold T according to the adopted algorithm of the matching correlation operation, and detects the comparison relation between the currently obtained correlation result and the SSDA threshold T at a reasonable calculation interval when the matching correlation operation of each search window is carried out.
The SSDA uses ═ jj f-t | dxdy as a matching scale, and the degree of dissimilarity m (u, v) of a point (u, v) in the image f (x, y) is calculated by the following formula:
where point (u, v) represents the top left corner position of the standard template.
If there is a pattern at (u, v) that is consistent with the standard template, the value of m (u, v) is small, and conversely, m (u, v) is large. Particularly, if the standard template and the image do not match each other at all, the sum of the gray scale differences increases sharply if the absolute value of the gray scale difference from the image increases in sequence for each pixel in the standard template. Therefore, if the gradation difference exceeds a predetermined threshold value during the addition, it is considered that a pattern matching the standard template does not exist at the position, and the calculation of m (u, v) is performed by shifting to the next position. Since the calculation including m (u, v) is addition and subtraction, and most of the calculation is stopped in the middle, the matching time can be greatly shortened. In practical applications, in order to stop the calculation, the positions of the pixels may be randomly selected for the calculation of the gray scale difference. The SSDA algorithm process is as follows:
(1) define the absolute error:
wherein,
(2) taking a constant threshold Tk。
(3) In sub-diagram Si,jAnd (m, n) selecting object points. Calculating the error value between it and the corresponding point in T, then adding up the error and the difference between other points, when the accumulated r times exceeds TkIf so, stopping accumulation, recording the times r, and defining the detection curved surface of the SSDA as:
(4) the point with large value of I (I, j) is used as the matching point, because it needs many times of accumulation to make the total error exceed TkThe candidate points that are typically needed are all at such points.
The computational efficiency can be further improved for SSDA. The selection order of the N-M +1 reference points can be advanced without point by point, namely, a standard template does not necessarily translate every point, and a uniform search method combining thickness and fineness can be adopted, namely, each M point is searched for good and bad matching, and then matching is carried out on each reference point in a local range around the maximum matching value. Whether this strategy can be used without losing true match points will depend on the smoothness and unimodal nature of the surface I (I, j).
At some reference point (i, j), M under standard template coverage2The calculation sequence of the point pairs can calculate errors in a random mode irrelevant to i and j, and can also adopt a mode of adapting to image contents to select a pseudo-random sequence according to the salient features in the standard template and determine the sequence of the calculation errors so as to discard non-matching points as soon as possible.
In the embodiment, the most possible region to be detected is searched in a point-by-point matching mode in consideration of the diversity of the PCB components and the complexity of the component textures. For threshold value TkFor each template, the calculation is by:
s205: and extracting the characteristics of the components in the region to be detected. Wherein, the characteristics of components and parts can include: color features, shape features, and texture features of the components.
S206: and classifying and recognizing the characteristics of the components by using a pre-trained Support Vector Machine (SVM) to obtain a classification recognition result.
In this embodiment, a sample needs to be selected in advance to train the SVM so as to realize the classification and identification of the components, and 25 pictures are selected for each component to be detected. In order to improve the generalization capability of the SVM, the following transformation is introduced in the geometric correction: (1) stretching longitudinally by 1.1 times; (2) horizontally stretching by 1.1 times; (3) scale up is 1.1 times; (4) the dimension is reduced to 95 percent of the original image; (5) rotate 5 degrees left and right. Through the combination of the transformation, each component picture can generate 48 different original samples, the total number of the samples is 1200, and after histogram equalization is carried out on the original samples, 500 samples are randomly selected to serve as samples of each component to be detected.
After the sample is selected, the characteristics of the component need to be extracted from the sample, and in this embodiment, the characteristics of the component extraction include: and extracting color features, shape features and texture features.
The color distribution information is mainly concentrated in low-order color moments, such as first-order moments describing average color, second-order moments describing color variance, and third-order moments describing color shift, so that the characteristics of color distribution can be approximately expressed by using the low-order moments. The color moments are defined as:
wherein M is1、M2And M3Are respectively first, second and third order moments of color, qi,jIndicating the probability that the color of pixel j is i and N is the number of pixels in the image. In the RGB color space, each color channel is divided into three color moments, which together form a 9-dimensional color feature vector denoted Fcolor=[Mr1,Mr2,Mr3,Mg1,Mg2,Mg3,Mb1,Mb2,Mb3]。
The shapes of different components have obvious difference, and in order to identify the shapes of the components, the outlines of the components are extracted by adopting an 8-neighborhood tracking algorithm. And obtaining 4 absolute value characteristics of the area S, the perimeter P, the minimum external moment length L and the minimum external moment width W of the component by utilizing the contour, and respectively calculating the shape complexity S1=4 pi S/P2, the width-length ratio S2= W/L, the dispersion S3= P2/A and the roundness S4=4 pi S/L2. The 4-dimensional shape feature vector of a component can be represented as Fshape=[S1,S2,S3,S4]。
In order to solve the problem that the gray level co-occurrence matrix statistical method is influenced by image rotation when extracting texture features, the embodiment integrates image gray level and gradient information, and extracts the texture features of components from the gray level-gradient co-occurrence matrix. Let f (x, y) be the original gray image, where x =1,2 … M, y =1,2 … N, and its gray level is L. Extracting a gradient image g (x, y) of f (x, y) by using a gradient operator, wherein the gray level of the gradient image g (x, y) is LgCarrying out gray level discretization on the gradient image, wherein the gradient image after transformation is as follows:
defining the number of elements in the set { (x, y) | f (x, y) = i, G (x, y) = j } as
Hij(i=0,1,...,L-1;j=0,1,...,Lg-1), to HijCarrying out normalization processing to obtain:
from PijConstructing a gray-gradient co-occurrence matrixFrom these, 15 image texture features can be extracted, and 4 most effective typical features are selected: energy T1, correlation T2, inertia T3, and entropy T4. Wherein T1-T4 are respectively calculated by the following formula:
wherein, ThIs the mean value of the gray levels, TtIs the mean value of the gradient. In this way, 4-dimensional physical features of the image, denoted as F, can be obtainedtexture=[T1,T2,T3,T4]。
Through the analysis of the color, the shape and the gray-gradient co-occurrence matrix of the components, 17-dimensional features are extracted in total, and the feature vectors are used for training and identifying the SVM.
In this embodiment, the feature of the component in the region to be detected may be extracted in the manner described above.
Because the existing SVM is a typical two-class classifier, namely, the problem that the SVM belongs to a positive sample or a negative sample is solved, the problem that the SVM is difficult to solve in the typical multi-classification problem such as circuit board defects is solved, and in order to solve the problem, the multi-class problem is solved by using the multi-level SVM in the embodiment. Firstly, designing a first-stage SVM which mainly realizes normal or defective detection of a circuit board, and if the detection result is normal, not needing subsequent detection; if the first-level SVM detects that the circuit board has a problem, designing a second-level SVM to finish the classification of the first main component defect and other types of component defects; and repeating the steps until the nth-level SVM classifies n types of component defects. And (4) finding out a corresponding support vector after training of each level of SVM, and establishing an optimal classification hyperplane of the SVM. Since the n-level SVM is executed according to the priority level, in a specific implementation, a final result can be obtained by searching from high to low according to a binary tree, and a work flow diagram of the n-level SVM is shown in fig. 3.
S207: and judging whether the area to be detected has a specified component or not according to the identification result, if so, executing step S208, and otherwise, executing step S209.
S208: and judging that the specified component exists in the area to be detected.
S209: and judging that no specified component exists in the region to be detected.
The PCB detection method provided by the embodiment of the invention can position the area to be detected and can judge whether the area to be detected has the specified component. The detection method provided by the invention is simple to realize, high in detection efficiency and low in false detection rate, and solves the problems that the traditional manual detection mode is low in efficiency, high in omission ratio and strong in subjectivity, and the PCB detection is easily interfered by the outside.
EXAMPLE III
The third embodiment of the present invention provides a PCB detection apparatus, and fig. 4 is a schematic structural diagram of the apparatus, where the apparatus includes: an acquisition unit 100, a preprocessing unit 101, a matching unit 102, a determination unit 103, an extraction unit 104, a classification unit 105, and a determination unit 106. Wherein:
the acquiring unit 100 is configured to acquire a PCB image of a PCB to be detected. And a preprocessing unit 101, configured to perform alignment preprocessing on the PCB image acquired by the acquisition unit 100. And the matching unit 102 is configured to match the PCB image preprocessed by the preprocessing unit 101 with a pre-created standard template. And the determining unit 103 is used for determining the to-be-detected area of the preprocessed PCB image when the matching condition is met. And the extracting unit 104 is used for extracting the features of the components in the region to be detected determined by the determining unit 103. And the classification unit 105 is configured to classify and recognize the features of the component extracted by the extraction unit 104 by using a pre-trained support vector machine SVM. And a determination unit 106, configured to determine whether a specified component exists in the region to be detected according to the classification and identification result of the classification unit 105.
Further, the preprocessing unit 101 includes: the image processing device comprises a first acquisition subunit, a first image processing subunit, a determination subunit, a second acquisition subunit and a second image processing subunit. Wherein:
and the first acquisition subunit is used for acquiring the foreground image of the PCB image by using a background difference method from the PCB image and a pre-constructed background image. And the first image processing subunit is used for binarizing the foreground image of the PCB image acquired by the first acquiring subunit and performing morphological processing on the binarized foreground image. And the determining subunit is used for determining edge pixels from the foreground image processed by the first image processing subunit and determining a straight line by using the Haff transform in combination with the edge pixels. And a second obtaining subunit, configured to obtain the direct slope determined by the determining subunit. And the second image processing subunit is used for rotating the image according to the slope of the straight line acquired by the second acquisition subunit.
The PCB board detection device provided by the embodiment of the invention can position the area to be detected and can judge whether the area to be detected has the specified component or not. The PCB detection device provided by the invention is simple to realize, high in detection efficiency and low in false detection rate, and solves the problems that the traditional manual detection mode is low in efficiency, high in omission ratio and strong in subjectivity, and the PCB detection is easily interfered by the outside.
Example four
The fourth embodiment of the invention provides a PCB detection system, which comprises a PCB detection device, wherein the PCB detection device can be the PCB detection device provided by the third embodiment.
The PCB detection system provided by the fourth embodiment of the invention can position the area to be detected and can judge whether the area to be detected has the specified component or not. The PCB detection system provided by the invention is simple to realize, high in detection efficiency and low in false detection rate, and solves the problems that the traditional manual detection mode is low in efficiency, high in omission ratio and strong in subjectivity, and the PCB detection is easily interfered by the outside.
For convenience of description, the above devices are described as being divided into various units by function, and are described separately. Of course, the functions of the units may be implemented in the same software and/or hardware or in a plurality of software and/or hardware when implementing the invention.
From the above description of the embodiments, it is clear to those skilled in the art that the present invention can be implemented by software plus necessary general hardware platform. Based on such understanding, the technical solutions of the present invention may be embodied in the form of a software product, which may be stored in a storage medium, such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the method according to the embodiments or some parts of the embodiments.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for system embodiments, since they are substantially similar to method embodiments, they are described in a relatively simple manner, and reference may be made to some descriptions of method embodiments for relevant points. The above-described system embodiments are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
The invention is operational with numerous general purpose or special purpose computing system environments or configurations. For example: personal computers, server computers, hand-held or portable devices, tablet-type devices, multiprocessor systems, microprocessor-based systems, set top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments that include any of the above systems or devices, and the like.
The invention may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The invention may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media including memory storage devices.
It is noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions.
The foregoing is directed to embodiments of the present invention, and it is understood that various modifications and improvements can be made by those skilled in the art without departing from the spirit of the invention.