Tuesday, May 23, 2017

PCB Inspector with pytrack

One of my friends gently asked me to help them on automatic inspection of PCBs (xray).

I used pytrack to investigate and report soldering failures (bad bubbles). 

# -*- coding: utf-8 -*-
# 07.05.2017
# porsuk
from __future__ import division, unicode_literals, print_function  # for compatibility with Python 2 and 3
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import DataFrame, Series  # for convenience
from scipy import ndimage
from skimage import morphology, util, filters
import pims
import trackpy as tp
def crop(img):
    """
    Crop the image to select the region of interest
    """
    x_min = 20#745
    x_max = -25#850
    y_min = 50#225
    y_max = -250#300
    
    return img[y_min:y_max,x_min:x_max]
def rgb2gray(rgb):

    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray
def preprocess_pcb(img):
    img=rgb2gray(img)
    """
    Apply image processing functions to return a binary image
    """
    # Crop the pictures as for raw images.
    img = crop(img)
    print(img)
    # Apply thresholds
    img = filters.threshold_adaptive(img, 57)
    threshold = 0.9
    idx = img > img.max() * threshold
    idx2 = img < img.max() * threshold
    img[idx] = 0
    img[idx2] = 3
    img = ndimage.binary_dilation(img)
    img = ndimage.binary_dilation(img)
    return util.img_as_int(img)
frames = pims.ImageSequence('*.jpg',process_func=preprocess_pcb)
for frame in frames:
     f = tp.locate(frame, percentile=10,diameter=77, minmass= 3.0e+07,separation=80,threshold=1/30)
     print('frame = ',f['frame'][0] )
     print(f)
     print('max bubble percentage: ', 20 * max(f['size']) / 250 )
     plt.figure(f['frame'][0])  # make a new figure
     tp.annotate(f,frame,plot_style={'markersize':35});

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home