How to use the mahotas.labeled function in mahotas

To help you get started, we’ve selected a few mahotas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github luispedro / mahotas / tests / test_labeled.py View on Github external
def test_border():
    labeled = np.zeros((32,32), np.uint8)
    labeled[8:11] = 1
    labeled[11:14] = 2
    labeled[14:17] = 3
    labeled[10,8:] = 0
    b12 = mahotas.labeled.border(labeled, 1, 2)
    YX = np.where(b12)
    YX = np.array(YX).T
    b13 = mahotas.labeled.border(labeled, 1, 3)

    assert not np.any(b13)
    assert np.any(b12)
    assert (11,0) in YX
    assert (11,1) in YX
    assert (12,1) in YX
    assert (12,9) not in YX

    b13 = mahotas.labeled.border(labeled, 1, 3, always_return=0)
    assert b13 is None
github Rhoana / dojo / _dojo / scripts / repr_vol.py View on Github external
num_patches = num_steps_x * num_steps_y * num_steps_z

#print num_patches*(num_bins-1)
features = np.zeros((num_patches,num_bins-1),dtype=np.uint32)
coordinates = np.zeros((num_patches,3),dtype=np.uint32)

i = 0
for x in range(0,shape_x-patch_size_x,step_size_x):
  for y in range(0,shape_y-patch_size_y,step_size_y):
    for z in range(0,shape_z-patch_size_z, step_size_z):
      #print x,y,z,i
      coordinates[i,2] = x
      coordinates[i,1] = y
      coordinates[i,0] = z
      sub_vol = vol[z:z+patch_size_z, y:y+patch_size_y, x:x+patch_size_x]
      sub_vol,_ = mh.labeled.relabel(sub_vol.astype(np.intc))
      sub_sizes = mh.labeled.labeled_size(sub_vol)
      hist, _ = np.histogram(sub_sizes, bins=bins)
      features[i,:] = hist.astype(np.uint32)
      i += 1
            
centroid = np.mean(features,axis=0)

centroid_matrix = np.tile(centroid, (num_patches,1))

dist_squared_matrix = np.square(np.subtract(features, centroid_matrix))

dist_vector = np.sqrt(np.sum(dist_squared_matrix, axis=1))

min_i = dist_vector.argmin()

x = coordinates[min_i,2]
github urakubo / UNI-EM / dojo / controller.py View on Github external
end_points = mh.morph.dilate(end_points, np.ones((2*brush_size, 2*brush_size)))

    # compute seeds
    seed_mask = np.zeros(brush_mask.shape,dtype=bool)
    # seed_mask[outside_brush_mask & brush_mask] = True
    seed_mask[outside_brush_mask] = True
    seed_mask[frame] = True
    # seed_mask[corners] = False
    seed_mask[end_points] = False
    seeds,n = mh.label(seed_mask)

    # remove small regions
    sizes = mh.labeled.labeled_size(seeds)
    min_seed_size = 5
    too_small = np.where(sizes < min_seed_size)
    seeds = mh.labeled.remove_regions(seeds, too_small).astype(np.uint8)

    #
    # run watershed
    #
    ws = mh.cwatershed(brush_image.max() - brush_image, seeds)

    lines_array = np.zeros(ws.shape,dtype=np.uint8)
    lines = []

    for y in range(ws.shape[0]-1):
      for x in range(ws.shape[1]-1):

        # print 'looking for', seg_sub_tile[y,x]

        if self.lookup_label(seg_sub_tile[y,x]) != self.label_id:
          continue
github Rhoana / dojo / _dojo / controller.py View on Github external
# seeds,n = mh.label(brush_boundary_mask)
    seeds,n = mh.label(seed_mask)

    print n

    # remove small regions
    sizes = mh.labeled.labeled_size(seeds)
    min_seed_size = 5
    too_small = np.where(sizes < min_seed_size)
    seeds = mh.labeled.remove_regions(seeds, too_small).astype(np.uint8)


    #
    # run watershed
    #
    ws = mh.cwatershed(brush_image.max() - brush_image, seeds)

    mh.imsave('/tmp/end_points.tif', 50*end_points.astype(np.uint8))
    mh.imsave('/tmp/seeds_mask.tif', 50*seed_mask.astype(np.uint8))
    mh.imsave('/tmp/seeds.tif', 50*seeds.astype(np.uint8))
    mh.imsave('/tmp/ws.tif', 50*ws.astype(np.uint8))

    lines_array = np.zeros(ws.shape,dtype=np.uint8)
    lines = []

    print label_id
github Rhoana / dojo / _dojo / scripts / splitexp.py View on Github external
grad_y = np.gradient(sub_tile)[1]
grad = np.add(np.square(grad_x), np.square(grad_y))
#grad = np.add(np.abs(grad_x), np.abs(grad_y))
grad -= grad.min()
grad /= grad.max()
grad *= 255
grad = grad.astype(np.uint8)

# compute seeds
seeds,_ = mh.label(mask)

# remove small regions
sizes = mh.labeled.labeled_size(seeds)
min_seed_size = 5
too_small = np.where(sizes < min_seed_size)
seeds = mh.labeled.remove_regions(seeds, too_small)


#
# run watershed
#
ws = mh.cwatershed(grad, seeds)

lines_array = np.zeros(ws.shape,dtype=np.uint8)
lines = []

for y in range(ws.shape[0]-1):
  for x in range(ws.shape[1]-1):
    if ws[y,x] != ws[y,x+1]:  
      lines_array[y,x] = 1
      lines.append([x,y])
    if ws[y,x] != ws[y+1,x]:
github luispedro / mahotas / mahotas / labeled.py View on Github external
min_size : int, optional
        Minimum size (in pixels) of objects to keep (default is no minimum)
    max_size : int, optional
        Maximum size (in pixels) of objects to keep (default is no maximum)

    Returns
    -------
    filtered : labeled array
    nr : int
        number of new labels
    '''
    from mahotas.labeled import remove_regions, labeled_size
    labeled = _as_labeled(labeled, labeled, 'filter_labeled')
    if remove_bordering:
        labeled = mh.labeled.remove_bordering(labeled)
        labeled,nr = mh.labeled.relabel(labeled)
    else:
        nr = labeled.max()

    to_keep = np.ones(nr+1, bool)

    if min_size is not None or max_size is not None:
        sizes = labeled_size(labeled)
        if min_size:
            to_keep &= (sizes >= min_size)
        if max_size:
            to_keep &= (sizes <= max_size)
    to_keep[0] = True
    to_remove = np.where(~to_keep)
    labeled = remove_regions(labeled, to_remove)
    labeled,nr = mh.labeled.relabel(labeled, inplace=True)
    return labeled, nr