def labnames2imgs_cens(labnames, n): data = [] for ln in labnames: t, z = (int(ln[-11:-8]), int(ln[-7:-4])) z2 = floor((z + 0.5) * 5.5) img = imread(imgnames(n)[t]) cen = imread(cennames(n)[t]) lab = imread(ln) lab = zoom(lab, (0.5, 0.5), order=0) lab = label(lab)[0] img = gputools.scale(img, (5.5, 0.5, 0.5)) img = img[..., np.newaxis] ## upscale centerpoints. make them just a single pixel. pts = np.array([n['centroid'] for n in nhl_tools.hyp2nhl(cen)]).astype(np.int) pts = np.floor((pts + 0.5) * [5.5, 0.5, 0.5]).astype(np.int) cen = np.zeros(img.shape[:-1]) cen[tuple(pts.T)] = 1 data.append({ 'img': img, 'cen': cen, 'lab': lab, 't': t, 'z': z, 'z2': z2, 'labname': ln }) return data
def build_rawdata(homedir, savedir): homedir = Path(homedir) savedir = Path(savedir) labnames = glob(str(homedir / 'Fluo-N3DH-CE/01_GT/SEG/man_seg_*')) print(labnames) cat = np.concatenate def imread2(x): return imread(str(x)) imgs = [] cens = [] labs = [] times_zs = [] hyps = [] for i in range(len(labnames)): i = 2 ln = labnames[i] tz = (int(ln[-11:-8]), int(ln[-7:-4])) t,z = tz lab = imread2(ln) img = imread2(homedir / 'Fluo-N3DH-CE/01/t{:03d}.tif'.format(t)) cen = imread2(homedir / 'Fluo-N3DH-CE/01_GT/TRA/man_track{:03d}.tif'.format(t)) img = gputools.scale(img, (5.5,0.5,0.5)) lab = zoom(lab, (0.5,0.5), order=0) lab = label(lab)[0] # cen = label(cen)[0].astype(np.uint16) cen = cen>0 cen = gputools.scale(cen, (5.5,0.5,0.5), interpolation='linear') cen = label(cen>0.5)[0] img2 = gputools.denoise.nlm3(img, sigma=20) hyp = watershed(img2, cen, mask=img2>img2.mean()*1.0, compactness=1000) z2 = int(z*5.5) print(ln) print(scores_dense.seg(lab, hyp[z2])) # ipdb.set_trace() return locals()
def regionotsu(img3): # threshimg = np.ones(img3.shape) sh = (50,1,2) threshimg = np.zeros(sh) patchshape = [ceil(1*img3.shape[i]/sh[i]) for i in range(img3.ndim)] # res = patchmaker.patchtool({'img':img3.shape,'patch':(170//2,256//4,356//4),'overlap_factor':(2,2,2)}) res = patchmaker.patchtool({'grid':sh,'img':img3.shape,'patch':patchshape}) for i in range(len(res['starts'])): ss = res['slices'][i] ind = res['inds'][i] th = otsu3d(img3[ss]) # th = np.percentile(img3[ss],90) #*0.75 #.mean()*2.5 # th = img3[ss].max threshimg[tuple(ind)] = th scale = [img3.shape[i]/threshimg.shape[i] for i in range(img3.ndim)] threshimg = gputools.scale(threshimg.astype(np.float32),scale) return threshimg
def test_scale(): d = np.zeros((100,100,100),np.float32) scale(d,1.7, interp = "linear") scale(d,.3, interp = "linear")
def test_scale(): d = np.zeros((100, 100, 100), np.float32) scale(d, 1.7, interp="linear") scale(d, .3, interp="linear")
def test_scale(x): s = np.random.uniform(.5,1.5,3) out1 = scale(x, s, interpolation="nearest") out2 = ndimage.zoom(x, s, order=0, prefilter=False) return out1, out2
def crop_stack(data, sc=(5.5, .5, .5)): newshape = _scale_shape(_scale_shape(data.shape, sc), tuple(1. / s for s in sc)) return pad_to_shape(data, newshape) def pad_stack(data, target_shape): return pad_to_shape(data, target_shape) data_target = np.zeros((31, 512, 712), np.uint16) sc = (5.5, .5, .5) sc_inv = tuple(1. / s for s in sc) data_crop = crop_stack(data_target, sc) data_crop_scaled = scale(data_crop, sc) # do segmenting on data_crop_scaled, resulting in seg_crop_scaled seg_crop_scaled = data_crop_scaled seg_crop = scale(seg_crop_scaled, sc_inv) seg_target = pad_stack(seg_crop, data_target.shape) print(data_target.shape, seg_target.shape)
def test_scale(): d = create_shape((101,102,103)) scale(d,1.7, interp = "linear") out = scale(d,.3, interp = "nearest") return out
def f(idx): return gputools.scale(img[idx[0], ..., idx[1]], scale)
def build_rawdata(homedir): homedir = Path(homedir) ## load data # img = imread(str(homedir / 'data/img006.tif')) img = np.load(str(homedir / 'data/img006_noconv.npy')) img = img[1] img = perm(img, "ZCYX", "ZYXC") img = norm_szyxc_per(img, (0, 1, 2)) img = img.astype(np.float32) points = lib.mkpoints() imgsem = {'axes': "ZYXC", 'nuc': 1, 'mem': 0, 'n_channels': 2} # img = img[:70,::2,::2] scale = (2.5, .5, .5) def f(idx): return gputools.scale(img[..., idx[0]], scale) img = ts.broadcast_over(f, (2, )) img = perm(img, "czyx", "zyxc") points = (points * scale).astype(np.int) # points[:,0] = points[:,0].clip(0,img.shape[0]-1) # scale! # img = zoom(img, (5,1,1,1)) if False: shape = np.array(img.shape) res = patchmaker.patchtool({ 'img': shape, 'patch': shape // 2, 'grid': (2, 2, 2, 2) }) scaled = [ gputools.scale((img[ss][..., 0]).astype(np.float32), (5, 1, 1)) for ss in res['slices'] ] ends = res['starts'] + shape // 2 * [5, 1, 1, 1] container = np.zeros(shape * [5, 1, 1, 1]) big_patches = patchmaker.starts_ends_to_slices(res['starts'], ends) for i in range(len(big_patches)): container[big_patches[i]] = scaled[i][..., np.newaxis] container = img.copy() cen = np.zeros(container.shape[:-1]) cen[list(points.T)] = 1 x = container[..., 1] hx = np.array([1, 1, 1]) / 3 x = gputools.convolve_sep3(x, hx, hx, hx, sub_blocks=(1, 1, 1)) lab = watershed(-x, label(cen)[0], mask=x > x.mean()) # bor = np.array([morph.binary_dilation(b) for b in bor]) ## just x,y # bor = np.array([morph.binary_dilation(b) for b in bor]) ## just x,y if False: target = lab[::2, ::2, ::2] points = points // 2 bg = target == 0 fg = target == 1 target[fg] = 0.0 hx = np.array([1, 1, 1]) / 3 for i in range(200): target[bg] = 0 target[list(points.T)] = 1 target = gputools.convolve_sep3(target, hx, hx, hx, sub_blocks=(2, 2, 2)) points = points * 2 target = gputools.scale(target, (2, 2, 2)) if False: xx = np.linspace(-2, 2, 80) xx = np.exp(-xx**2 / 2) xx = xx / xx.sum() gauss = gputools.convolve_sep3(cen, xx, xx, xx, sub_blocks=(2, 2, 2)) if True: target = lab2bgdist(lab) if False: target = target * gauss target = np.exp(-target / 10) res = dict() res['source'] = container res['imgsem'] = imgsem res['target'] = target res['points'] = points res['cen'] = cen return res
def test_scale(): d = create_shape((101, 102, 103)) scale(d, 1.7, interpolation="linear") out = scale(d, .3, interpolation="nearest") return out
def f(x): # t,c = ind # x = arr_in[t,c] x = gputools.scale(x, (subsample, 1, 1), interpolation='linear') return x