Method and Device for Effectively Utilizing OpenCV to Detect PCB Board Size

: In the process of PCB board production, it is necessary to accurately measure the contour dimensions of the PCB board and the dimensions of various types of wires, holes, or slots on the PCB board to avoid a large number of defective products flowing into the subsequent production process. In existing technology, the measurement of the dimensions of wires, holes, or slots on the PCB board mostly relies on manual work, which has the problems of low measurement efficiency and high measurement error rate, Unable to meet the efficient and high-quality production needs of PCB boards. This article proposes a method and device for effectively utilizing OpenCv to detect PCB board size. This device is based on an optical image measurement system, combined with the template matching method in OpenCv, and can automatically complete forming inspection and size measurement by scanning multiple PCB boards by one time.This device can complete measurement evaluation, report generation, and SPC data analysis for multiple PCB boards, achieving high automation and significantly improving measurement accuracy system.


Introduction
In 2022, the global shipment volume of smartphones is approximately 1.31 billion units, tablet computers are 162.1 million units, and wearable devices are approximately 515.6 million units. These devices are constantly developing towards miniaturization and functional diversification, so their PCBs are developing towards multi-layer, thin wire and fine hole, high-frequency and high-speed directions, with higher requirements for quality and full size inspection. From the perspective of shipping volume over the years, the production scale is increasing. Therefore, traditional detection and measurement methods are difficult to meet the requirements of measurement efficiency.
Traditional detection and measurement methods include manual detection or manual operation of measuring instruments. Manual operation has the problems of low measurement efficiency and high measurement error rate, which cannot meet the efficient and high-quality production needs of circuit boards. The problems and defects of manually operated measuring instruments include the need to make fixtures or fixtures to fix the measured object, which can only measure one PCB board at one time. Additionally, technical workers need to select different line widths, circle radii, and other measuring tools in the software to depict and measure the image of the components to be tested, which is inefficient.
In response to the above issues, this article proposes an efficient and accurate PCB size measurement instrument, which is based on an optical image measurement system and combines high-precision image analysis methods to achieve automatic completion of forming inspection and size measurement in one scan.

The Development Status of Dimensional Testing Methods
Xue Ping [1] proposed an accurate ball detection method based on bearing image, which is used to avoid unqualified bearings caused by assembly errors due to size in the aerospace industry. This method uses a ball segmentation model based on DeepLab v3 network to segment local balls, and adds CBAM to the Xception network of the original network. Based on the characteristics of segmentation results, a circle detection algorithm based on circle fitting evaluation is proposed for incomplete short arc segments. After experimental verification, this method can perform well in the bearing assembly industry, but it is not suitable for measuring the size of PCB boards with more precision, complexity, multiple shapes, and the need to measure multiple irregular shapes.
Tay Y. H. [2] proposed a machine vision system for automatic box size detection in 3D to support intelligent packaging simulators. The system adopts a single camera and a square grid printing platform. The box size detection algorithm is based on the platform area location, and uses Bilateral filter, binarization and morphological image transformation to extract grid features. To measure the size of the box, the length, width, and height of the box are detected by counting the reference squares. Further calculate the volume of the box based on the obtained three-dimensional values. Then, the performance of the algorithm is evaluated by calculating the error between the true values obtained from manual measurements. The experimental results show that the average accuracy of box size detection is 94.3%, but it is also not suitable for the field of PCB board size measurement. Chen Jiaxu [3] used image processing methods such as image matching and feature extraction to extract contour features. Establish a measurement coordinate system based on the waist groove. The coordinate conversion relationship is determined by the calibration of the visual inspection system. Thus, the geometric relationship of the arc groove relative to the measurement coordinate system is obtained. The geometric dimensions of the safety seat base were measured. The experimental results show that the relative error in measuring the waist groove width is 2.7%, and the relative error in measuring the length is 2.1%. Li Kui et al. [4] proposed a machine vision based method for detecting surface defects and dimensions of graphite sealing rings in order to solve the problem of low efficiency in size detection and reduce the influence of human factors. On the one hand, image preprocessing and Canny algorithm are used to extract the edges of graphite sealing rings, and then the least squares circle fitting method is used to complete the edge fitting, thereby calculating the inner and outer diameters of the sealing rings. On the other hand, template matching algorithm is used to identify surface defects of sealing rings. The experimental results show that this method can detect defects in graphite sealing rings with a defect recognition rate of 85.7%. The size measurement accuracy can reach 0.06 mm, meeting the measurement accuracy requirements. The measurement accuracy of the above two methods is not suitable for PCB boards, but some of their ideas can be used for reference.

