예제 #1
0
def lab2bgdist(lab):
    distimg = lab.copy()
    distimg[lab != 0] = 1
    bor = label_tools.find_boundaries(lab)
    distimg[bor] = 0  ## mem is also bg!
    distimg = distance_transform_edt(distimg)
    hx = np.array([1, 1, 1]) / 3
    distimg = gputools.convolve_sep3(distimg, hx, hx, hx, sub_blocks=(1, 1, 1))
    distimg = gputools.convolve_sep3(distimg, hx, hx, hx, sub_blocks=(1, 1, 1))
    distimg[lab == 0] = 0
    distimg[bor] = 0
    for i in range(1, lab.max()):
        m = lab == i
        distimg[m] = distimg[m] / max(distimg[m].max(), 1)
    return distimg
예제 #2
0
def test_conv_sep3_numpy():
    Nz, Nx, Ny = 128, 203, 303

    d = np.zeros((Nz, Ny, Nx), np.float32)

    d[::10, ::10, ::10] = 1.

    hx = np.ones(8)
    hy = np.ones(3)
    hz = np.ones(11)

    res1 = gputools.convolve_sep3(d, hx, hy, hz, sub_blocks=(1, 1, 1))
    res2 = gputools.convolve_sep3(d, hx, hy, hz, sub_blocks=(7, 4, 3))

    assert np.allclose(res1, res2)
    return res1, res2
예제 #3
0
    def apply(self, data):
        sigs = (self.sx, self.sy, self.sz)
        Ns = [2 * s + 1 for s in sigs]

        xs = [np.arange(-N, N + 1) for N in Ns]
        hs = [np.exp(-x**2 / 2. / s**2) for x, s in zip(xs, sigs)]
        hs = [1. * h / sum(h) for h in hs]
        return gputools.convolve_sep3(data, *hs)
예제 #4
0
    def apply(self,data):
        sigs = (self.sx,self.sy,self.sz)
        Ns  = [2*s+1 for s in sigs]

        xs = [np.arange(-N,N+1) for N in Ns]
        hs = [np.exp(-x**2/2./s**2) for x,s in zip(xs,sigs)]
        hs = [1.*h/sum(h) for h in hs]
        return gputools.convolve_sep3(data, *hs)
예제 #5
0
def test_conv_gpu():
    N = 128
    d = np.zeros((N, N + 3, N + 5), np.float32)

    d[N // 2, N // 2, N // 2] = 1.

    h = np.exp(-10 * np.linspace(-1, 1, 17)**2)

    res = gputools.convolve_sep3(d, h, h, h)
예제 #6
0
def test_conv_sep3_numpy():
    Nz, Nx, Ny  = 128, 203, 303

    d = np.zeros((Nz, Ny,Nx),np.float32)

    d[::10,::10,::10] = 1.


    hx = np.ones(8)
    hy = np.ones(3)
    hz = np.ones(11)


    res1 = gputools.convolve_sep3(d,hx,hy,hz, sub_blocks=(1,1,1))
    res2 = gputools.convolve_sep3(d,hx,hy,hz,  sub_blocks=(7,4,3))

    assert np.allclose(res1,res2)
    return res1, res2
예제 #7
0
def test_conv_gpu():

    N  = 128
    d = np.zeros((N,N+3,N+5),np.float32)

    d[N/2,N/2,N/2]  = 1.

    h = np.exp(-10*np.linspace(-1,1,17)**2)

    res = gputools.convolve_sep3(d,h,h,h)
예제 #8
0
def segment(d, params):
    img2 = gputools.denoise.nlm3(d['img'][..., 0], sigma=params['sigma'])
    hx = np.array([1, 1, 1]) / 3
    for _ in range(params['nblur']):
        img2 = gputools.convolve_sep3(img2, hx, hx, hx)
    hyp = watershed(-img2,
                    label(d['cen'] > 0)[0],
                    mask=img2 > img2.mean(
                        (1, 2), keepdims=True) * params['m_mask'],
                    compactness=params['compactness'])
    return hyp
예제 #9
0
def segment_pimg_img(d, params):
    img = d['img'][..., 0].astype(np.float32)
    pimg = d['pimg']
    hx = np.array([1, 1, 1]) / 3
    img2 = gputools.denoise.nlm3(img, sigma=params['sigma'])
    for _ in range(params['nblur']):
        img2 = gputools.convolve_sep3(img2, hx, hx, hx)
    hyp = watershed(-img2,
                    label(pimg > params['thi'])[0],
                    mask=img2 > params['fmask'](img2) * params['m_mask'],
                    compactness=params['compactness'])
    return hyp
예제 #10
0
def test_simple3():
    d = np.zeros((100, 100, 100))
    d[50, 50, 50] = 1.
    hx = np.random.uniform(0, 1, 7)
    hy = np.random.uniform(0, 1, 11)
    hz = np.random.uniform(0, 1, 17)

    # hy = np.ones(1)
    # hz = np.ones(1)

    res1 = _conv_sep3_numpy(d, hx, hy, hz)
    res2 = gputools.convolve_sep3(d, hx, hy, hz)
    return res1, res2
예제 #11
0
def segment_otsu(d, params):
    img2 = gputools.denoise.nlm3(d['img'][..., 0], sigma=params['sigma'])
    hx = np.array([1, 1, 1]) / 3
    for _ in range(params['nblur']):
        img2 = gputools.convolve_sep3(img2, hx, hx, hx)
    img2 = norm_szyxc(img2, (0, 1, 2))
    # local_otsu = np.array([rank.otsu(img2[i], disk(params['m_mask']))/255 for i in range(img2.shape[0])])
    otsu = otsu3d(img2)
    hyp = watershed(-img2,
                    label(d['cen'] > 0)[0],
                    mask=img2 > otsu,
                    compactness=params['compactness'])
    return hyp
예제 #12
0
def segment_and_plot(img, with_border_cells=True):
    potential = 1 - img[..., 1] + img[..., 0]
    potential = np.clip(potential, 0, 1)
    hx = gaussian(21, 2.0)
    potential = gputools.convolve_sep3(potential, hx, hx, hx)
    potential = lib.normalize_percentile_to01(potential, 0, 100)
    # nhl,hyp = watershed(potential, c1=0.5, c2=0.9)
    hyp = watershed(potential, label(potential < 0.1)[0], mask=potential < 0.5)
    if with_border_cells == False:
        mask_borders = lib.mask_border_objs(hyp)
        hyp[mask_borders] = 0
    nhl = lib.hyp2nhl(hyp, img[..., 1])
    nhl = np.array(nhl)
    areas = np.array([n['area'] for n in nhl])
    xmom = np.array([n['moments_img'][0, 0, 0] / n['area'] for n in nhl])
    col = plt.scatter(np.log2(areas), xmom)  #np.log2(xmom))
    # selector = view.SelectFromCollection(plt.gca(), col)
    return nhl, hyp
예제 #13
0
def build_rawdata2(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))

    imgsem = {'axes': "ZYXC", 'nuc': 1, 'mem': 0, 'n_channels': 2}

    points = lib.mkpoints()
    cen = np.zeros(img.shape[:-1])
    cen[list(points.T)] = 1
    x = img[..., 1]
    hx = np.array([1, 1, 1]) / 3
    x = gputools.convolve_sep3(x, hx, hx, hx)
    lab = watershed(-x, label(cen)[0], mask=x > x.mean())

    bor = label_tools.find_boundaries(lab)
    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
    # bor = np.array([morph.binary_dilation(b) for b in bor]) ## just x,yp
    # bor = morph.binary_erosion(bor)
    # bor = morph.binary_dilation(bor)
    # bor = morph.binary_erosion(bor)
    # bor = morph.binary_dilation(bor)
    # bor = morph.binary_dilation(bor)
    lab[lab != 0] = 1
    lab[bor] = 2
    labsem = {'n_classes': 3, 'nuc': 1, 'mem': 2, 'bg': 0, 'axes': 'ZYX'}

    res = dict()
    res['img'] = img
    res['imgsem'] = imgsem
    res['lab'] = lab
    res['labsem'] = labsem
    res['points'] = points
    res['cen'] = cen
    # res['mask_labeled_slices'] = mask_labeled_slices
    # res['inds_labeled_slices'] = inds_labeled_slices
    # res['gt_slices'] = gt_slices
    return res
