The significance of a defect-free Printed Circuit Board (PCB) has broadened in today’s modern era. This is because of the successful marketing and high demand for low-cost electronic items. Such systems demand no error Printed Circuit Board. Therefore, it becomes important to detect defects and imperfections to create top-notch and zero deformity PCB. This defect detection system concentrates on finding the faults by making use of images of the PCBs.

The system is available in the form of a desktop application or GUI. It allows the users to select an image of the PCB film, which is desired as the output, followed by the image of the Printed Circuit Board that is to be inspected. The system then detects the possible shortcomings like an open circuit, breakouts, track missing, and a pin hole in the image inspected. The Image Processing technique using OpenCV is used to inspect the bare PCB images. This is done using an algorithm that can detect the defected region by using the knowledge of the structural similarity and estimating the difference ratio between the input images. This work aims to detect defects in Printed Circuit Board films and find out the affected area to avoid the defective PCBs from being used for fabrication.


In general, Printed Circuit Board imperfection recognition is performed physically by prepared individuals, hence the task may be tedious and expensive. Likewise, it prompts over-the-top piece rates and does not guarantee high quality. Therefore, it becomes important to encourage a less expensive, programmed Printed Circuit Board inspection framework. In a multidisciplinary process like PCB fabrication, etching is the most crucial part of the whole manufacturing process of a PCB. In the process of etching, the copper board generally undergoes a peeling process, where the circuit layout is preserved, while the rest of the copper background is washed out.

Sometimes, in this process, the required circuit pattern is also removed other than the unwanted copper. Therefore, it becomes very important that the inspection is done in the early stage, so that the scrap caused by the wrongly etched Printed Circuit Board panel, is minimized. Above all, functional defects and cosmetic defects stand out as the major categories of the defect in PCBs.


  • Python Programming Knowledge
  • Good knowledge in Jupyter Notebook, Tkinter, OpenCV.


Fig. 1 – Flowchart for the System Model

Fig. 2 – Identification of Submodules


Structural Similarity Index (SSIM) is an algorithm, that is used for predicting the quality of images and videos. It measures the similarity between two images, considering the original, uncompressed, and distortion-free image as the reference metric. Being a perception-based model, it incorporates various perceptual phenomena, such as luminance and contrast masking. It has been developed upon the idea that the pixels have high inter-dependencies when they are spatially close. These dependencies carry crucial data about the structure of the object, perceived visually.


The proposed technique uses different Image Processing techniques to detect the defects in the Printed Circuit Board.


It takes two images as input, one is the original, perfect Printed Circuit Board image without defects for reference, and the other one is the image that needs to be tested.

imageA = cv2.imread("11.JPG")
imageB = cv2.imread("/content/11_spur_01.jpg")


Both the images are scaled and then converted into grayscale images.

imageA = cv2.resize(imageA,(1000,500),interpolation=cv2.INTER_LINEAR)
imageB = cv2.resize(imageB,(1000,500),interpolation=cv2.INTER_LINEAR)

grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)


SSIM is calculated, by comparing both the grayscale images. If the SSIM is 1, it denotes that both the images are the same. And if the SSIM is 0, it denotes that a wrong reference image is selected for the corresponding test image. So, the SSIM should always be between 0 and 1(0<SSIM<1).

(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff*255).astype("uint8")

Fig. 3 – Output Screen with the SSIM Calculated and the total number of defects detected


The next step is to threshold the Test image and converts it into its equivalent Binary image.

thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]


Then it finds contours or continuous pixel area that different from its surrounding, from the binary image.

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)


Now different Morphological operations are applied to the Test image, to processes the image based on its shape, form, and structure. This is done to remove noise and textures from the Binary image, as it may contain various imperfections. Different morphological operations used are as follows:

  • Dilation: To expand the original image
  • Erosion: To shrink down the original image
  • Gradient: To get the difference between erosion and dilation
  • Opening: To perform erosion followed by dilation
  • Closing: To perform dilation followed by erosion


Finally, loop over the detected contours, bounded them by rectangular blocks, and the final output image is displayed on the screen.

for c in cnts:
    (x, y, w, h) = cv2.boundingRect(c)
    #cv2.rectangle(imageA, (x, y), (x+w, y+h), (0, 0, 255), 1)
    cv2.rectangle(imageB, (x, y), (x+w, y+h), (0, 0, 255), 1)

Function Flow Diagram

Fig. 4 – Function Flow Diagram Representation of the system

Data Flow Modeling Diagram

Fig. 5 – 2 Level Data Flow Model for the system

Learning Tools and Strategy

  1. OpenCV is a library that has originally been built upon C++ and is mostly preferred for Image Processing.
  2. Python is used as the programming language, to build the system upon, as it is easier to code in it as compared to C++ and as Python can easily be extended with C++, to create wrappers that can be used as Python modules, it is as fast as C++.
  3. Tkinter and easygui modules of Python have been used to create the GUI for the system.
  4. Loading images in Tkinter using Pillow module.
  5. Morphological operations in Python using OpenCV.
  6. Looping over the detected areas, with rectangular boxes.
  7. Other important Python libraries used are Pillow, scikit-image, and imutils to perform different image processing functions and Numpy.

Reflective Analysis

The work undertaken, provides an easier and promising environment that can be used by both high and low-skilled laborers, who have little to no knowledge of the field of Printed Circuit Board defect detection. Besides, it also provides a very time-convenient usage and maintenance along with high accuracy in detecting the defects. Thus, minimizing human as well as machine errors, and making valuable positive contributions towards great market products.

Conclusion and Future Work

Based on data, dependencies between the costs of early defect detection, late defect detection, and defect fixing are generally seen as expensive and time-consuming. This system of detecting defects in the Printed Circuit Board can be extended further to incorporate the knowledge of object detection algorithms so that besides detecting the defects, it can also identify the type of defect that is there. This method of detection can be extended further in various other industries to resolve the similar issues before the final implication.

The GitHub link for the code of the Printed Circuit Board Defect Detection system is available here.

Stay tuned for more such projects.

Thank you!