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 background_subtraction_wavelet_hazen(img, THRES=100, ITER=5, WLEVEL=6, OFFSET=50): """Wavelet background subtraction. """ back = wavelet_subtraction_hazen(img.astype(np.float), ITER=ITER, THRES=THRES, WLEVEL=WLEVEL) img = img - back return convert_positive(img, OFFSET)
def background_subtraction_wavelet(img, level=7, OFFSET=10): ''' It might be radical but works in many cases in terms of segmentation. Use "background_subtraction_wavelet_hazen" for a proper implementation. ''' from pywt import WaveletPacket2D from skimage.transform import resize def wavelet_subtraction(img, level): """6- 7 level is recommended""" if level == 0: return img wp = WaveletPacket2D(data=img.astype(np.uint16), wavelet='haar', mode='sym') back = resize(np.array(wp['a'*level].data), img.shape, order=3, mode='reflect')/(2**level) img = img - back return img img = wavelet_subtraction(img, level) return convert_positive(img, OFFSET)
def rolling_ball(img, RADIUS=100, SIGMA=3, OFFSET=50): """Rolling ball background subtraction. """ back = rolling_ball_subtraction_hazen(img.astype(np.float), RADIUS) img = img - back return convert_positive(img, OFFSET)