def test_watershed_seeded(): seeds_bool = (landscape == 0) seeds_unique = nd.label(seeds_bool)[0] seeded_watershed_result = np.array([1,1,1,1,1,0,2,2,2,0,3,3]) seeded_watershed1 = morpho.watershed(landscape, seeds_bool, dams=True) seeded_watershed2 = morpho.watershed(landscape, seeds_unique, dams=True) assert_array_equal(seeded_watershed1, seeded_watershed_result) assert_array_equal(seeded_watershed2, seeded_watershed_result)
def main(args): ws_id = generate_ws_id(args.min_seed_size, args.connectivity, args.smooth_thresh, args.keep_speckles, args.override_skimage) paths = get_paths("XX", args.traintest, args.size, args.volume_id, "idsia", "XX", "XX", "XX", ws_id) stack = read_h5(paths["hypercubes"], args.h5_key) if args.dry_run: stack = stack[:2,:,:] if not args.dont_invert: stack[...] = invert_gray(stack) ws = np.zeros(stack.shape) cur_max = 0 for ii in range(stack.shape[0]): print ii ws[ii,:,:] = morpho.watershed(stack[ii,:,:], minimum_seed_size=args.min_seed_size, connectivity=args.connectivity, smooth_thresh=args.smooth_thresh, override_skimage=args.override_skimage) + cur_max cur_max = ws[ii,:,:].max() ws = ws.astype('int64') print "unique labels in ws:",np.unique(ws).size if not args.keep_speckles: ws = agglo.despeckle_watershed(ws) print "unique labels after despeckling:",np.unique(ws).size ws, _, _ = evaluate.relabel_from_one(ws) if ws.min() < 1: ws += (1-ws.min()) write_h5(ws, paths["watersheds"], dry=args.dry_run) if args.no_quarters or (args.size != "whole"): return for row_start, row_end, col_start, col_end, frame_start, frame_end, label, size in fractions: path = generate_path(args.traintest, size, label, ws_id) write_h5(ws[frame_start:frame_end, row_start:row_end, col_start:col_end], path)
def test_watershed_saddle_basin(): saddle_landscape = np.array([[0,0,3],[2,1,2],[0,0,3]]) saddle_result = np.array([[1,1,1],[0,0,0],[2,2,2]]) saddle_ws = morpho.watershed(saddle_landscape, dams=True) assert_array_equal(saddle_ws, saddle_result)
def test_watershed_seeded_nodams(): seeds_bool = landscape==0 seeded_nodam_ws_result = np.array([1,1,1,1,1,1,2,2,2,3,3,3]) seeded_nodam_ws = morpho.watershed(landscape, seeds=seeds_bool, override_skimage=True, dams=False) assert_array_equal(seeded_nodam_ws, seeded_nodam_ws_result)
def test_watershed_nodams(): nodam_watershed_result = np.array([1,1,1,2,2,2,3,3,3,4,4,4]) nodam_watershed = morpho.watershed(landscape, dams=False) assert_array_equal(nodam_watershed, nodam_watershed_result)
def test_watershed(): regular_watershed_result = np.array([1,1,1,0,2,0,3,3,3,0,4,4]) regular_watershed = morpho.watershed(landscape, dams=True) assert_array_equal(regular_watershed, regular_watershed_result)
def test_watershed_images(): wss = [morpho.watershed(probs[i], dams=True) for i in range(3)] + \ [morpho.watershed(probs[3], dams=False)] for i, (ws, res) in enumerate(zip(wss, results)): assert_array_equal(ws, res, 'Image watershed test %i failed.' % i)
def test_watershed_images(): wss = [morpho.watershed(probs[i], dams=(i == 0)) for i in range(2)] for i, (ws, res) in enumerate(zip(wss, results)): yield (assert_array_equal, ws, res, 'Image watershed test %i failed.' % i)
# Do watershed min_seed_size = 2 connectivity = 2 smooth_thresh = 0.02 override = 0 #membraneTrain = membraneTrain.astype('float64')/255.0 ws_train = np.zeros(membraneTrain.shape) cur_max = 0 for ii in range(membraneTrain.shape[0]): print ii ws_train[ii, :, :] = morpho.watershed( 1 - membraneTrain[ii, :, :], connectivity=connectivity, smooth_thresh=smooth_thresh, override_skimage=override, minimum_seed_size=min_seed_size) + cur_max cur_max = ws_train[ii, :, :].max() ws_train = ws_train.astype('int64') print "unique labels in ws:", np.unique(ws_train).size ws_train = optimized.despeckle_watershed(ws_train) print "unique labels after despeckling:", np.unique(ws_train).size ws_train, _, _ = evaluate.relabel_from_one(ws_train) if ws_train.min() < 1: ws_train += (1 - ws_train.min())
from skimage.morphology import closing from skimage.morphology import binary_dilation from skimage.morphology import binary_erosion from skimage.filters.rank import threshold import matplotlib.pyplot as plt # Gather images from nXp_data.npy file print "Getting images" membrane_images = np.load('nXp_data.npy') pre_image = 255 * membrane_images[0] min_seed_size = 5 #2 connectivity = 2 #2 smooth_thresh = 0.02 #0.02 ws_train = morpho.watershed(pre_image, connectivity=connectivity, smooth_thresh=smooth_thresh, override_skimage=1,minimum_seed_size=min_seed_size) + 1 #ws_train = ws_train fig, axes = plt.subplots(ncols=1, figsize=(8, 2.7), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'}) ax0 = axes ax0.imshow(ws_train)#, cmap=plt.cm.spectral, interpolation='nearest') ax0.set_title('Overlapping objects') fig.subplots_adjust(hspace=0.01, wspace=0.01, top=0.9, bottom=0, left=0, right=1) plt.show()