예제 #14
0
def _convolve_rand(dshape, hshapes):
    print("convolving test: dshape = %s, hshapes  = %s" % (dshape, hshapes))
    np.random.seed(1)
    d = np.random.uniform(0, 1, dshape).astype(np.float32)

    hs = [
        np.random.uniform(1, 2, hshape).astype(np.float32)
        for hshape in hshapes
    ]

    if len(hs) == 2:
        out1 = _conv_sep2_numpy(d, *hs)
        out2 = gputools.convolve_sep2(d, *hs)
    elif len(hs) == 3:
        out1 = _conv_sep3_numpy(d, *hs)
        out2 = gputools.convolve_sep3(d, *hs)

    print("difference: ", np.amax(np.abs(out1 - out2)))

    npt.assert_allclose(out1, out2, rtol=1.e-2, atol=1.e-5)

    return out1, out2
예제 #15
0
 def apply(self,data):
     N  = 2*self.sigma+1
     x = np.arange(-N,N+1)
     h = np.exp(-x**2/2./self.sigma**2)
     h *= 1./sum(h)
     return gputools.convolve_sep3(data, h, h, h)
예제 #16
0
 def apply(self, data):
     N = 2 * self.sigma + 1
     x = np.arange(-N, N + 1)
     h = np.exp(-x**2 / 2. / self.sigma**2)
     h *= 1. / sum(h)
     return gputools.convolve_sep3(data, h, h, h)
예제 #17
0
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
예제 #18
0
    'NET (border weights)', 'NET (t007)'
]
data = [ys_ilastik, ys_t001, ys_t002, ys_t005, ys_t006, ys_t007]
plt.figure()
for i in range(6):
    name = names[i]
    img = data[i]
    # if i==5:
    # img = img[1::5]
    # potential = 1 - img[...,1] + img[...,0]
    # potential = lib.normalize_percentile_to01(potential, 0, 100)*2
    # hyp = watershed(potential, seeds, mask=potential<0.5)
    # nhl = lib.hyp2nhl(hyp)
    potential = img[..., 1]  # - img[...,0]
    hx = gaussian(21, 2.0)
    potential = gputools.convolve_sep3(potential, hx, hx, hx)
    potential = lib.normalize_percentile_to01(potential, 0, 100)
    nhl, hyp = lib.two_var_thresh(potential, c1=0.5, c2=0.9)
    areas = np.array([n['area'] for n in nhl])
    if i == 5:
        areas = areas / 5
    plt.plot(sorted(np.log2(areas)), label=name)
plt.legend()

## Let's take a close look at segmentations using slice views

data = [ys_ilastik, ys_t001, ys_t007]
a, b, c, d = ys_t001.shape
bigimg = np.zeros((a, b, 3 * c), dtype=np.float)
for i in range(3):
    img6_t1 = img6[1, ..., 1].copy()