PCB Defects Detection with OpenCV

1.0 Abstract

The main objective of this project is to let students apply the image processing techniques that been taught in class in a given conditions based on their title. This particular project involved the detection of the defect exist in a PCB board. A Printed Circuit Board (PCB) is a circuit board consists of electronics components mounted on the surface. Basically, to produce a perfect bare PCB board, inspection of PCB is necessary to reduce defects. We apply the machine vision concept to inspect the bare PCB. We first compare a standard PCB image with a PCB image to be inspected using a simple subtraction algorithm that can detect the defected region. Our focus is to detect the defect on the PCB, such as broken track. There are so many algorithms out there to detect the defect on the PCB board.

2.0 Introduction

Nowadays, PCB manufacturing is getting more and more important for the electronics industry such as mobile phones, tablets, washing machines and prototyping boards. Hence, it is very important to have machine vision inspection of the PCB to improve the qua lity of the PCBs. As you know, in manufacturing industry, there are errors such as defects, misalignment and orientation error. Hence we need automated inspection to analysed for the errors. There are plenty of such algorithms out there now. There are 3 main processes in inspection of PCBs : defect detection, defect classification, and the location of the defect.

Of course, automated inspection system is better than human inspection in which subjectivity, fatigue, slowness and high cost is involved. The technology is moving very fast and hence a fast and efficient inspection system is required to make sure 100% quality PCBs are produced. These conclude that automated systems are preferred in manufacturing industry nowadays for higher productivity.

In my case study, I am going to investigate about how to detect a broken track on the PCB. This project required us to only use the images captured by the laboratory camera or our own digital camera devices. In this project, Visual Studio C++ 2010 was used as the main compiler with linked libraries to the OpenCV image processing library.

defect pcb

3.0 Methodology

A. Inspection Flow Chart

pcb inspection flow chart

B. Image Subtraction Operation

Well, this is the simplest algorithm that we can find to detect the difference between the two images, i.e. to detect the defect on the PCB. In other word, image subtraction is performed in order to get the difference between two images. In this algorithm, we need to use two images – reference image and the inspected image.

In this method, we compare both the images pixel-by-pixel using XOR logic operator. After performing the operation, we will get the resulting image that consist of the defects. The subtract operation produces either negative or positive pixel value. Therefore the outcome of this operation is divided into negative image and positive image.

Image Subtraction Operation

The overview of the XOR/Subtraction operation process is shown below:

overview of the XOR Subtraction

To perform the image subtraction operation, it is required that both images has same size in terms of pixels. The logical XOR operation will show us the defect in inspected image as compared with reference image.

C. OpenCV Functions Used

opencv used functions

4.0 Results And Discussion

A. Top Lighting

top lighting

Top and 2 Sides Lighting

B. Top and 2 Sides Lighting

Top and 2 Sides Lighting 2

Top and 2 Sides Lighting 3

As you can see from the result, we first load the RGB image, both reference and defected image. Next we will then grayscale both image. The reason for differentiating such images from any other sort of colour image is that less information needs to be provided for each pixel. In fact a `gray’ colour is one in which the red, green and blue components all have equal intensity in RGB space, and so it is only necessary to specify a single intensity value for each pixel, as opposed to the three intensities needed to specify each pixel in a full colour image.

Next, we convert the grayscale image to binary image that consist only binary pixel ‘1’ or ‘0’ for easy operation process later on.

Now, after we get both the binary image, we perform the XOR operation to detect the defected region on the image.

If there is no defect detected, the window will show as below:

no defects detected

Comparison between two different lighting

As you can see from the result of two different lighting A) Top Lighting and B) Top and 2 Sides Lighting, the result is kind deviate a little under different lighting with constant threshold value of 138. The result for B) are subjected to more noise. However, both the type of lighting are able to detect the defect and show it at the final result.

5.0 Limitations

This image subtraction algorithm actually has a lot of disadvantages and limitations :

  • It only can detect for the broken track but cannot detect like short circuit, missing components.
  • The images size must be the same (i.e. the reference image and defected image must have same size)
  • Both the inspected image and reference image must have the same orientation.

6.0 Further Improvement

This project can be further improved by upgrading defect detection algorithm to suit a variety of lighting conditions. Besides, we could improve the algorithm to detect different kind of defects such as missing components or short circuits. Improvement can be made on stating the location for the defect and calculate the size of the defect.

7.0 Conclusion

In conclusion, we manage to complete the project with a great success in testing and coding for the defect detection on the bare PCB. Indeed, throughout this project, we have learnt how to use the OpenCV library in Visual Studio C++ 2010. We were also exposed to the effects of different kind of lighting condition which could affect the performance of the algorithm.

This project enable us to apply the knowledge and theory that we learnt in class to solve real life problems.

8.0 Reference

1. Convert RGB to Black & White in OpenCV –
2. Load, Display and Save Image –
3. Detection of Bare PCB Defects by Image Subtraction Method using Machine Vision –

9.0 Appendix

using namespace cv;
int main ( int argc, char **argv )
   Mat dst;
   Mat im_gray;
   Mat im_gray_1;
   //Mat im_gray = imread("PCB_no_defect.tif",CV_LOAD_IMAGE_GRAYSCALE);
   Mat im_rgb = imread("PCB_no_defect.tif");
   //Mat im_gray_1 = imread("PCB_defect.tif",CV_LOAD_IMAGE_GRAYSCALE);
   Mat im_rgb_1 = imread("PCB_defect.tif");
   equalizeHist( im_gray, dst );
   Mat img_bw = im_gray > 138;
   Mat img_bw_1 = im_gray_1 > 138;
   Mat result = img_bw_1-img_bw;
   namedWindow( "Display window1", WINDOW_AUTOSIZE ); // Create a window for display.
   imshow( "Display window1", result);  // Show 
   namedWindow( "Display window2", WINDOW_AUTOSIZE ); // Create a window for display.
   imshow( "Display window2",img_bw );  // Show our image inside it.
   namedWindow( "Display window3", WINDOW_AUTOSIZE );// Create a window for display.
   imshow( "Display window3", img_bw_1); 
   imwrite("im_gray.jpg", im_gray);
   imwrite("im_gray_1.jpg", im_gray_1);
   imwrite("im_bw.jpg", img_bw);
   imwrite("im_bw_1.jpg", img_bw_1);
   waitKey(0);  // Wait for a keystroke in the window
   return 0;
return 0;


Join the conversation!

Error! Please fill all fields.
Looking for the latest from TI?