def label_image_bounds_iterator(self, key=None, labellist=None, background=None, area=None, more_keys=None, maskvalue=0, value=0, forcecontinue=False): for lbl, lblim in self.label_image_iterator(key=key, background=background, labellist=labellist, area=area): # self.logging('self.amax() = {}', self.amax()) try: bounds = lib.find_bounding_rect(lblim, s_=True) lblim = lib.crop_bounding_rect(lblim, bounds) if more_keys is not None: more_ims = self.crop_bounding_rect(bounds, keys=more_keys, return_only=True) more_ims.mask_image(maskvalue=maskvalue, value=value, keys=more_keys, indict={'lblim': lblim}) yield [lbl, lblim, more_ims, bounds] else: yield [lbl, lblim, bounds] except: if forcecontinue: self.errprint('Warning: Something went wrong in label {}, jumping to next label'.format(lbl), traceback) continue else: raise
def shortest_paths_wrapper(labelim, gt_im, dt_im, bc_im, lbl, kl, k, params, for_class=True, correspondence={}, logger=None): print 'Wrapper called...' # Create an image that contains only the one object lblim = np.zeros(labelim.shape, dtype=np.uint16) lblim[labelim == lbl] = lbl # Get the region of the one object bounds = lib.find_bounding_rect(lblim, s_=True) # Crop the label image lblim = lib.crop_bounding_rect(lblim, bounds) # Crop the gt as well gt_im = lib.crop_bounding_rect(gt_im, bounds=bounds) # Crop and mask the distance transform dt_im = lib.crop_bounding_rect(dt_im, bounds=bounds) dt_im[lblim == 0] = 0 # Crop and mask border contacts bc_im = lib.crop_bounding_rect(bc_im, bounds=bounds) bc_im[lblim == 0] = 0 # Done: Check for correctness # Compute all paths within this object which start and end in different # gt-objects # Supply the correspondence table to this function and only compute a path # if the respective correspondence is not found return shortest_paths( params['penaltypower'], bounds, lbl, kl + [k], gt_im, dt_im, bc_im, for_class=for_class, correspondence=correspondence, avoid_duplicates=params['avoid_duplicates'], max_paths_per_object=params['max_paths_per_object'], max_paths_per_object_seed=params['max_paths_per_object_seed'], yield_in_bounds=True, return_pathim=False, minimum_alternative_label_count=params[ 'minimum_alternative_label_count'], logger=logger)
def find_border_centroids(hfp, keys, areas, largeobjkey, disttransfkey, resultkey): for k, bounds in keys.iteritems(): # bounds = (shp[0],) * 2 for lbl, lblim in hfp['faces', largeobjkey].label_image_iterator(key=k, background=0, area=areas[k]): hfp.logging('---\nLabel {} found in image {}', lbl, k) # Avoid very small artifacts lblim = morphology.opening(lblim) # Connected component analysis to detect when a label touches the border multiple times conncomp = vigra.analysis.labelImageWithBackground(lblim.astype(np.uint32), neighborhood=8, background_value=0) for l in np.unique(conncomp): # Ignore background if l == 0: continue # Get the current label object curobj = conncomp == l # Get disttancetransf of the object curdist = np.array(hfp['faces', disttransfkey, k]) curdist[curobj == False] = 0 # Detect the global maximum of this object amax = np.amax(curdist) curdist[curdist < amax] = 0 curdist[curdist > 0] = lbl # Only one pixel is allowed to be selected bds = lib.find_bounding_rect(curdist) centroid = (int((bds[1][0] + bds[1][1]-1) / 2), int((bds[0][0] + bds[0][1]-1) / 2)) # Now translate the calculated centroid to the position within the orignial 3D volume centroidm = (centroid[0] - bounds, centroid[1] - bounds) # hfp.logging('centroidxy = {}', centroidm) # Set the pixel try: if centroidm[0] < 0 or centroidm[1] < 0: raise IndexError else: if k == 'xyf': hfp[resultkey][centroidm[0], centroidm[1], 0] = lbl elif k == 'xyb': hfp[resultkey][centroidm[0], centroidm[1], -1] = lbl elif k == 'xzf': hfp[resultkey][centroidm[0], 0, centroidm[1]] = lbl elif k == 'xzb': hfp[resultkey][centroidm[0], -1, centroidm[1]] = lbl elif k == 'yzf': hfp[resultkey][0, centroidm[0], centroidm[1]] = lbl elif k == 'yzb': hfp[resultkey][-1, centroidm[0], centroidm[1]] = lbl except IndexError: pass
def label_image_bounds_iterator(self, key=None, labellist=None, background=None, area=None, more_keys=None, maskvalue=0, value=0, forcecontinue=False): for lbl, lblim in self.label_image_iterator(key=key, background=background, labellist=labellist, area=area): # self.logging('self.amax() = {}', self.amax()) try: bounds = lib.find_bounding_rect(lblim, s_=True) lblim = lib.crop_bounding_rect(lblim, bounds) if more_keys is not None: more_ims = self.crop_bounding_rect(bounds, keys=more_keys, return_only=True) more_ims.mask_image(maskvalue=maskvalue, value=value, keys=more_keys, indict={'lblim': lblim}) yield [lbl, lblim, more_ims, bounds] else: yield [lbl, lblim, bounds] except: if forcecontinue: self.errprint( 'Warning: Something went wrong in label {}, jumping to next label' .format(lbl), traceback) continue else: raise
# Load ground truth seg_image = np.array( hp(filepath=gt_path + gt_file, nodata=True, skeys=[gt_skey])[gt_skey]) gt_labels = np.unique(lib.getvaluesfromcoords(seg_image, path)) t_seg_image = np.array(seg_image) for l in gt_labels: t_seg_image[t_seg_image == l] = 0 seg_image[t_seg_image > 0] = 0 t_seg_image = None else: sys.exit() if crop is None: crop = lib.find_bounding_rect(seg_image, s_=True) print 'crop = {}'.format(crop) seg_image = lib.crop_bounding_rect(seg_image, crop) path = np.swapaxes(path, 0, 1) path[0] = path[0] - crop[0].start path[1] = path[1] - crop[1].start path[2] = path[2] - crop[2].start # Load raw image raw_image = np.array( hp(filepath=raw_path + raw_file, nodata=True, skeys=[raw_skey])[raw_skey]) raw_image = lib.crop_bounding_rect(raw_image, crop)
def find_border_centroids(ipl, faces, key, facesinfo, facesd, resultkey, resultshp): """ :param ipl: the result is stored here using resultkey :param faces: ipl containing faces as returned by compute_faces :param key: key of the image in ipl :param facesinfo: ipl containing facesinfo as returned by compute_faces :param facesd: ipl containing the faces of the distance transform as returned by compute_faces :param resultkey: :return: """ ipl[resultkey, key] = np.zeros(resultshp) for k, startpoint in facesinfo[key, 'startpoints'].iteritems(): # bounds = (shp[0],) * 2 for lbl, lblim in faces[key].label_image_iterator(key=k, background=0, area=facesinfo[key, 'areas', k]): ipl.logging('---\nLabel {} found in image {}', lbl, k) # Avoid very small artifacts lblim = morphology.opening(lblim) # Connected component analysis to detect when a label touches the border multiple times conncomp = vigra.analysis.labelImageWithBackground(lblim.astype(np.uint32), neighborhood=8, background_value=0) for l in np.unique(conncomp): # Ignore background if l == 0: continue # Get the current label object curobj = conncomp == l # Get disttancetransf of the object curdist = np.array(facesd[key, k]) curdist[curobj == False] = 0 # Detect the global maximum of this object amax = np.amax(curdist) curdist[curdist < amax] = 0 curdist[curdist > 0] = lbl # Only one pixel is allowed to be selected try: bds = lib.find_bounding_rect(curdist) except ValueError: # A value error is thrown when the current object is just one pixel in size # This can be ignored without ignoring relevant border contacts pass centroid = (int((bds[1][0] + bds[1][1]-1) / 2), int((bds[0][0] + bds[0][1]-1) / 2)) # Now translate the calculated centroid to the position within the orignial 3D volume centroidm = (centroid[0] - startpoint, centroid[1] - startpoint) # ipl.logging('centroidxy = {}', centroidm) # Set the pixel try: if centroidm[0] < 0 or centroidm[1] < 0: raise IndexError else: if k == 'xyf': ipl[resultkey, key][centroidm[0], centroidm[1], 0] = lbl elif k == 'xyb': ipl[resultkey, key][centroidm[0], centroidm[1], -1] = lbl elif k == 'xzf': ipl[resultkey, key][centroidm[0], 0, centroidm[1]] = lbl elif k == 'xzb': ipl[resultkey, key][centroidm[0], -1, centroidm[1]] = lbl elif k == 'yzf': ipl[resultkey, key][0, centroidm[0], centroidm[1]] = lbl elif k == 'yzb': ipl[resultkey, key][-1, centroidm[0], centroidm[1]] = lbl except IndexError: pass