def _execute_GRAPHCUT(self, roi, result): data = self.Input(roi.start, roi.stop).wait() data = vigra.taggedView(data, self.Input.meta.axistags) data_zyx = data[0, ..., 0] beta = self.GraphcutBeta.value ft = self.FinalThreshold.value # The segmentGC() function will implicitly threshold at 0.5, # but we want to respect the user's FinalThreshold setting. # Here, we scale from input pixels --> gc potentials in the following way: # # 0.0..FT --> 0.0..0.5 # 0.5..FT --> 0.5..1.0 # # For instance, input pixels that match the user's FT exactly will map to 0.5, # and graphcut will place them on the threshold border. above_threshold_mask = (data_zyx >= ft) below_threshold_mask = ~above_threshold_mask data_zyx[below_threshold_mask] *= 0.5 / ft data_zyx[above_threshold_mask] = 0.5 + ( data_zyx[above_threshold_mask] - ft) / (1 - ft) binary_seg_zyx = segmentGC(data_zyx, beta).astype(np.uint8) del data_zyx vigra.analysis.labelMultiArrayWithBackground(binary_seg_zyx, out=result[0, ..., 0])
def processSingleObject(i): logger.debug("processing object {}".format(i)) # maxs are inclusive, so we need to add 1 xmin = max(mins[i][0] - margin[0], 0) ymin = max(mins[i][1] - margin[1], 0) zmin = max(mins[i][2] - margin[2], 0) xmax = min(maxs[i][0] + margin[0] + 1, cc.shape[0]) ymax = min(maxs[i][1] + margin[1] + 1, cc.shape[1]) zmax = min(maxs[i][2] + margin[2] + 1, cc.shape[2]) ccbox = cc[xmin:xmax, ymin:ymax, zmin:zmax] resbox = resultXYZ[xmin:xmax, ymin:ymax, zmin:zmax] nVoxels = ccbox.size if nVoxels > MAXBOXSIZE: #problem too large to run graph cut, assign to seed logger.warn("Object {} too large for graph cut.".format(i)) resbox[ccbox == i] = 1 return probbox = pred[xmin:xmax, ymin:ymax, zmin:zmax] gcsegm = segmentGC(probbox, beta) gcsegm = vigra.taggedView(gcsegm, axistags='xyz') ccsegm = vigra.analysis.labelVolumeWithBackground( gcsegm.astype(np.uint8)) # Extended bboxes of different objects might overlap. # To avoid conflicting segmentations, we find all connected # components in the results and only take the one, which # overlaps with the object "core" or "seed", defined by the # pre-thresholding seed = ccbox == i filtered = seed * ccsegm passed = vigra.analysis.unique(filtered) passed.sort() assert len(passed.shape) == 1 if passed.size > 2: logger.warn("ambiguous label assignment for region {}".format( (xmin, xmax, ymin, ymax, zmin, zmax))) resbox[ccbox == i] = 1 elif passed.size <= 1: logger.warn("box {} segmented out with beta {}".format( i, beta)) else: # assign to the overlap region label = passed[1] # 0 is background resbox[ccsegm == label] = 1
def processSingleObject(i): logger.debug("processing object {}".format(i)) # maxs are inclusive, so we need to add 1 xmin = max(mins[i][0]-margin[0], 0) ymin = max(mins[i][1]-margin[1], 0) zmin = max(mins[i][2]-margin[2], 0) xmax = min(maxs[i][0]+margin[0]+1, cc.shape[0]) ymax = min(maxs[i][1]+margin[1]+1, cc.shape[1]) zmax = min(maxs[i][2]+margin[2]+1, cc.shape[2]) ccbox = cc[xmin:xmax, ymin:ymax, zmin:zmax] resbox = resultXYZ[xmin:xmax, ymin:ymax, zmin:zmax] nVoxels = ccbox.size if nVoxels > MAXBOXSIZE: #problem too large to run graph cut, assign to seed logger.warn("Object {} too large for graph cut.".format(i)) resbox[ccbox == i] = 1 return probbox = pred[xmin:xmax, ymin:ymax, zmin:zmax] gcsegm = segmentGC(probbox, beta) gcsegm = vigra.taggedView(gcsegm, axistags='xyz') ccsegm = vigra.analysis.labelVolumeWithBackground( gcsegm.astype(np.uint8)) # Extended bboxes of different objects might overlap. # To avoid conflicting segmentations, we find all connected # components in the results and only take the one, which # overlaps with the object "core" or "seed", defined by the # pre-thresholding seed = ccbox == i filtered = seed*ccsegm passed = vigra.analysis.unique(filtered.astype(np.uint32)) assert len(passed.shape) == 1 if passed.size > 2: logger.warn("ambiguous label assignment for region {}".format( (xmin, xmax, ymin, ymax, zmin, zmax))) resbox[ccbox == i] = 1 elif passed.size <= 1: logger.warn( "box {} segmented out with beta {}".format(i, beta)) else: # assign to the overlap region label = passed[1] # 0 is background resbox[ccsegm == label] = 1