def bright_lines(features): im = features['im_no_fingers'] h, w = im.shape if 'finger_period_row' in features: rh = int(round(features['finger_period_row'])) cw = int(round(features['finger_period_col'])) else: rh = int(round(features['finger_period'])) cw = int(round(features['finger_period'])) f_v = im - np.maximum(np.roll(im, shift=2 * cw, axis=1), np.roll(im, shift=-2 * cw, axis=1)) pixel_ops.ApplyThresholdLT_F32(f_v, f_v, 0.0, 0.0) # filter mask = (f_v > 0.02).astype(np.uint8) min_size = 0.0005 * h * w ip.remove_small_ccs(mask, min_size) f_v[mask == 0] = 0 # features['_f_v'] = f_v.copy() f_h = im - np.maximum(np.roll(im, shift=2 * rh, axis=0), np.roll(im, shift=-2 * rh, axis=0)) pixel_ops.ApplyThresholdLT_F32(f_h, f_h, 0.0, 0.0) # filter mask = (f_h > 0.02).astype(np.uint8) min_size = 0.0005 * h * w ip.remove_small_ccs(mask, min_size) f_h[mask == 0] = 0 # features['_f_h'] = f_h.copy() # normalize f_h /= 0.3 f_v /= 0.3 pixel_ops.ClipImage(f_h, 0.0, 1.0) pixel_ops.ClipImage(f_v, 0.0, 1.0) features['ov_lines_horizontal_u8'] = (f_h * 255).astype(np.uint8) features['ov_lines_vertical_u8'] = (f_v * 255).astype(np.uint8) features['bright_lines_horizontal'] = f_h.mean() * 100 features['bright_lines_vertical'] = f_v.mean() * 100 if False: view = ImageViewer(im) ImageViewer(f_v) ImageViewer(f_h) view.show() sys.exit()
def dark_spots(features): im = features['im_no_fingers'] h, w = im.shape im_mini = im[::6, ::6] im_mini_med = cv2.medianBlur(im_mini, ksize=5) im_mini_smooth = cv2.GaussianBlur(im_mini_med, ksize=(0, 0), sigmaX=1) background = cv2.resize(im_mini_smooth, (w, h)) dark_areas = background - im pixel_ops.ApplyThresholdLT_F32(dark_areas, dark_areas, 0.0, 0.0) foreground_mask = ((features['bl_cropped_u8'] == 0) | (features['bl_cropped_u8'] == 4)) structure = ndimage.generate_binary_structure(2, 1) foreground_mask = ndimage.binary_erosion(foreground_mask, structure=structure, iterations=3) dark_areas[~foreground_mask] = 0 DARK_SPOT_SENSITIVITY = 0.08 dark_spots = (dark_areas > DARK_SPOT_SENSITIVITY).astype(np.uint8) min_size = int(h * w * 0.0001) ip.remove_small_ccs(dark_spots, min_size) dark_spots_outline = ndimage.binary_dilation( dark_spots, structure=structure, iterations=2) - dark_spots features['mk_dark_spots_filled_u8'] = dark_spots features['mk_dark_spots_outline_u8'] = dark_spots_outline if False: view = ImageViewer(im) ImageViewer(background) ImageViewer(dark_spots) ImageViewer(ip.overlay_mask(im, dark_spots_outline)) view.show()
def robust_dislocations(smooth, impure, features): c = parameters.ROBUST_CROP smooth = np.ascontiguousarray(smooth[c:-c, c:-c]) impure = np.ascontiguousarray(impure[c:-c, c:-c]) struct = ndimage.generate_binary_structure(2, 1) # robust dislocation mask dog1 = (cv2.dilate( smooth, cv2.getStructuringElement( cv2.MORPH_ELLIPSE, (parameters.DOG_STRUCT_SIZE, parameters.DOG_STRUCT_SIZE))) - smooth) dog2 = (ip.fast_smooth(smooth, sigma=parameters.DOG_SIGMA2) - cv2.GaussianBlur(smooth, (0, 0), parameters.DOG_SIGMA1, borderType=cv2.BORDER_REPLICATE)) dog = dog1 + dog2 IMP_THRESH = 0.4 pixel_ops.ApplyThresholdLT_F32(impure, dog, IMP_THRESH, 0) if False: view = ImageViewer(dog1) ImageViewer(dog2) ImageViewer(dog1 + dog2) ImageViewer(dog) view.show() defect_mask = np.zeros_like(dog, np.uint8) DOG_THRESH = parameters.BLOCK_DISLOCATION_THRESH pixel_ops.ApplyThresholdGT_F32_U8(dog, defect_mask, DOG_THRESH, 1) num_pure_pixels = pixel_ops.CountThresholdGT_F32(impure, IMP_THRESH) defect_robust = (pixel_ops.CountEqual_U8(defect_mask, 1)) / float(num_pure_pixels) # compute surface area eroded = defect_mask - cv2.erode(defect_mask, struct.astype(np.uint8)) defect_pixels = float(pixel_ops.CountEqual_U8(defect_mask, 1)) if defect_pixels > 0: defect_surface = pixel_ops.CountEqual_U8(eroded, 1) / defect_pixels else: defect_surface = 0 if False: print defect_robust, defect_surface view = ImageViewer(smooth) ImageViewer(defect_mask) ImageViewer(eroded) view.show() sys.exit() features['defect_robust_area_fraction'] = defect_robust features['defect_surface'] = defect_surface
def dark_spots(features): im = features['im_no_fingers'] # shrink to standard size h, w = 300, 300 im_small = cv2.resize(im, (h, w)) dark_areas = np.zeros_like(im_small) pixel_ops.DarkSpots(im_small, dark_areas, 8) candidates = (dark_areas > parameters.DARK_SPOT_MIN_STRENGTH).astype(np.uint8) ip.remove_small_ccs(candidates, parameters.DARK_SPOT_MIN_SIZE) candidates = cv2.resize(candidates, (im.shape[1], im.shape[0])) candidates[features['mask_busbar_filled']] = 0 dark_spots_outline = ndimage.binary_dilation(candidates, iterations=3).astype(np.uint8) - \ ndimage.binary_dilation(candidates, iterations=1).astype(np.uint8) features['mk_dark_spots_outline_u8'] = dark_spots_outline features['mk_dark_spots_filled_u8'] = candidates features['dark_spots_area_fraction'] = candidates.mean() dark_areas_no_noise = dark_areas - parameters.DARK_SPOT_MIN_STRENGTH pixel_ops.ApplyThresholdLT_F32(dark_areas_no_noise, dark_areas_no_noise, 0.0, 0.0) features['dark_spots_strength'] = dark_areas_no_noise.mean() * 10000 features['dark_spots_count'] = ip.connected_components(candidates)[1] if False: print features['dark_spots_area_fraction'] print features['dark_spots_strength'] print features['dark_spots_count'] rgb = ip.overlay_mask(im, dark_spots_outline) view = ImageViewer(rgb) ImageViewer(dark_areas) ImageViewer(dark_areas_no_noise) ImageViewer(candidates) view.show()
def efficiency_analysis(features): im = features['im_no_fingers'] im_peaks = features['im_norm'][features['_peak_row_nums'], :] bbs = features['_busbar_cols'] # make sure no zero values im_peaks = im_peaks.copy() pixel_ops.ApplyThresholdLT_F32(im_peaks, im_peaks, 0.01, 0.01) im = im.copy() pixel_ops.ApplyThresholdLT_F32(im, im, 0.01, 0.01) # IDEA: # two defect mask: # 1. existing one (highlights anything dark) # 2. do a dark line (local mins). mask out & interpolate. # this one won't pick up grain boundaries # - slider blends between them ################# # DEFECT MASK 1 # ################# # - very sensitive to all dark areas # need to interpolate values along edges/busbars so defects in these regions can be found xs = np.arange(im_peaks.shape[0]) cols = [features['cell_edge_left'], features['cell_edge_right']] cols += list(np.where(features['mask_busbar_edges'][0, :])[0]) for c in cols: ys = im_peaks[:, c].copy() ys[ys > features['_bright_area_thresh']] = features['_bright_area_thresh'] params_op = optimize.fmin_powell(line_error, [0.0, ys.mean()], disp=0, args=(xs, ys), xtol=0.05, ftol=0.05) vals = xs * params_op[0] + params_op[1] im_peaks[:, c] = vals if False: print features['_bright_area_thresh'] mask = np.zeros_like(im, np.uint8) mask[:, c] = 1 ImageViewer(ip.overlay_mask(im, mask)) plt.figure() plt.plot(ys) plt.plot(vals) plt.show() # max of actual val and: interpolate vertical line at: # - left & right edge # - left & right of each BB # make monotonic (don't care about local mins) bb_mono = between_bb_mono(im_peaks, bbs) background1 = bb_mono background1 = cv2.GaussianBlur(background1, ksize=(0, 0), sigmaX=3, borderType=cv2.BORDER_REPLICATE) # relative difference foreground1 = background1 / im_peaks foreground1 -= 1 pixel_ops.ClipImage(foreground1, 0, 3.0) foreground1[:, features['mask_busbar_filled'][0, :]] = 0 # expand to full size full_size = np.zeros(im.shape, np.float32) pixel_ops.ExpandFingers(full_size, foreground1, features['_peak_row_nums']) foreground1 = full_size background_full = np.zeros(im.shape, np.float32) pixel_ops.ExpandFingers(background_full, background1, features['_peak_row_nums']) if False: view = ImageViewer(im) ImageViewer(foreground1) ImageViewer(background1) view.show() ################# # DEFECT MASK 2 # ################# # - less likely to include dark grains # - mask out local mins and areas of high gradient, and the interpolate background flatten_cols = np.r_[im.mean(axis=0)] im_flattened = im - flatten_cols flatten_rows = np.c_[im_flattened.mean(axis=1)] im_flattened -= flatten_rows im_flattened[features['mask_busbar_filled']] = 0 im_smoothed = cv2.GaussianBlur(im_flattened, ksize=(0, 0), sigmaX=2) # local mins dark_lines = np.zeros_like(im_flattened, np.uint8) pixel_ops.LocalMins(im_smoothed, dark_lines) s = ndimage.generate_binary_structure(2, 1) dark_lines = ndimage.binary_dilation(dark_lines, structure=s) # high gradient edgesH = cv2.Sobel(im_smoothed, cv2.CV_32F, 1, 0) edgesV = cv2.Sobel(im_smoothed, cv2.CV_32F, 0, 1) edges = cv2.magnitude(edgesH, edgesV) # combine defect_candidates = ((edges > 0.2) | dark_lines) if False: view = ImageViewer(im_flattened) ImageViewer(ip.overlay_mask(im_flattened, dark_lines)) ImageViewer(ip.overlay_mask(im_flattened, edges > 0.2)) ImageViewer(ip.overlay_mask(im_flattened, defect_candidates)) view.show() sys.exit() background2 = interpolate_background(im_flattened, defect_candidates.astype(np.uint8)) background2 += flatten_rows background2 += flatten_cols # relative difference foreground2 = background2 / im foreground2 -= 1 pixel_ops.ClipImage(foreground2, 0, 3.0) foreground2[features['mask_busbar_filled']] = 0 if False: view = ImageViewer(im) ImageViewer(background2) ImageViewer(foreground2) view.show() sys.exit() if False: features['_defect1'] = foreground1 features['_defect2'] = foreground2 # discrimination function x1, y1 = 0.0, 0.5 x2, y2 = 0.4, 0.0 foreground = (-1 * ((y2 - y1) * foreground2 - (x2 - x1) * foreground1 + x2 * y1 - y2 * x1) / np.sqrt((y2 - y1)**2 + (x2 - x1)**2)) foreground += 0.1 + parameters.CELL_DISLOCATION_SENSITIVITY foreground *= 0.25 pixel_ops.ClipImage(foreground, 0, 1) features['ov_dislocations_u8'] = (foreground * 255).astype(np.uint8) if False: view = ImageViewer(im) ImageViewer(foreground1) ImageViewer(foreground2) ImageViewer(foreground) #ImageViewer(background_full) # ImageViewer(foreground > 0.2) view.show() sys.exit() ################ # IMPURE AREAS # ################ def FindImpure(imp_profile, imp_profile_r, defects, debug=False): if False: ImageViewer(im) plt.figure() plt.plot(imp_profile) plt.plot(defects) plt.show() imp_ratio = (imp_profile / imp_profile_r).astype(np.float32) imp_ratio[imp_ratio > 0.9] = 0.9 imp_ratio /= 0.9 global_min = np.argmin(imp_ratio) local_mins = np.where((imp_ratio < np.roll(imp_ratio, 1)) & (imp_ratio < np.roll(imp_ratio, -1)))[0] # make monotonic from global minimum imp_mono = imp_ratio.copy() flip = False if global_min > len(imp_mono) // 2: flip = True imp_mono = imp_mono[::-1] global_min = np.argmin(imp_mono) rest = np.ascontiguousarray(imp_mono[global_min:]) rest_mono = np.empty_like(rest) pixel_ops.MakeMonotonic(rest, rest_mono) imp_mono[global_min:] = rest_mono # if edge_dist < 0.075: # imp_mono[:global_min] = imp_ratio[global_min] if flip: imp_mono = imp_mono[::-1] global_min = np.argmin(imp_mono) # for a real impure area, the global minimum should be close to an edge edge_dist = min(global_min, len(imp_ratio) - global_min) / float(len(imp_ratio)) edge_spot = int(0.03 * len(imp_mono)) imp_at_edge = min(imp_mono[edge_spot], imp_mono[-edge_spot]) imp_1d = np.ones_like(imp_mono) # check the defect content in the "impure" area if (imp_mono < 1.0).sum() == 0: defect_strength = 0 else: # defect_strength = defects[imp_mono < 1.0].mean() defect_strength = np.median(defects[imp_mono < 1.0]) if (edge_dist < 0.075 and # lowest point close to edge imp_at_edge < 0.8 and # edge is dark defect_strength < 0.1 and # not too many defects # (global_min == local_mins[0] or global_min == local_mins[-1]) and # no local mins closer to edge (imp_mono - imp_ratio).mean() < 0.035): # no large non-impure dips imp_1d = imp_mono.copy() imp_1d -= 0.3 if global_min < len(imp_profile) // 2: imp_1d[:global_min] = imp_1d[global_min] else: imp_1d[global_min:] = imp_1d[global_min] if debug: print edge_dist, edge_dist < 0.075 print imp_at_edge, imp_at_edge < 0.8 print defect_strength, defect_strength < 0.1 # print global_min, local_mins[0], (global_min == local_mins[0] or global_min == local_mins[-1]) print(imp_mono - imp_ratio).mean(), (imp_mono - imp_ratio).mean() < 0.035 ImageViewer(im) plt.figure() plt.plot(imp_profile, 'r') plt.plot(imp_mono, 'g') plt.plot(imp_ratio, 'b') # plt.plot((0, len(signal_var)), (0.1, 0.1)) plt.plot(defects, 'c') plt.plot(imp_1d, 'c') plt.show() # find impure edge width THRESH = 0.5 if imp_1d[0] < THRESH: edge_width = np.where(imp_1d < THRESH)[0][-1] / float( len(imp_profile)) left = True elif imp_1d[-1] < THRESH: edge_width = (len(imp_profile) - np.where(imp_1d < THRESH)[0][0]) / float( len(imp_profile)) left = False else: edge_width = 0 left = True return imp_1d, edge_width, left if False: # ignore defect areas defect_mask = foreground > 0.2 mx = ma.masked_array(im, mask=defect_mask) if False: view = ImageViewer(im) ImageViewer(defect_mask) view.show() ''' # left/right edges cols = np.apply_along_axis(stats.scoreatpercentile, 0, im, per=75) cols[cols > 1.0] = 1.0 imp_profile = ndimage.gaussian_filter1d(cols, sigma=3, mode="nearest") imp_profile_r = imp_profile[::-1] if len(bbs) >= 2: mid = (bbs[0] + bbs[-1]) // 2 else: mid = im.shape[1] // 2 imp_profile_r = np.roll(imp_profile_r, mid - (len(imp_profile) // 2)) col_defect = foreground.mean(axis=0) imp_h, ew_h, e_left = FindImpure(imp_profile, imp_profile_r, col_defect, debug=False) ''' # top/bottom edges rows = np.apply_along_axis(stats.scoreatpercentile, 1, im, per=75) rows[rows > 1.0] = 1.0 imp_profile = ndimage.gaussian_filter1d(rows, sigma=3, mode="nearest") imp_profile_r = imp_profile[::-1] row_defect = foreground.mean(axis=1) imp_v, ew_v, e_top = FindImpure(imp_profile, imp_profile_r, row_defect, debug=False) features['impure_edge_width'] = ew_v if e_top: features['impure_edge_side'] = 0 else: features['impure_edge_side'] = 2 impure = np.ones_like(im) impure[:, :] = np.c_[imp_v] if False: print features['impure_edge_width'], features['impure_edge_side'] view = ImageViewer(impure) view.show() sys.exit() imp_cutoff = 0.55 pixel_ops.ApplyThresholdGT_F32(impure, impure, imp_cutoff, imp_cutoff) impure /= imp_cutoff impure_overlay = np.log10(2 - impure) if False: plt.figure() plt.plot(impure[:, 0], 'r') plt.plot(np.log(impure[:, 0] + 1), 'g') plt.plot(np.log10(impure[:, 0] + 1), 'b') plt.show() view = ImageViewer(impure) view.show() pixel_ops.ClipImage(impure_overlay, 0, 1) features['ov_impure2_u8'] = (impure_overlay * 255).astype(np.uint8) ########### # METRICS # ########### num_pixels = im.shape[0] * im.shape[1] # impure impure_thresh = 0.9 # 0.8 dislocation_thresh = 0.1 num_impure_pixels = pixel_ops.CountThresholdLT_F32(impure, impure_thresh) features['impure_area_fraction'] = (num_impure_pixels / float(num_pixels)) if features['impure_area_fraction'] > 0.001: pure_mask = (impure > impure_thresh) & (foreground < dislocation_thresh) features['impure_strength'] = (im[pure_mask].mean() / im[impure < impure_thresh].mean()) - 1 else: features['impure_strength'] = 0 features['impure_area_fraction'] = 0 features['impure_strength2'] = features['impure_strength'] * features[ 'impure_area_fraction'] * 100 # dislocations num_dislocations = pixel_ops.CountThresholdGT_F32(foreground, dislocation_thresh) features['dislocation_area_fraction'] = (num_dislocations / float(num_pixels)) features['_foreground'] = foreground features['_dislocation_thresh'] = dislocation_thresh features['_impure'] = impure features['_impure_thresh'] = impure_thresh # find the average distance between dislocation pixels ys, xs = np.where(foreground > dislocation_thresh) points = np.c_[ys, xs] num_samples = 1500 if len(ys) > 0: points = points[::max(1, points.shape[0] // num_samples), :] pixel_dists = distance.pdist(points) avg_dist = np.mean(pixel_dists) avg_dist = (avg_dist - 0.3 * im.shape[0]) / (0.5 * im.shape[0]) else: avg_dist = 0 features['dislocation_density'] = 1.0 - avg_dist if len(ys) > 0: dis_vals = foreground[ys, xs] dislocation_strength = math.sqrt((dis_vals**2).mean()) features['dislocation_severity_A'] = dislocation_strength # create a second defect strength. ratio2 = background_full / im ratio2 = np.clip(ratio2, 1, 5) ratio2[:, features['mask_busbar_filled'][0, :]] = 1 features['dislocation_severity_B'] = ratio2[ys, xs].mean() if False: view = ImageViewer(background_full) ImageViewer(im) ImageViewer(ratio2) view.show() else: features['dislocation_severity_A'] = 0 features['dislocation_severity_B'] = 0 if False: view = ImageViewer(im) foreground[foreground < 0] = 0 ImageViewer(foreground) ImageViewer(background_full) view.show() sys.exit() # dislocation histogram features foreground_bins = np.zeros((5), np.float32) pixel_ops.BackgrounHistogram(foreground, foreground_bins) foreground_bins = (foreground_bins / num_pixels) * 100 features['dislocation_hist_01'] = foreground_bins[0] features['dislocation_hist_02'] = foreground_bins[1] features['dislocation_hist_03'] = foreground_bins[2] features['dislocation_hist_04'] = foreground_bins[3] features['dislocation_hist_05'] = foreground_bins[4] features['dislocation_strength'] = features[ 'dislocation_area_fraction'] * features['dislocation_severity_A'] if False: # print features['foreground_hist_01'], features['foreground_hist_02'], features['foreground_hist_03'], # print features['foreground_hist_04'], features['foreground_hist_05'] view = ImageViewer(im) ImageViewer(foreground) ImageViewer(impure) ImageViewer(create_overlay(features)) view.show()
def plir(im_sp, im_lp, im_pl, features, spline_plir, spline_plc): t_start = timeit.default_timer() pixel_ops.ApplyThresholdLT_F32(im_sp, im_sp, 1.0, 1.0) pixel_ops.ApplyThresholdLT_F32(im_lp, im_lp, 1.0, 1.0) pixel_ops.ApplyThresholdLT_F32(im_pl, im_pl, 1.0, 1.0) if im_sp.shape != im_lp.shape: print im_sp.shape, im_lp.shape assert False im_sp = im_sp.astype(np.float64) im_lp = im_lp.astype(np.float64) im_pl = im_pl.astype(np.float64) if False: view = ImageViewer(im_sp) ImageViewer(im_lp) ImageViewer(im_pl) view.show() sys.exit() # vertical registration c = np.argmax(im_sp.mean(axis=0)) profile_sp = im_sp[:, c - 10:c + 11].mean(axis=1) profile_lp = im_lp[:, c - 10:c + 11].mean(axis=1) shift_v = register(profile_sp, profile_lp, debug=False) if False: print c, shift_v view = ImageViewer(im_lp) ImageViewer(im_sp) ImageViewer(np.roll(im_sp, shift=shift_v, axis=0)) view.show() sys.exit() im_sp = np.roll(im_sp, shift=shift_v, axis=0) # compute plir (ratio of LP to SP) plir = im_lp / im_sp if False: t = stats.scoreatpercentile(plir, per=90) print plir.min(), t, plir.max() plir[plir > t] = t view = ImageViewer(im_lp) ImageViewer(im_sp) ImageViewer(plir) view.show() sys.exit() # Get cropping and rotation parameters (based on short pass) vals = im_sp[::2, ::2].flat vals = np.sort(vals) min_val = vals[int(0.025 * vals.shape[0])] max_val = vals[int(0.975 * vals.shape[0])] features['norm_range'] = max_val - min_val features['norm_lower'] = min_val im_normed_temp = (im_sp - min_val) / (max_val - min_val) rotated_temp = block_rotate(im_normed_temp, features) block_crop(rotated_temp, features) rotation = features['crop_rotation'] x1, x2, y1, y2 = features['_crop_bounds'] if False: cropped_temp = rotated_temp[y1:y2, x1:x2] view = ImageViewer(im_sp) ImageViewer(rotated_temp) ImageViewer(cropped_temp) view.show() if 'input_param_skip_features' in features and int( features['input_param_skip_features']) == 1: return True # rotate all images if abs(rotation) > 0.01: h, w = plir.shape rot_mat = cv2.getRotationMatrix2D((w // 2, h // 2), rotation, 1.0) plir_rotated = cv2.warpAffine(plir, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) sp_rotated = cv2.warpAffine(im_sp, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) lp_rotated = cv2.warpAffine(im_lp, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) h, w = im_pl.shape rot_mat = cv2.getRotationMatrix2D((w // 2, h // 2), rotation, 1.0) nf_rotated = cv2.warpAffine(im_pl, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) else: plir_rotated = plir sp_rotated = im_sp lp_rotated = im_lp nf_rotated = im_pl # find marker location features['marker_loc'] = find_marker(sp_rotated[y1:y2, x1:x2]) nf_to_sp_ratio = nf_rotated.shape[1] / float(sp_rotated.shape[1]) features['marker_loc'] *= nf_to_sp_ratio # crop plir image cropped_plir = plir_rotated[y1:y2, x1:x2] cropped_plir = np.ascontiguousarray(cropped_plir) cropped_sp = sp_rotated[y1:y2, x1:x2] cropped_lp = lp_rotated[y1:y2, x1:x2] if False: _, upper = ip.get_percentile(cropped_plir, 0.005) pixel_ops.ClipImageF64(cropped_plir, 0, upper) print cropped_plir.min(), cropped_plir.dtype view = ImageViewer(plir) ImageViewer(cropped_plir) ImageViewer(cropped_sp) view.show() sys.exit() # convert plir image to bulk image tau_bulk_plir = interpolate.splev(cropped_plir.flatten(), spline_plir).reshape(cropped_plir.shape) _, upper = ip.get_percentile(tau_bulk_plir, 0.0001) pixel_ops.ClipImageF64(tau_bulk_plir, 0.1, upper) if False: ImageViewer(im_sp) ImageViewer(im_lp) plt.figure() plt.imshow(cropped_plir) plt.colorbar() plt.figure() plt.imshow(tau_bulk_plir) plt.colorbar() plt.show() sys.exit() # zoom bulk image to make the same size as NF if im_pl.shape != im_sp.shape: size_ratio_h = im_pl.shape[0] / float(im_sp.shape[0]) size_ratio_w = im_pl.shape[1] / float(im_sp.shape[1]) x1 = int(round(x1 * size_ratio_w)) x2 = int(round(x2 * size_ratio_w)) y1 = int(round(y1 * size_ratio_h)) y2 = int(round(y2 * size_ratio_h)) # correct and crop plir image (using params from short pass) cropped_nf = nf_rotated[y1:y2, x1:x2] # upsize low res bulk tau_bulk_plir = ndimage.zoom(tau_bulk_plir, zoom=2.0, order=1) # make sure same size height = min(tau_bulk_plir.shape[0], cropped_nf.shape[0]) width = min(tau_bulk_plir.shape[1], cropped_nf.shape[1]) tau_bulk_plir = tau_bulk_plir[:height, :width] cropped_nf = cropped_nf[:height, :width] assert tau_bulk_plir.shape == cropped_nf.shape else: cropped_nf = nf_rotated[y1:y2, x1:x2] if False: view = ImageViewer(tau_bulk_plir) ImageViewer(cropped_nf) view.show() sys.exit() if parameters.PLIR_INTERPOLATE_MARKER_WIDTH > 0 and features[ 'marker_loc'] > 0: # interpolate marker print features['marker_loc'] locs = np.array([int(round(features['marker_loc']))], np.int32) cropped_nf = np.ascontiguousarray(cropped_nf, np.float32) pixel_ops.InterpolateBBs(cropped_nf, locs, parameters.PLIR_INTERPOLATE_MARKER_WIDTH) tau_bulk_plir = np.ascontiguousarray(tau_bulk_plir, np.float32) pixel_ops.InterpolateBBs(tau_bulk_plir, locs, parameters.PLIR_INTERPOLATE_MARKER_WIDTH) if False: view = ImageViewer(cropped_nf) ImageViewer(tau_bulk_plir) view.show() # correct for doping and transfer to bulk c_vals = fit_c_vals(cropped_nf, tau_bulk_plir, spline_plc) doping = ndimage.gaussian_filter1d(c_vals, sigma=2, mode="reflect") if False: ImageViewer(cropped_nf) plt.figure() plt.plot(c_vals) plt.plot(doping) plt.show() nf_dope = cropped_nf * np.r_[doping] nf_dope[nf_dope > spline_plc[0][-1]] = spline_plc[0][-1] tau_bulk_nf = interpolate.splev(nf_dope.flatten(), spline_plc).astype( np.float32).reshape(cropped_nf.shape) _, upper_p = ip.get_percentile(tau_bulk_nf, 0.0001) pixel_ops.ClipImage(tau_bulk_nf, 0.1, upper_p) features['_C_vals'] = doping.astype(np.float32) features['im_tau_bulk_f32'] = tau_bulk_nf features['im_tau_bulk_u8'] = (ip.scale_image(tau_bulk_nf) * 255).astype( np.uint8) features['im_cropped_nf_u8'] = (ip.scale_image(cropped_nf) * 255).astype( np.uint8) features['im_cropped_nf_u16'] = np.round(cropped_nf).astype(np.uint16) features['im_cropped_sp_u16'] = np.round(cropped_sp).astype(np.uint16) features['im_cropped_lp_u16'] = np.round(cropped_lp).astype(np.uint16) if False: print tau_bulk_nf.min(), tau_bulk_nf.max() _, upper_p = ip.get_percentile(tau_bulk_nf, 0.001) pixel_ops.ClipImage(tau_bulk_nf, 0.1, upper_p) # print interpolate.splev([0.0, 0.0245], spline_plc) #ImageViewer(cropped_nf * np.r_[doping]) #ImageViewer(tau_bulk_nf) plt.figure() plt.plot(tau_bulk_plir.mean(axis=0)) plt.plot(tau_bulk_nf.mean(axis=0)) # plt.figure() # plt.hist(tau_bulk_full.flat, bins=100) if False: plt.figure() plt.plot(doping) plt.plot(c_vals) plt.figure() pl = np.mean(cropped_nf, axis=0) plt.plot(pl, label="PL") plt.legend() plt.show() # compute runtime t_stop = timeit.default_timer() features['runtime'] = t_stop - t_start return True
def plir2(im_sp, im_lp, features, spline_plir, spline_sp): pixel_ops.ApplyThresholdLT_F32(im_sp, im_sp, 1.0, 1.0) pixel_ops.ApplyThresholdLT_F32(im_lp, im_lp, 1.0, 1.0) if im_sp.shape != im_lp.shape: print im_sp.shape, im_lp.shape assert False im_sp = im_sp.astype(np.float64) im_lp = im_lp.astype(np.float64) if False: view = ImageViewer(im_sp) ImageViewer(im_lp) view.show() sys.exit() # register short and long pass images def register(signal1, signal2, debug=False): bandpass1 = ndimage.gaussian_filter1d( signal1, sigma=10) - ndimage.gaussian_filter1d(signal1, sigma=3) bandpass2 = ndimage.gaussian_filter1d( signal2, sigma=10) - ndimage.gaussian_filter1d(signal2, sigma=3) offsets = range(-10, 11) fits = [(np.roll(bandpass1, shift=s) * bandpass2).mean() for s in offsets] optimal_shift = offsets[np.argmax(fits)] if debug: plt.figure() plt.plot(signal1) plt.plot(signal2) plt.figure() plt.plot(offsets, fits) plt.figure() plt.plot(np.roll(bandpass1, shift=optimal_shift)) plt.plot(bandpass2) plt.show() return optimal_shift c = np.argmax(im_sp.mean(axis=0)) profile_sp = im_sp[:, c - 10:c + 11].mean(axis=1) profile_lp = im_lp[:, c - 10:c + 11].mean(axis=1) shift_v = register(profile_sp, profile_lp, debug=False) if False: print c, shift_v view = ImageViewer(im_lp) ImageViewer(im_sp) ImageViewer(np.roll(im_sp, shift=shift_v, axis=0)) view.show() sys.exit() im_sp = np.roll(im_sp, shift=shift_v, axis=0) # compute PL (ratio of LP to SP) plir = im_lp / im_sp if False: t = stats.scoreatpercentile(plir, per=99) print plir.min(), t, plir.max() plir[plir > t] = t view = ImageViewer(im_lp) ImageViewer(im_sp) ImageViewer(plir) view.show() sys.exit() # Get cropping and rotation parameters (based on short pass) # normalisation - find the 99th percentile vals = im_sp[::4, ::4].flat vals = np.sort(vals) min_val = vals[int(0.025 * vals.shape[0])] max_val = vals[int(0.975 * vals.shape[0])] features['norm_range'] = max_val - min_val features['norm_lower'] = min_val im_normed_temp = (im_sp - min_val) / (max_val - min_val) rotated_temp = block_rotate(im_normed_temp, features) # get crop bounds crop block_crop(rotated_temp, features) rotation = features['crop_rotation'] x1, x2, y1, y2 = features['_crop_bounds'] if 'input_param_skip_features' in features and int( features['input_param_skip_features']) == 1: return True if False: cropped_temp = rotated_temp[y1:y2, x1:x2] view = ImageViewer(im_sp) ImageViewer(rotated_temp) ImageViewer(cropped_temp) view.show() # correct and crop plir image (using params from short pass) if abs(rotation) > 0.01: h, w = plir.shape rot_mat = cv2.getRotationMatrix2D((w // 2, h // 2), rotation, 1.0) plir_rotated = cv2.warpAffine(plir, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) sp_rotated = cv2.warpAffine(im_sp, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) lp_rotated = cv2.warpAffine(im_lp, rot_mat, (w, h), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REPLICATE) else: plir_rotated = plir sp_rotated = im_sp lp_rotated = im_lp features['marker_loc'] = find_marker(sp_rotated[y1:y2, x1:x2]) plir_cropped = plir_rotated[y1:y2, x1:x2] plir_cropped = np.ascontiguousarray(plir_cropped) # continue with plir _, upper = ip.get_percentile(plir_cropped, 0.005) pixel_ops.ClipImageF64(plir_cropped, 0, upper) if False: _, upper = ip.get_percentile(plir, 0.005) pixel_ops.ClipImageF64(plir, 0, upper) print plir_cropped.min(), plir_cropped.dtype view = ImageViewer(plir) ImageViewer(plir_cropped) view.show() sys.exit() tau_bulk_plir = interpolate.splev(plir_cropped.flatten(), spline_plir).reshape(plir_cropped.shape) _, upper = ip.get_percentile(tau_bulk_plir, 0.0001) pixel_ops.ClipImageF64(tau_bulk_plir, 0.1, upper) if False: ImageViewer(im_sp) ImageViewer(im_lp) plt.figure() plt.imshow(plir_cropped, cmap=cmaps.viridis) plt.colorbar() plt.figure() plt.imshow(tau_bulk_plir, cmap=cmaps.viridis) plt.colorbar() plt.show() sys.exit() # dislocation and impure processing for PL image cropped_sp = sp_rotated[y1:y2, x1:x2] cropped_lp = lp_rotated[y1:y2, x1:x2] # compute c-values c_vals = fit_c_vals(cropped_sp, tau_bulk_plir, spline_sp) doping = ndimage.gaussian_filter1d(c_vals, sigma=2, mode="reflect") if False: ImageViewer(cropped_sp) plt.figure() plt.plot(c_vals) plt.plot(doping) plt.show() features['_C_vals'] = doping.astype(np.float32) sp_dope = cropped_sp * np.r_[doping] sp_dope[sp_dope > spline_sp[0][-1]] = spline_sp[0][-1] tau_bulk_full = interpolate.splev(sp_dope.flatten(), spline_sp).astype( np.float32).reshape(cropped_sp.shape) # pixel_ops.ApplyThresholdLT_F32(tau_bulk_full, tau_bulk_full, 0.1, 0.1) _, upper_p = ip.get_percentile(tau_bulk_full, 0.0001) pixel_ops.ClipImage(tau_bulk_full, 0.1, upper_p) features['im_tau_bulk_f32'] = tau_bulk_full features['im_tau_bulk_u8'] = (ip.scale_image(tau_bulk_full) * 255).astype( np.uint8) features['im_cropped_sp_u8'] = (ip.scale_image(cropped_sp) * 255).astype( np.uint8) features['im_cropped_sp_u16'] = np.round(cropped_sp).astype(np.uint16) features['im_cropped_lp_u16'] = np.round(cropped_lp).astype(np.uint16) if False: if True: _, upper_p = ip.get_percentile(tau_bulk_full, 0.001) pixel_ops.ClipImage(tau_bulk_full, 0.1, upper_p) else: tau_bulk_full = np.log(tau_bulk_full) ImageViewer(cropped_sp) ImageViewer(sp_dope) ImageViewer(tau_bulk_plir) ImageViewer(tau_bulk_full) plt.figure() plt.plot(tau_bulk_plir.mean(axis=0)) plt.plot(tau_bulk_full.mean(axis=0)) if False: plt.figure() plt.plot(doping) plt.plot(c_vals) plt.show() return True
def feature_extraction(im, features, crop=True, skip_features=False): h, w = im.shape if im.dtype != np.float32: im = im.astype(np.float32) if crop: # cropping rotation_corrected = block_rotate(im, features) cropped_u16 = block_crop(rotation_corrected, features) bounds = features['_crop_bounds'] else: rotation_corrected = im cropped_u16 = im bounds = (0, w - 1, 0, h - 1) features['_crop_bounds'] = bounds features['crop_rotation'] = 0 # get original coordinates of the block corners find_corners(im, features) features['_rotation_corrected'] = rotation_corrected if False: view = ImageViewer(im) ImageViewer(cropped_u16) view.show() sys.exit() # normalisation vals = cropped_u16[::2, ::2].flat vals = np.sort(vals) min_val = vals[int(0.01 * vals.shape[0])] max_val = vals[int(0.99 * vals.shape[0])] features['norm_range'] = max_val - min_val features['norm_lower'] = min_val im_normed = (cropped_u16 - min_val) / (max_val - min_val) pixel_ops.ApplyThresholdLT_F32(im_normed, im_normed, 0.0, 0.0) cropped = im_normed croped_u8 = im_normed.copy() pixel_ops.ApplyThresholdGT_F32(croped_u8, croped_u8, 1.0, 1.0) features['im_cropped_u8'] = (croped_u8 * 255).astype(np.uint8) features['im_cropped_u16'] = cropped_u16.astype(np.uint16) if skip_features or ('input_param_skip_features' in features and int(features['input_param_skip_features']) == 1): return if False: view = ImageViewer(im) ImageViewer(cropped, vmin=0, vmax=1) view.show() sys.exit() # compute some row/column percentiles col_sorted = np.sort(cropped[::4, :], axis=0) features['_col_90'] = np.ascontiguousarray( col_sorted[int(round(0.9 * 0.25 * cropped.shape[0])), :]) features['_col_60'] = np.ascontiguousarray( col_sorted[int(round(0.6 * 0.25 * cropped.shape[0])), :]) row_sorted = np.sort(cropped[:, ::4], axis=1) features['_row_90'] = np.ascontiguousarray( row_sorted[:, int(round(0.9 * 0.25 * cropped.shape[1]))]) # background background = block_background(cropped, features) # foreground foreground = block_foreground(cropped, features) # normalise background background /= background.max() # calculate metrics robust_dislocations(cropped, background, features) # dislocation area DIS_THRESH = 0.3 dislocation_area = ( pixel_ops.CountThresholdGT_F32(foreground, DIS_THRESH) / float(foreground.shape[0] * foreground.shape[1])) impure_area = 1 - (pixel_ops.CountThresholdGT_F32(background, 0.5) / float(foreground.shape[0] * foreground.shape[1])) # edge width l4 = background.shape[1] // 4 profile = background[:, l4:-l4].mean(axis=1) fg = np.where(profile > parameters.BRICK_EDGE_THRESH)[0] if len(fg) > 0: left_width, right = fg[[0, -1]] right_width = len(profile) - right - 1 edge_width = max(left_width, right_width) if edge_width < 0.05 * len(profile): edge_width = 0 else: edge_width = 100 features['edge_width'] = edge_width if False: print edge_width ImageViewer(cropped) plt.figure() plt.plot(profile) plt.show() if False: dislocations = np.zeros_like(foreground, dtype=np.uint8) pixel_ops.ApplyThresholdGT_F32_U8(foreground, dislocations, DIS_THRESH, 1) print features['defect_robust_area_fraction'], impure_area view = ImageViewer(im) ImageViewer(dislocations) ImageViewer(foreground) ImageViewer(background, vmin=0, vmax=1) view.show() # sys.exit() imp_cutoff = 0.55 pixel_ops.ApplyThresholdGT_F32(background, background, imp_cutoff, imp_cutoff) background /= imp_cutoff background = np.log10(2 - background) dis_cutoff = 0.1 foreground -= dis_cutoff foreground = np.clip(foreground, 0, 1) foreground *= 0.5 features['ov_impure_u8'] = (background * 255).astype(np.uint8) features['ov_defects_u8'] = (foreground * 255).astype(np.uint8) features['_bounds'] = bounds pixel_ops.ClipImage(im_normed, 0, 1) features['dislocation_area_fraction'] = dislocation_area features['impure_area_fraction'] = impure_area return features
def block_foreground(im, features): per_col = features['_col_60'] im_col = np.dot(np.ones((im.shape[0], 1), np.float32), per_col.reshape(1, per_col.shape[0])) per_row = features['_row_90'] im_row = np.dot(per_row.reshape(im.shape[0], 1), np.ones((1, im.shape[1]), np.float32)) background = ip.fast_smooth(np.minimum(im_col, im_row), sigma=5) foreground = background - im pixel_ops.ApplyThresholdLT_F32(foreground, foreground, 0, 0) pixel_ops.ApplyThresholdLT_F32(background, foreground, 0.3, 0) if False: view = ImageViewer(im, vmin=0, vmax=1) ImageViewer(im_col, vmin=0, vmax=1) ImageViewer(im_row, vmin=0, vmax=1) ImageViewer(background, vmin=0, vmax=1) ImageViewer(foreground, vmin=0, vmax=1) view.show() sys.exit() # skeletonized version of defects local_mins = np.zeros_like(foreground, np.uint8) f = cv2.GaussianBlur(foreground * -1, ksize=(0, 0), sigmaX=2) pixel_ops.LocalMins(f, local_mins) dis = ((local_mins == 1) & (foreground > 0.1)).astype(np.uint8) ys, xs = np.where(dis) pixel_ops.FastThin(dis, ys.copy(), xs.copy(), ip.thinning_lut) ip.remove_small_ccs(dis, 10) if False: crossing = np.zeros_like(dis) pixel_ops.ComputeCrossings(dis, crossing) junctions = crossing > 2 struct = ndimage.generate_binary_structure(2, 2) junctions_d = ndimage.binary_dilation(junctions, struct) branches = dis.copy() branches[junctions_d] = 0 # find branches that touch an end point ccs, num_ccs = ip.connected_components(branches) spurs = np.zeros_like(dis) for cc in set(ccs[crossing == 1]): if cc == 0: continue spurs[ccs == cc] = 1 # sys.exit() remove = spurs.copy() ip.remove_small_ccs(remove, 10) removed = spurs - remove pruned = dis - removed crossing = np.zeros_like(dis) pruned = pruned.astype(np.uint8) pixel_ops.ComputeCrossings(pruned, crossing) pruned[crossing == 1] = 0 dis = pruned rgb = ip.overlay_mask(im, dis, colour='b') ip.save_image("brick_lines_skeleton.png", dis) ip.save_image("brick_lines_overlay.png", rgb) view = ImageViewer(foreground, vmin=0, vmax=1) ImageViewer(rgb) view.show() sys.exit() # create a height-based profile of dislocation levels # - crop impure areas at top and bottom, imp = np.where(per_row < 0.5)[0] mid = len(per_row) // 2 upper_half = imp[imp < mid] if len(upper_half) > 0: top = upper_half.max() else: top = 0 lower_half = imp[imp > mid] if len(lower_half) > 0: bottom = lower_half.min() else: bottom = len(per_row) if False: plt.figure() plt.plot(per_row) plt.vlines([top, bottom], ymin=0, ymax=1) plt.show() sys.exit() foreground_pure = foreground[top:bottom, :] dislocation_profile = foreground_pure.mean(axis=0) dislocation_profile[per_col < 0.6] = 0 dislocation_profile = ndimage.gaussian_filter1d(dislocation_profile, sigma=5) features['_dis_avg_height'] = dislocation_profile if False: view = ImageViewer(im, vmin=0, vmax=1) # ImageViewer(im_col, scale=0.5, vmin=0, vmax=1) # ImageViewer(im_row, scale=0.5, vmin=0, vmax=1) ImageViewer(background, vmin=0, vmax=1) ImageViewer(foreground, vmin=0, vmax=1) ImageViewer(foreground_pure, vmin=0, vmax=1) plt.figure() plt.plot(dislocation_profile) view.show() sys.exit() return foreground
def firing_defects(features): im = features['im_norm'] finger_rows = features['_finger_row_nums'] # im_fingers = features['_finger_im'] im_smooth = cv2.GaussianBlur(im, ksize=(0, 0), sigmaX=1) im_fingers = im_smooth[finger_rows, :] if False: view = ImageViewer(im) # view = ImageViewer(im_smooth) ImageViewer(im_fingers) view.show() sys.exit() # find depth of local minimums S = 5 dips = np.minimum(np.roll(im_fingers, S, axis=1), np.roll(im_fingers, -S, axis=1)) - im_fingers pixel_ops.ApplyThresholdLT_F32(dips, dips, 0, 0) dips[:, features['mask_busbar_filled'][0, :]] = 0 dips[:, :features['cell_edge_left']] = 0 dips[:, features['cell_edge_right']:] = 0 # TODO: add upper bound as well locs = ((im_fingers < np.roll(im_fingers, 1, axis=1)) & (im_fingers < np.roll(im_fingers, -1, axis=1)) & (dips > 0.02) & (dips < 0.05)).astype(np.float32) # count num dips in a region. ignore areas with only 1 w = im.shape[1] // 10 weights = np.ones(w, np.int32) local_count = ndimage.convolve1d(locs, weights, axis=1, mode="constant") dips_filtered = dips.copy() dips_filtered[local_count <= 1] = 0 if False: view = ImageViewer(im) ImageViewer(dips) # ImageViewer(locs) # ImageViewer(local_count) # ImageViewer(dips_filtered) view.show() im_dots = cv2.GaussianBlur(dips_filtered, ksize=(0, 0), sigmaX=10, sigmaY=2) # im_dots -= 0.001 if False: view = ImageViewer(dips_filtered) view = ImageViewer(im_dots) view.show() splotch = np.empty_like(im) pixel_ops.ExpandFingers(splotch, im_dots, finger_rows) splotch = ip.fast_smooth(splotch, sigma=10) features["_firing"] = splotch.copy() splotch *= 100.0 * (1.0 + parameters.FIRING_SENSITIVITY) pixel_ops.ClipImage(splotch, 0.0, 1.0) pixel_ops.ApplyThresholdGT_F32(features['im_center_dist_im'], splotch, features['wafer_radius'], 0) features["ov_splotches_u8"] = (splotch * 255).astype(np.uint8) # metrics if splotch.max() >= 0.2: features['firing_area_strength'] = splotch.mean() * 100 mask_firing = (splotch > 0.2).astype(np.uint8) im_pl = features['_cropped_f32'] firingPL = pixel_ops.MaskMean_F32(im_pl, mask_firing, 1) goodPL = pixel_ops.MaskMean_F32(im_pl, mask_firing, 0) features['firing_area_mean_PL'] = firingPL features['firing_area_fraction'] = mask_firing.mean() features['firing_area_PL_intensity_ratio'] = firingPL / max(1, goodPL) else: features['firing_area_strength'] = 0.0 features['firing_area_mean_PL'] = 0.0 features['firing_area_fraction'] = 0.0 features['firing_area_PL_intensity_ratio'] = 0.0 if False: print features['firing_area_strength'] view = ImageViewer(im) ImageViewer(splotch, vmin=0, vmax=1) ImageViewer(mask_firing) f2 = {'im_cropped_u8': features['im_cropped_u8'], 'ov_splotches_u8': features['ov_splotches_u8']} ImageViewer(create_overlay(f2)) view.show() sys.exit()
def dark_areas(features): # dark areas (not dark spots) im = features['im_norm'] h, w = im.shape row_nums = features['_peak_row_nums'] cell_mask = features['bl_cropped_u8'][row_nums, :] bb_locs = features['_busbar_cols'] # fill edges & corners foreground = im[row_nums].copy() edge = features['cell_edge_tb'] rr, cc = draw.circle_perimeter(features['wafer_middle_y'], features['wafer_middle_x'], int(round(features['wafer_radius'])) - edge) mask = (cc >= 0) & (cc < w) & np.in1d(rr, row_nums) lut = np.zeros(h, np.int32) lut[row_nums] = np.arange(len(row_nums)) rr = rr[mask] cc = cc[mask] rr = np.take(lut, rr) pixel_ops.FillCorners(foreground, rr.astype(np.int32), cc.astype(np.int32)) foreground[:, :edge] = np.c_[foreground[:, edge]] foreground[:, -edge:] = np.c_[foreground[:, -edge]] # create background # 1. make monotonic between edges & BBs mono_lr = np.empty_like(foreground) mono_rl = np.empty_like(foreground) pixel_ops.MakeMonotonicBBs(foreground, mono_lr, bb_locs) pixel_ops.MakeMonotonicBBs(np.ascontiguousarray(foreground[:, ::-1]), mono_rl, np.ascontiguousarray(w - bb_locs[::-1])) mono_rl = mono_rl[:, ::-1] mono = np.minimum(mono_lr, mono_rl) background = mono da1 = background - foreground # fill BBs and flatten background2 = background.copy() pixel_ops.InterpolateBBs(background2, features['_busbar_cols'], features['busbar_width']) cols = background2.mean(axis=0) background2 -= np.r_[cols] rows = background2.mean(axis=1) background2 -= np.c_[rows] da2 = -1 * background2 pixel_ops.ApplyThresholdLT_F32(da2, da2, 0.0, 0.0) if False: view = ImageViewer(foreground) ImageViewer(background) ImageViewer(background2) ImageViewer(da1) ImageViewer(da2) ImageViewer(da1 + da2) # plt.figure() # for x in range(0, background.shape[1], 50): # plt.plot(background[:, x], label="Col %d"%(x)) # plt.legend() view.show() sys.exit() dark_areas = da1 + da2 dark_areas -= (0.1 - parameters.DARK_AREA_SENSITIVITY) dark_areas[(cell_mask == 1) | (cell_mask == 8)] = 0 # corners pixel_ops.ClipImage(dark_areas, 0.0, 1.0) dark_areas_full = np.empty_like(im) pixel_ops.ExpandFingers(dark_areas_full, dark_areas, features['_peak_row_nums']) if False: pixel_ops.ApplyThresholdGT_F32(features['im_center_dist_im'], dark_areas_full, features['wafer_radius'], 0) print features['wafer_radius'] view = ImageViewer(dark_areas) ImageViewer(dark_areas_full) ImageViewer(features['im_center_dist_im']) view.show() dark_areas_full = cv2.GaussianBlur(dark_areas_full, ksize=(0, 0), sigmaX=1) # metrics mask_dark = (dark_areas_full > 0.2).astype(np.uint8) features['dark_area_strength'] = dark_areas_full.mean() * 100 im_pl = features['_cropped_f32'] darkPL = pixel_ops.MaskMean_F32(im_pl, mask_dark, 1) brightPL = pixel_ops.MaskMean_F32(im_pl, mask_dark, 0) features['dark_area_mean_PL'] = darkPL features['dark_area_fraction'] = mask_dark.mean() features['dark_area_PL_intensity_ratio'] = brightPL / max(1, darkPL) features['ov_dark_areas_u8'] = (dark_areas_full * 255).astype(np.uint8) if False: print features['dark_area_fraction'], features['dark_area_strength'] # plt.figure() # plt.plot(cols) # plt.plot(cols_mono) view = ImageViewer(im) ImageViewer(foreground) ImageViewer(dark_areas) ImageViewer(mask_dark) ImageViewer(dark_areas_full) view.show()
def finger_defects(features): im = features['im_norm'] h, w = im.shape row_nums = features['_finger_row_nums'] finger_im = im[row_nums, :] # .copy() mask = features['bl_cropped_u8'][row_nums, :] bb_locs = features['_busbar_cols'] # make a little more robust by averaging 3 locations: # - finger, and a little above/below offset = 1 # int(features['finger_period'] / 2.0) off_up = im[row_nums - offset, :] off_down = im[row_nums + offset, :] finger_im = (finger_im + off_up + off_down) / 3.0 features['_finger_im'] = finger_im if False: view = ImageViewer(im) ImageViewer(finger_im) # ImageViewer(finger_im2) view.show() # make monotonic (don't care about local mins) mono_lr = np.empty_like(finger_im) mono_rl = np.empty_like(finger_im) pixel_ops.MakeMonotonicBBs(finger_im, mono_lr, bb_locs) pixel_ops.MakeMonotonicBBs(np.ascontiguousarray(finger_im[:, ::-1]), mono_rl, np.ascontiguousarray(w - bb_locs[::-1])) mono_rl = mono_rl[:, ::-1] mono = np.minimum(mono_lr, mono_rl) if False: view = ImageViewer(im) # ImageViewer(normed) # ImageViewer(mono_lr) # ImageViewer(mono_rl) ImageViewer(mono) view.show() #################### # BROKEN FINGERS 1 # #################### # Detect intensity changes along a finger # - works best away from edges, and can handle breaks aligned in a column # 1. at a break, the min on bright side should be greater than max on dark side # 2. sharp local gradient, not a region with a steady (but high) gradient finger_filled = mono.copy() # 1 - brighter areas s = 25 offset = s // 2 + 3 maxs = ndimage.maximum_filter(finger_filled, size=(1, s), mode="nearest") pixel_ops.ApplyThresholdLT_F32(maxs, maxs, 0.1, 0.1) mins = ndimage.minimum_filter(finger_filled, size=(1, s), mode="nearest") d1 = np.roll(mins, offset, axis=1) / np.roll(maxs, -offset, axis=1) d2 = np.roll(mins, -offset, axis=1) / np.roll(maxs, offset, axis=1) break_strength = np.maximum(d1, d2) # 2 - sharp local drop f = np.array([[-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]], dtype=np.float32) edges_abs = np.abs(ndimage.correlate(mono, weights=f, mode="nearest")) edges_local = edges_abs - np.maximum(np.roll(edges_abs, 6, axis=1), np.roll(edges_abs, -6, axis=1)) break_strength += edges_local features['_edges_abs'] = edges_abs if False: print parameters.BROKEN_FINGER_THRESHOLD1 print parameters.BROKEN_FINGER_EDGE_THRESHOLD view = ImageViewer(finger_filled) # ImageViewer(maxs) # ImageViewer(mins) # ImageViewer(d1) # ImageViewer(d2) # ImageViewer(edges_local) ImageViewer(break_strength) view.show() sys.exit() # find local maxes breaks1 = ((break_strength >= np.roll(break_strength, 1, axis=1)) & (break_strength >= np.roll(break_strength, -1, axis=1)) & (break_strength > parameters.BROKEN_FINGER_THRESHOLD1)).astype(np.uint8) # For this detector, ignore breaks near edges, corners & busbars mask_background = mask == 8 # edges mask2 = ndimage.binary_dilation(mask_background, np.ones((1, s + 4), np.uint8)) mask3 = ndimage.binary_dilation(mask2, np.ones((3, 1), np.uint8)) corners = mask2.sum(axis=0) > 2 mask2[:, corners] = mask3[:, corners] bb_mask = features['mask_busbar_filled'][row_nums, :] bb_mask = ndimage.binary_dilation(bb_mask, np.ones((1, 11), np.uint8)) mask2 = (mask2 | bb_mask) breaks1[mask2] = 0 if False: view = ImageViewer(im) ImageViewer(break_strength) ImageViewer(breaks1) view.show() sys.exit() #################### # BROKEN FINGERS 2 # #################### # - find breaks by comparing with fingers above/below (using bright lines image) # - the advantage of this approach is that it can find breaks near edges # and busbars # - doesn't need to be very sensitive -- just find breaks near borders, which should be strong # - note that there also needs to be a gradient at that spot, otherwise we get # some phantom breaks rows_filtered = np.zeros_like(mono) pixel_ops.FilterV(mono, rows_filtered) rows_filtered[:2, :] = finger_im[:2, :] rows_filtered[-2:, :] = finger_im[-2:, :] bright_lines = mono - rows_filtered mask_background = ((mask == 1) | (mask == 8)) bright_lines[mask_background] = False pixel_ops.ClipImage(bright_lines, 0.0, 1.0) pixel_ops.ApplyThresholdLT_F32(mono, bright_lines, 0.4, 0.0) if False: view = ImageViewer(mono) ImageViewer(mask) ImageViewer(bright_lines) view.show() sys.exit() # filter bright lines min_length = w // 20 cc_sums = np.zeros_like(bright_lines) breaks2 = np.zeros_like(bright_lines, np.uint8) pixel_ops.BrightLineBreaks(bright_lines, cc_sums, breaks2, 0.04, parameters.BROKEN_FINGER_THRESHOLD2, min_length) if False: for r in range(cc_sums.shape[0]): if cc_sums[r, :].sum() == 0: continue print r plt.figure() plt.plot(bright_lines[r, :]) plt.plot((cc_sums[r, :] > 0) * bright_lines[r, :].max()) plt.figure() plt.plot(mono[r, :]) plt.plot((cc_sums[r, :] > 0) * mono[r, :].max()) plt.show() if False: view = ImageViewer(im) ImageViewer(bright_lines) ImageViewer(cc_sums) ImageViewer(breaks2) view.show() ########### # COMBINE # ########### # if there are 2+ close together, only keep one with strongest gradient if False: # check break source # green == independent lines (breaks1) # red == relative lines (breaks2) breaks_full = np.zeros_like(im) breaks_full[features["_finger_row_nums"], :] = breaks1 breaks_full = ndimage.binary_dilation(breaks_full, structure=ndimage.generate_binary_structure(2, 1), iterations=3) rgb = ip.overlay_mask(im, breaks_full, 'g') breaks_full = np.zeros_like(im) breaks_full[features["_finger_row_nums"], :] = breaks2 breaks_full = ndimage.binary_dilation(breaks_full, structure=ndimage.generate_binary_structure(2, 1), iterations=3) rgb = ip.overlay_mask(rgb, breaks_full, 'r') view = ImageViewer(rgb) view.show() sys.exit() breaks = (breaks1 | breaks2).astype(np.uint8) break_count = ndimage.correlate(breaks, weights=np.ones((1, 15), np.uint8), mode='constant') # .astype(np.uinut8) pixel_ops.CombineBreaks(breaks, break_count, edges_abs) # ignore breaks in cell edge or background cell_template = features['bl_cropped_u8'][features['_finger_row_nums']] breaks[((cell_template == 1) | (cell_template == 8))] = 0 # create break mask breaks_full = np.zeros_like(im, np.uint8) breaks_full[features["_finger_row_nums"], :] = breaks features['mk_finger_break_u8'] = breaks_full if False: print breaks_full.sum() view = ImageViewer(im) ImageViewer(finger_filled) ImageViewer(break_count) ImageViewer(breaks) view.show() sys.exit()
def feature_combination(features): if "ov_bright_area_u8" in features: # dilate to find defects near edges bright = (features['ov_bright_area_u8'] / 255.).astype(np.float32) kernel = np.ones((15, 15), np.uint8) bright = cv2.dilate(bright, kernel=kernel) else: bright = None if "ov_splotches_u8" in features: firing = (features['ov_splotches_u8'] / 255.).astype(np.float32) else: firing = None if "ov_dark_areas_u8" in features: dark = (features['ov_dark_areas_u8'] / 255.).astype(np.float32) else: dark = None # bright areas causing dark areas if bright is not None and dark is not None: weight = 5.0 cols = bright.mean(axis=0) rows = bright.mean(axis=1) dark -= np.r_[cols] * weight dark -= np.c_[rows] * weight pixel_ops.ApplyThresholdLT_F32(dark, dark, 0.0, 0.0) if False: plt.figure() plt.plot(cols) plt.plot(rows) view = ImageViewer(bright) ImageViewer(features['ov_dark_areas_u8'] / 255.) ImageViewer(dark) view.show() features['ov_dark_areas_u8'] = (dark * 255).astype(np.uint8) # dark middles # remove dark spots & broken fingers in high firing areas if firing is not None: FIRING_THRESH = 0.1 # if "mk_cracks_u8" in features: # pixel_ops.ApplyThresholdGT_F32_U8(firing, features["mk_cracks_u8"], FIRING_THRESH, 0) if "mk_dark_spots_outline_u8" in features: pixel_ops.ApplyThresholdGT_F32_U8(firing, features["mk_dark_spots_outline_u8"], FIRING_THRESH, 0) if "mk_finger_break_u8" in features: # TODO: handle finger breaks differently: look at sum along finger, and remove all pixel_ops.ApplyThresholdGT_F32_U8(firing, features["mk_finger_break_u8"], FIRING_THRESH, 0) if False: view = ImageViewer(firing) view.show() # remove cracks, dark spots & broken fingers in bright areas if bright is not None: # expand a bit to get artifacts on corner BRIGHT_THRESH = 0.1 # if "mk_cracks_u8" in features: # pixel_ops.ApplyThresholdGT_F32_U8(bright, features["mk_cracks_u8"], BRIGHT_THRESH, 0) if "mk_finger_break_u8" in features: pixel_ops.ApplyThresholdGT_F32_U8(bright, features["mk_finger_break_u8"], BRIGHT_THRESH, 0) if "mk_dark_spots_outline_u8" in features: pixel_ops.ApplyThresholdGT_F32_U8(bright, features["mk_dark_spots_outline_u8"], BRIGHT_THRESH, 0) if False: view = ImageViewer(bright) view.show() # bright areas causing middle dark # overlapping cracks and dark spots if "mk_dark_spots_outline_u8" in features and "mk_cracks_u8" in features and \ features['mk_dark_spots_outline_u8'].max() > 0 and \ features['mk_cracks_u8'].max() > 0: ccs, num_ccs = ip.connected_components(features["mk_dark_spots_outline_u8"]) remove_list = set(ccs[(features['mk_dark_spots_outline_u8'] == 1) & (features['mk_cracks_u8'] == 1)]) lut = np.ones(num_ccs + 1, np.int32) lut[0] = 0 lut[list(remove_list)] = 0 removed = np.take(lut, ccs) if False: view = ImageViewer(features["mk_cracks_u8"]) ImageViewer(ccs) ImageViewer(removed) view.show() features['mk_dark_spots_outline_u8'] = removed.astype(np.uint8)
def finger_defects(features): # TODO: bright lines for top and bottom rows im = features['im_norm'] row_nums = features['_finger_row_nums'] ################ # BRIGHT LINES # ################ # highlight bright lines based on difference between lines above/below finger_im = im[row_nums, :].copy() # based on differences above & below rows_smooth = finger_im rows_filtered = np.zeros_like(rows_smooth) pixel_ops.FilterV(rows_smooth, rows_filtered) rows_filtered[:2, :] = finger_im[:2, :] rows_filtered[-2:, :] = finger_im[-2:, :] bright_lines = rows_smooth - rows_filtered bright_lines /= max(0.1, (0.5 - parameters.BRIGHT_LINES_MULTI_SENSITIVITY)) # make less sensitive for multi bright_lines -= 0.25 bright_lines /= 0.75 bright_lines = ndimage.uniform_filter(bright_lines, size=(1, 5)) pixel_ops.ClipImage(bright_lines, 0.0, 1.0) if False: plt.figure() plt.plot(finger_im[42, :]) view = ImageViewer(finger_im) ImageViewer(im) ImageViewer(rows_filtered) ImageViewer(bright_lines) view.show() sys.exit() # get strength of each line (sum) # - need to insert lines between rows for computing CCs bl_cc = np.zeros((bright_lines.shape[0] * 2, bright_lines.shape[1]), np.uint8) bl_cc[::2, :] = bright_lines > 0 ccs, num_ccs = ip.connected_components(bl_cc) ccs = ccs[::2, :] sums = ndimage.sum(bright_lines, ccs, range(num_ccs + 1)) sums /= (float(bright_lines.shape[1]) / 100.0) line_strengths = np.take(sums, ccs) bright_lines[line_strengths < parameters.BRIGHT_LINES_MULTI_THRESHOLD] = 0 features['bright_lines_count'] = (sums > parameters.BRIGHT_LINES_MULTI_THRESHOLD).sum() if False: print features['bright_lines_count'] view = ImageViewer(bl_cc) ImageViewer(ccs) ImageViewer(line_strengths) ImageViewer(bright_lines) view.show() # create a full size image of bright lines bright_lines_full = np.zeros_like(im) bright_lines_full[row_nums, :] = bright_lines bright_lines_full = cv2.GaussianBlur(bright_lines_full, sigmaX=2, ksize=(0, 0)) features['ov_bright_lines_u8'] = (bright_lines_full * 255).astype(np.uint8) features['bright_lines_strength'] = bright_lines.mean() features['bright_lines_length'] = (bright_lines > 0.5).sum() if False: im[row_nums, :] = 0 view = ImageViewer(im) ImageViewer(finger_im) ImageViewer(bright_lines > 0.5) ImageViewer(bright_lines_full) view.show() sys.exit() if True: # want a 1-1 correspondence with bright lines, so: # - threshold based on sum of line # - add break to end with highest gradient f = np.array([[-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]], dtype=np.float32) edges_abs = np.abs(ndimage.correlate(finger_im, weights=f, mode="nearest")) cc_labels = np.where(sums > parameters.BRIGHT_LINES_MULTI_THRESHOLD)[0] max_pos = ndimage.maximum_position(edges_abs, ccs, cc_labels) breaks = np.zeros_like(finger_im, np.uint8) if len(max_pos) > 0: ys, xs = zip(*max_pos) breaks[np.array(ys, np.int32), np.array(xs, np.int32)] = 1 else: #################### # BROKEN FINGERS 1 # #################### # - find breaks by comparing with fingers above/below (using bright lines image) # - the advantage of this approach is that it can find breaks near edges # and busbars # - since there is a step 2, doesn't need to be very sensitive -- just find # breaks near borders, which should be strong # - note that there also needs to be a gradient at that spot, otherwise we get # some phantom breaks w = 11 g = 3 s = g + (w // 2) maxs = ndimage.maximum_filter(bright_lines, size=(1, w)) mins = ndimage.minimum_filter(bright_lines, size=(1, w)) diffs = np.maximum(np.roll(mins, s, axis=1) - np.roll(maxs, -s, axis=1), np.roll(mins, -s, axis=1) - np.roll(maxs, s, axis=1)) diffs = ndimage.gaussian_filter1d(diffs, sigma=3, axis=1) pixel_ops.ApplyThresholdLT_F32(diffs, diffs, 0.0, 0.0) local_maxes = np.logical_and((diffs > np.roll(diffs, 1, axis=1)), (diffs > np.roll(diffs, -1, axis=1))) diffs[~local_maxes] = 0 f = np.array([[-1, -1, -1, -1, -1, 0, 1, 1, 1, 1, 1]], dtype=np.float32) edges_abs = np.abs(ndimage.correlate(finger_im, weights=f, mode="nearest")) breaks1 = ((diffs > parameters.BROKEN_FINGER_MULTI_THRESHOLD1) & (edges_abs > parameters.BROKEN_FINGER_MULTI_EDGE_THRESHOLD)) breaks1[:, :features['cell_edge_left']] = False breaks1[:, features['cell_edge_right']:] = False if False: view = ImageViewer(bright_lines) ImageViewer(diffs) ImageViewer(edges_abs) ImageViewer(breaks1) # ImageViewer(np.roll(mins, shift=s, axis=1) - np.roll(maxs, shift=-s, axis=1)) view.show() sys.exit() # add to defect mask breaks_full = np.zeros_like(im) breaks_full[features["_finger_row_nums"], :] = breaks breaks_full = ndimage.binary_dilation(breaks_full, structure=ndimage.generate_binary_structure(2, 1), iterations=3) features['mk_finger_break_u8'] = breaks_full.astype(np.uint8) features['finger_break_count'] = breaks.sum() if False: view = ImageViewer(im) ImageViewer(bright_lines) # ImageViewer(break_strength) ImageViewer(breaks) ImageViewer(create_overlay(features)) view.show() sys.exit()
def bright_areas(features): im = features['im_norm'] row_nums = features['_finger_row_nums'] im_fingers = features['im_norm'][row_nums, :] vals = np.sort(im_fingers.flat) count = vals.shape[0] p01 = vals[int(round(0.01 * count))] p95 = vals[int(round(0.95 * count))] p99 = vals[int(round(0.999 * count))] # find highest peak counts, edges = np.histogram(vals, bins=50, density=True) i_max = np.argmax(counts) val_mode = (edges[i_max] + edges[i_max + 1]) / 2.0 if p99 - p95 > 0.2: # thresh 1: assuming a symmetric distribution, mode+spread on low ed thresh1 = val_mode + (val_mode - p01) * 2 # thresh 2: look for a distribution that drops very low below_02 = np.where(counts[i_max:] < 0.025)[0] if len(below_02) > 0: i = i_max + below_02[0] thresh2 = (edges[i] + edges[i + 1]) / 2.0 thresh = min(thresh1, thresh2) else: thresh = thresh1 else: thresh = im_fingers.max() thresh -= parameters.BRIGHT_AREA_MULTI_SENSITIVITY bright_areas = im_fingers - thresh bright_areas /= 0.5 pixel_ops.ApplyThresholdLT_F32(bright_areas, bright_areas, 0.0, 0.0) pixel_ops.ApplyThresholdGT_F32(bright_areas, bright_areas, 1.0, 1.0) # create a full size image of bright lines bright_lines_full = np.zeros_like(im) bright_lines_full[row_nums, :] = bright_areas bright_lines_full = cv2.GaussianBlur(bright_lines_full, sigmaX=1, sigmaY=2, ksize=(0, 0)) if 'ov_bright_area_u8' in features: features['ov_bright_area_u8'] = np.maximum((bright_lines_full * 255).astype(np.uint8), features['ov_bright_area_u8']) else: features['ov_bright_area_u8'] = (bright_lines_full * 255).astype(np.uint8) features['bright_area_strength'] = bright_areas.mean() * 100 features['bright_area_fraction'] = (pixel_ops.CountThresholdGT_F32(bright_areas, 0.1)) / float( im.shape[0] * im.shape[1]) features['_bright_area_thresh'] = thresh if False: print features['bright_area_strength'] print pixel_ops.CountThresholdGT_F32(bright_areas, 0.1) * 100 / float(im.shape[0] * im.shape[1]) print features['bright_area_fraction'] view = ImageViewer(im) ImageViewer(im_fingers) ImageViewer(bright_areas) plt.figure() plt.hist(im_fingers.flatten(), bins=50, normed=True) plt.plot((edges[:-1] + edges[1:]) / 2.0, counts) plt.vlines(thresh, 0, counts.max()) view.show()