Automatic optical inspection (AOI) is a kind of visual inspection method which has been developed in recent years. It obtains images by CCD, and judges defects and faults by computer processing, analysis and comparison. The application of AOI in PCB production line has the advantages of fast detection speed, short programming time, and intelligent detection and control of images in large quantities. With the development of industrial technology, the requirements for the aperture size of PCB are more and more accurate, and the requirements for mass production of machines are higher and higher. In this paper, a project of a production digital hole machine company requires that the minimum radius of the hole position of PCB is 0.2mm, as many as 2000 circular holes on each PCB board, the total processing time is required to be no more than 10s, the center of the circle obtained after processing is accurate to 0.01 pixel level, and the deviation between each center of the circle and the center of the template is not more than 25um, If 80% of the deviation of the center of the circle exceeds this index, it is deemed as unqualified, then the equipment benchmark needs to be adjusted.
Edge refers to the set of pixels whose gray level has step change or roof like change. It exists between target and background, target and target, region and region, primitive and primitive. He is very useful for image recognition and analysis. The edge can draw the outline of the target object, which makes the observer clear at a glance and contains abundant and real information. Therefore, in order to obtain the information of hole location, edge detection and contour tracking must be carried out first. In this paper, Freeman chain code is used to track the edge contour, and chain code table and line segment table are used to store and process the data. The specific flow chart is shown in Figure 1:
1 image acquisition and preprocessing
(1) In this paper, backlight and high-resolution camera are used to obtain high contrast and high-quality images. The image resolution is 409 * 096. The quality of image source is very important, which will directly affect the effect of subsequent image processing.
(2) In order to reduce or eliminate the influence of noise as much as possible and improve the image quality, median filter is used to smooth the image. Under certain conditions, median filter can overcome the image detail blur brought by linear filter, such as least mean square filter, mean filter, etc., and it is the most effective for filtering pulse interference and image scanning noise. For the image involved in this paper is the hole detail defect of PCB board, median filter is more suitable. Its advantage is that it can eliminate noise without breaking The edge of the bad image.
(3) This method is simple, stable and effective. It is a binarization method that automatically selects the threshold value based on the minimum function and the principle of two multiplication. The idea is to segment the image histogram into two groups with a certain gray level. When the variance of the two groups is the largest, the gray level is determined as the best threshold.
2. Describe the data results of the image with chain code table and line segment table 
(1) Using Freeman chain code contour tracking Freeman chain code  is divided into eight connected and four connected chain codes. The eight connected chain code is defined by the direction of the center pixel pointing to its eight neighbors. The four connected chain code is defined by the direction of the center pixel pointing to its four neighbors, with a value of 0-3. In this paper, eight connected chain codes are used, as shown in Figure 2 below. 8-connected chain codes are rotated clockwise, and the chain code value is reduced by 1 for every 45 degrees of clockwise rotation. If the chain code value is increased by 4, the direction is reversed, and if the chain code value is increased by 8, the original direction is returned. When the chain code value is more than 8 or less than 0, the module 8 operation is used to remove or supplement 8.
(2) The application of chain code table
For the tracking of a single hole in PCB, Freeman chain code is used to get the boundary chain code of the target object. When a point is searched as the gray level of the target body, it is the boundary point of the target body. If this point is not coded, it is the starting point of a hole. Freeman chain code is used to track the contour from this point In order to track the chain code value of each point on the boundary, and according to the deviation value between the center point and the coordinates of each adjacent point, as shown in Table 1, the coordinates of each boundary point can be obtained from the coordinates of the starting point, which can be expressed as a two-dimensional array Inc  .
The specific encoding process is: set the gray level of the target object as G1, and the background gray level as G2. Search the whole PCB image. When a point a meets the gray level of G1, it is regarded as a boundary point. Start from a, search the boundary of hole position clockwise. Define the initial value as the direction of chain code value equal to 4 to search the next point. If the gray value of the next point is equal to G1, it is the next boundary point. Otherwise, rotate the search direction clockwise by 45 Degree, continue to search the point with chain code value of 3, so as to find the first point B satisfying gray value equal to G1. Then B is regarded as a new boundary point, and the end of tracking flag is 0. If no boundary point is found in the adjacent points, the end of tracking flag is 1, which means it is an isolated point.
As a new boundary point, point B takes its chain code value as the direction of the starting chain code, and repeats the above tracking clockwise until the last boundary point is point a. the tracking of a single hole bitmap is completed, and the area is marked with color filling, which represents that it has been searched, and the tracked chain code value is stored in the chain code table. In this paper, the chain code table is stored in the one-dimensional shape array code, the storage contents of subscripts 0 and 1 are coordinates x and y of the starting boundary point, the total number of chain codes n is stored in the cell with subscripts 2, the chain code value of the first boundary point and the chain code value of the second boundary point are stored at one time from the cell with subscripts 3 Chain code value up to the last nth boundary point.
3 line segment table
(1) Definition of segment table
Because the chain code table can only represent the boundary of the target body, but not the inside and outside of the boundary, the chain code table can not represent the internal pixels of the target body, but in fact, to process an image, we need to get not only the boundary points and perimeter, but also other parameters, For example, in order to process and calculate the internal pixels, we need another structure – line segment table.
The region can also be regarded as composed of horizontal line segments. Each line segment can be represented by its two endpoints. The end table that arranges all horizontal line segments in a region by scanning is called the line segment table. Each point of the line segment table is composed of two parts. The even point represents the coordinates of the left end point, and the odd point represents the coordinates of the right end point. If a structure dot is defined to represent the line segment table, and a one-dimensional array dot  is used, and I is set to represent the number of line segments, then the left end point of this line segment can be represented as dot [2 * I], and the right end point as dot [2 * I + 1].
(2) Application of line segment table
The line segment table obtains the end value of each line segment and each line segment, so it is very convenient to obtain the internal pixels of the region and other important parameters of the image through the operation. For example, the parameters needed to be calculated in this paper: calculate the area and center of gravity of the region; and A region is filled quickly (avoid repeated search and improve efficiency); the shape parameter of the region is expressed as: F = L * L / 4 π s
The purpose of this project is to detect the center of a circular hole, so it is particularly important to detect the shape of an area. In the above formula, l is the perimeter and S is the area. The shape parameter f obtained by this ratio is 1. The greater the difference between 1 and 1, the greater the difference between 1 and 1. In order to count the number and center of circular holes in PCB holes, this shape parameter can be used If it is near 1, it can be treated as a round hole approximately. Otherwise, it will be directly filled as a non round hole color mark and will not be recorded in the result.
(3) Conversion from chain code table to line segment table
Other important parameters of the image can be obtained by using line segment table representation, and the conversion from chain code table to line segment table can be in accordance with table 2.
The specific steps are as follows:
After transforming the chain code into a table, three types are obtained: middle point, type number 0; left end point, type number 1; right end point, type number 2; singular point, type number 3. According to the data in the chain code table, we can know the incoming chain code value and the outgoing chain code value, and we can determine the type of this point for the direction of the two. In the program design, the type of points can be determined according to table 3. If it is a left and right endpoint, its coordinates can be stored in a temporary array. The singular point can be regarded as a same left and right point coincidence, that is, it should be stored twice, and the middle point does not need to be stored. In the segment table, the endpoints are sorted by the size of Y coordinate, and in the same row by the size of X coordinate.
4 experimental results
In this experiment, a screenshot of PCB image of this project is used for processing, and the screenshot and processed image are shown in Figure 3.
The algorithm and program in this paper are used to process the screenshot, and the chain code table and line segment table are used to store the data. Some parameter information of PCB hole position can be obtained, as shown in Table 4:
In this paper, high quality PCB image is acquired by backlight, binary image of source image is acquired stably and simply by Ostu thresholding segmentation method, scanning and contour tracking are carried out, the principle of tracking the next point to the right is adopted, more time-saving is prepared, using chain code table and line segment table to store data and data processing, it is very convenient and accurate to obtain the perimeter, area and hole of PCB The position centroid successfully solves the positioning problem of PCB drilling. This experiment has been run and verified on the visual c + + software platform. It can detect and process 216 circular holes in 31us time, and obtain the speed and precision required by this project. The results show that this method is feasible.