The Application Status of Template Matching Algorithms
In the literature on PCB defect detection, a relatively simple and fast template matching detection scheme is generally used for the positioning of the target object to be tested [5] .
Visual based matching positioning technology mainly refers to combining a smaller image with a larger image Partial matching [6] .Specifically, it means finding a known target in the search area that has the same size as the template, and determining its position and angle through a certain algorithm [7] . Due to the different matching factors used in matching templates and targets, template matching is generally divided into two types at home and abroad, namely based on grayscale values and image features [8] . Template matching based on grayscale information is to calculate and compare the grayscale values in the source image and template image to obtain matching results. Template matching based on image features utilizes local features [9] of colors, boundaries, or shapes in the image to match and obtain the target position.
Template matching is often used in the field of defect detection. For example, Tu Zimei [10] proposed using template matching to locate the components to be tested when implementing a machine vision based printed circuit board detection and sorting system. They then conducted grayscale range detection on the located components, and detected defect types based on the grayscale range, achieving the detection of PCB component defects.
There is currently little literature on the application of template matching in the field of PCB board size detection. Given the excellent performance of template matching in target localization, this article intends to apply the template matching method to the field of PCB board size detection. In order to address the shortcomings of existing technologies in measuring PCB board size mentioned in the introduction, this article proposes an effective device for using OpenCv to detect PCB board size. The structural diagram of the device [11] is shown in Figure 1. The hardware part includes a frame 10, a glass platform 20, an image collector 30, a light source module 40, a driving mechanism 50, an adjustment mechanism 60, a conveying mechanism 70, and a cover plate mechanism 80. Before the device works, the attitude of the image collector relative to the driving mechanism is adjusted by adjusting the mechanism to align the center position of the image collector with the center position illuminated by the lower light source, thereby improving the measurement accuracy of subsequent measurement operations. At the same time, the backend system integrates and analyzes the collected image information to obtain the outline of the circuit board, as well as the size information of lines, holes, or slots on the circuit board.

Methods for locating various shapes to be measured
This article experimented with directly using the HoughCircles function of OpenCv's Hough transform algorithm to measure the size of circular and semicircular holes or components, and also experimented with directly using the HoughLinesP function of OpenCv's probability Hough transform algorithm to measure the length of straight lines, but the results were not very ideal. Therefore, this article changes the method. First, creating a calibration module in the software that performs calibration operations on various types of standard PCB boards to be tested. During the calibration operation, it is necessary to select each area of the PCB board to be tested in the software for template image storage, and set the pixel and millimeter (or micrometer) values of the template image length, width, distance, or radius to be saved, all template images and calibration data that need to be saved are associated with the PCB board model and saved. As long as the calibration has been done once, subsequent testing of PCB boards of the same model can be carried out based on the template matching of the area to be tested collected during calibration. After matching to the corresponding area, the number of pixels in length, width, distance, or radius can be measured. Based on the conversion relationship between the number of pixels obtained during calibration and the millimeter (or micrometer) value, the millimeter (or micrometer) value of various sizes can be calculated.
Among them, in the calibration module, you can choose to select template images for the original PCB board image, or you can choose to select template images for the binary grayscale image of the PCB board. The binary grayscale image is obtained by processing the original image using OpenCv's binary processing function Threshold. As shown in Figure 2, it is a color original image of the light source projected onto the PCB board above the PCB board. As shown in Figure 3, it is an image original image collected from the back projected light source under the glass board onto the PCB board. As shown in Figure 4, it is a grayscale image processed by OpenCv on Figure  3. In the automatic detection module, the software can choose which model of PCB board to start batch detection, and then query the template images and calibration data of that model from the database, and sequentially match the templates based on each template image. The template matching uses OpenCv's template matching function MatchTemplate. As shown in Figure 5, the white rectangular boxes are the positioning areas obtained by template matching the appearance, gold fingers, and card slots of the PCB board in Figure 2. As shown in Figure 6, each white rectangular box is the positioning map obtained by template matching the semicircular holes of the PCB board in Figure 4.

Method for calculating the dimensions of the shape to be measured
After locating the shape to be tested based on template matching, the size type to be calculated based on the current template number recorded in the database, such as length, width, radius, area, or angle, is obtained by first obtaining the number of pixels corresponding to the shape area to be tested, and then multiplying it by the number of millimeters (or micrometers) represented by 1 pixel during calibration. For example, to calculate the radius of a semicircle, the template image selects an area that exactly contains the semicircle feature, as shown in Figure 7. Assuming y0 represents the left vertex y-coordinate of the shape area (rectangle) to be measured, it is only necessary to search for white pixel values from the bottom up from the axis of the shape area to be measured. If the y-axis coordinate is y1, and the next three consecutive pixel values upwards are all white pixels, the radius R of the semicircle hole is calculated as follows: R = (y1-y0)*m In the above equation, m represents the number of millimeters for 1-pixel calibration. For example, to calculate the height and width of the gold finger area on a PCB board, if the template image selects the entire gold finger area, the horizontal number of pixels in the matched gold finger area * 1 pixel calibration millimeter is the length of the gold finger area, and the vertical number of pixels * 1 pixel calibration millimeter is the width of the gold finger area.

Experiment and result analysis
The experimental environment of this article is based on the Windows 10 operating system, with a memory of 128GB. A C # desktop application was implemented on VS2019, with a CPU model of Pentium G2120. Figure 8 shows the software interface developed in this article. In this section, a series of experiments were conducted to validate the algorithm using real PCB board images. Figure  9 shows some of the experimental results.

Conclusion
The method and device described in this article can efficiently detect the overall length, width, aperture (including circular holes, square holes, and irregular holes), distance from hole to hole, distance from hole to line, slot width, slot length, arc, angle, etc. of multiple PCB boards within one minute. It can also measure the length, width, distance from pad to pad, distance from pad to line, distance from pad to hole, size of pad, position of pad centerline, line width on board, line length, etc. Dimensions such as line distance, length and width of gold fingers, spacing between gold fingers, distance from gold fingers to edges, and distance from gold fingers to holes. The methods and devices described in this article make it possible to achieve efficient automated detection of PCB boards, which can keep up with the PCB board processing accuracy measurement of electronic products such as mobile phones, tablets, and wearable devices produced on a large scale in the market. The application prospects are good and there are relatively high economic benefits.