def calculate_vi_ri_ari(result, gt): # false merges(缺失), false splits(划痕) merger_error, split_error = ev.split_vi(result, gt) vi = merger_error + split_error ri = ev.rand_index(result, gt) adjust_ri = ev.adj_rand_index(result, gt) return {'vi':vi,'ri':ri,'adjust_ri':adjust_ri, 'merger_error':merger_error, 'split_error':split_error}
def get_ari(pred: np.ndarray, mask: np.ndarray, bg_value: int = 0) -> float: """ Adjusted rand index Implemented by gala (https://github.com/janelia-flyem/gala.) """ label_pred, num_pred = label(pred, connectivity=1, background=bg_value, return_num=True) label_mask, num_mask = label(mask, connectivity=1, background=bg_value, return_num=True) value = ev.adj_rand_index(label_pred, label_mask) return value
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()) ws_train = ws_train.astype('int64') print "Training watershed complete" print "Watershed train (VI, ARI)" vi_ws_train = ev.split_vi(ws_train, gt_train), ari_ws_train = ev.adj_rand_index(ws_train, gt_train) print vi_ws_train print ari_ws_train scipy.io.savemat('trainWS.mat', mdict={'ws_train': ws_train}) scipy.io.savemat('trainMembrane.mat', mdict={'membraneTrain': membraneTrain}) # create a feature manager fc = features.base.Composite(children=[ features.moments.Manager(), features.histogram.Manager(25, 0, 1, [0.1, 0.5, 0.9]), features.graph.Manager(), features.contact.Manager([0.1, 0.5, 0.9]) ]) #feature_manager=features.base.Composite(children=[features.moments.Manager])
# print "Running watershed" # Now we want to separate the two objects in image # Generate the markers as local maxima of the distance to the background distance = ndi.distance_transform_edt(foreground) distance_float = rescale_intensity(distance, in_range='image', out_range='float') # Changes local mas peak paramater for peakPrint in peakArr: local_maxi = peak_local_max(distance, indices=False, footprint=np.ones( (peakPrint, peakPrint)), labels=foreground) markers = ndi.label(local_maxi)[0] #[0] labels = watershed(foreground, markers, mask=foreground) ######## Get the watershed evaluation split_eval = ev.split_vi(labels, membrane_ground[index]) adjusted_rand = ev.adj_rand_index(labels, membrane_ground[index]) fm = ev.fm_index(labels, membrane_ground[index]) outFile.write( str(index) + "_" + str(foot) + "_" + str(filt) + "_" + str(peakPrint) + " " + str(split_eval) + " " + str(adjusted_rand) + " " + str(fm) + "\n")
print ws_test.dtype ws_test = ws_test.astype('int64') print "unique labels in ws:",np.unique(ws_test).size ws_test = optimized.despeckle_watershed(ws_test) print "unique labels after despeckling:",np.unique(ws_test).size ws_test, _, _ = evaluate.relabel_from_one(ws_test) if ws_test.min() < 1: ws_test += (1-ws_test.min()) print "Testing watershed complete" print "Watershed test (VI, ARI)" vi_ws_test = ev.split_vi(ws_test, gt_test), ari_ws_test = ev.adj_rand_index(ws_test, gt_test) print vi_ws_test print ari_ws_test scipy.io.savemat('testWS.mat', mdict={'ws_test':ws_test}) print 'Time Elapsed So Far: ' + str(time.time()-start) print 'applying classifier...' g_test = agglo.Rag(ws_test, membraneTest, learned_policy, feature_manager=fc) print 'choosing best operating point...' g_test.agglomerate(0.5) # best expected segmentation segtestGala = g_test.get_segmentation() print "Completed Gala Run" # gala allows implementation of other agglomerative algorithms, including
threshold = threshold_otsu(img_med) binary_img = img_med > threshold # make image into binary_img and invert it image = binary_img foreground = 1 - image ######## # Watershed segmentation ######## # print "Running watershed" # Now we want to separate the two objects in image # Generate the markers as local maxima of the distance to the background distance = ndi.distance_transform_edt(foreground) distance_float = rescale_intensity(distance, in_range='image', out_range='float') # Changes local mas peak paramater for peakPrint in peakArr: local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((peakPrint, peakPrint)), labels=foreground) markers = ndi.label(local_maxi)[0] #[0] labels = watershed(foreground, markers, mask=foreground) ######## Get the watershed evaluation split_eval = ev.split_vi(labels, membrane_ground[index]) adjusted_rand = ev.adj_rand_index(labels, membrane_ground[index]) fm = ev.fm_index(labels, membrane_ground[index]) outFile.write(str(index) + "_" + str(foot) + "_" + str(filt) + "_" + str(peakPrint) + " " + str(split_eval) + " " + str(adjusted_rand) + " " + str(fm) + "\n")