def split(image, prob, segmentation, n=1, min_pixels=100): ''' ''' ugly_segmentation = np.array(segmentation) hist = Util.get_histogram(ugly_segmentation.astype(np.uint64)) labels = len(hist) for l in range(labels): if l == 0: continue for s in range(n): binary_mask = Util.threshold(ugly_segmentation, l) splitted_label, border = Patch.split_label(image, binary_mask) # check if splitted_label is large enough if len(splitted_label[splitted_label == 1]) < min_pixels: continue ugly_segmentation[splitted_label == 1] = ugly_segmentation.max() + 1 return ugly_segmentation
def generate_split_error(image, prob, segmentation, n=3): hist = Util.get_histogram(segmentation.astype(np.uint64)) labels = range(1, len(hist)) # do not include zeros np.random.shuffle(labels) for l in labels: binary_mask = Util.threshold(segmentation, l) labeled_parts = skimage.measure.label(binary_mask) labeled_parts += 1 # avoid the 0 labeled_parts[binary_mask == 0] = 0 labeled_parts, no_labeled_parts = mh.labeled.relabel(labeled_parts) for i in range(n): for part in range(1, no_labeled_parts + 1): binary_part = Util.threshold(labeled_parts, part) split_binary_mask, split_isolated_border = Patch.split_label( image, binary_part) if split_binary_mask.max() == 0: # the strange err (label too small or sth) print 'Caught empty..' continue patches = Patch.analyze_border(image, prob, split_binary_mask, binary_mask.invert(), split_isolated_border) for s in patches: yield s yield Patch.fliplr(s) yield Patch.flipud(s) yield Patch.rotate(s, 90) yield Patch.rotate(s, 180) yield Patch.rotate(s, 270)