Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# brush_boundary_mask = brush_boundary_mask[x0:x1,y0:y1]
# brush_image = brush_image[x0:x1,y0:y1]
# 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)
#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]
y = coordinates[min_i,1]
plt.subplot(3,2,2)
f = mahotas.gaussian_filter(f, 4)
f = (f> f.mean())
plt.title('gaussian_filter')
plt.imshow(f)
plt.subplot(3,2,3)
labeled, n_nucleus = mahotas.label(f)
plt.title('Found {} nuclei.'.format(n_nucleus))
plt.imshow(labeled)
plt.subplot(3,2,4)
sizes = mahotas.labeled.labeled_size(labeled)
too_big = np.where(sizes > 10000)
labeled = mahotas.labeled.remove_regions(labeled, too_big)
plt.title('remove_regions')
plt.imshow(labeled)
plt.subplot(3,2,5)
labeled = mahotas.labeled.remove_bordering(labeled)
plt.title('remove_bordering')
plt.imshow(labeled)
plt.subplot(3,2,6)
relabeled, n_left = mahotas.labeled.relabel(labeled)
plt.title('After filtering and relabeling, there are {} nuclei left.'.format(n_left))
plt.imshow(relabeled)
first_points, last_points = (bind(x) for x in [first_point, last_point])
end_points[first_points],end_points[last_points] = (True, True)
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]
# mh.imsave('/tmp/mask.tif', mask)
grad_x = np.gradient(sub_tile)[0]
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]:
first_points, last_points = (bind(x) for x in [first_point, last_point])
end_points[first_points],end_points[last_points] = (True, True)
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]