def adaptive_thres_two(img, FIL1=10, FIL2=100, R1=100, R2=100): """adaptive thresholding for picking objects with different brightness. FIL2 and R2 for removing background. """ bw = adaptive_thresh(img, R=R1, FILTERINGSIZE=FIL1) foreground = adaptive_thresh(img, R=R2, FILTERINGSIZE=FIL2) bw[-foreground] = 0 return label(bw)
def adaptive_thres_otsu(img, FIL1=4, R1=1): """adaptive thresholding for picking objects with different brightness. Use Otsu's method for removing background """ bw = adaptive_thresh(img, R1, FIL1) foreground = global_otsu(img) > 0 bw[-foreground] = 0 return label(bw)
def n4_illum_correction(img, RATIO=1.5, FILTERINGSIZE=50): """ Implementation of the N4 bias field correction algorithm. Takes some calculation time. It first calculates the background using adaptive_thesh. """ bw = adaptive_thresh(img, R=RATIO, FILTERINGSIZE=FILTERINGSIZE) img = homogenize_intensity_n4(img, -bw) return img
def ring_dilation_above_adaptive(labels, img, MARGIN=0, RINGWIDTH=4, BUFFER=2, RATIO=1.05, FILSIZE=10): sub_labels = dilate_to_cytoring_buffer(labels, RINGWIDTH, MARGIN, BUFFER) bw = adaptive_thresh(img, R=RATIO, FILTERINGSIZE=FILSIZE) sub_labels[-bw] = 0 return sub_labels
def n4_illum_correction_downsample(img, DOWN=2, RATIO=1.05, FILTERINGSIZE=50, OFFSET=10): """Faster but more insensitive to local illum bias. """ fil = sitk.ShrinkImageFilter() cc = sitk.GetArrayFromImage(fil.Execute(sitk.GetImageFromArray(img), [DOWN, DOWN])) bw = adaptive_thresh(cc, R=RATIO, FILTERINGSIZE=FILTERINGSIZE/DOWN) himg = homogenize_intensity_n4(cc, -bw) himg = cc - himg # himg[himg < 0] = 0 bias = resize_img(himg, img.shape) img = img - bias return convert_positive(img, OFFSET)
def agglomeration_seed(labels, img, MINSIZE=50, STEPS=100, FILSIZE=5, RATIO=0): """ MINSIZE: minimum area for a seed object. It can be smaller than actual objects. STEPS: Larger it is, more resolution and computation FILSIZE: argument for adaptive thresholding. Larger if capturing too much backrgound. RATIO: argument for adaptive thresholding. Larger if capturing too much backrgound. """ seed = binary_erosion(labels, np.ones((3, 3))) li = [] img = img.astype(np.float32) mask = adaptive_thresh(img, RATIO, FILSIZE) mask = binary_opening(mask, np.ones((3, 3))) mask = remove_small_objects(mask, MINSIZE) foreground = img[mask] perclist = [ np.percentile(foreground, r) for r in np.linspace(0, 100, STEPS) ] for _r in perclist: thresed = remove_small_objects(img > _r, MINSIZE, connectivity=2) > 0 li.append(thresed.astype(np.uint16)) if seed is not None: li.append((seed > 0).astype(np.uint16)) for l in li: l[seed > 0] = 1 q = np.sum(np.dstack(li), axis=2) p = label(q) for ind in reversed(np.unique(q).tolist()): c = seeding_separate(label(q >= ind), p) w = watershed(q >= ind, markers=c, mask=(q >= ind), watershed_line=True) w[mask == 0] = 0 w = remove_small_objects(w, MINSIZE) p = label(w, connectivity=2) return p
def adaptive_thres(img, FIL1=10, R1=100): """adaptive thresholding for picking objects with different brightness. """ bw = adaptive_thresh(img, R=R1, FILTERINGSIZE=FIL1) return label(bw)