def sem_label_to_inst_masks(sem_label, as_list=True, order='instances_first'): """Convert semantic label to instance masks using connected components. Args: label: Possible options: 'train', 'val', 'test', or 'val_predpaths' as_list: Return as list or np array? order: Return np array as [instance count, H, W] or [H, W, instance count]? Returns: masks: [instance count, H, W]/[H, W, instance count] np array or list([H,W]) """ # Split semantic label into instance masks using connected components if len(sem_label.shape) == 3 and sem_label.shape[2] == 1: lab_img = sk_label(np.squeeze(sem_label, -1) > 128) else: lab_img = sk_label(sem_label > 128) # Build list of instance masks masks = [(lab_img == i).astype('uint8') * 255 for i in range(1, lab_img.max() + 1)] if as_list: return masks else: if order == 'instances_first': return np.asarray(masks) else: return np.rollaxis(np.asarray(masks), 0, 3)
def task1_post_process(y_prediction, threshold=0.5, gauss_sigma=0.): for im_index in range(y_prediction.shape[0]): # smooth image by Gaussian filtering if gauss_sigma > 0.: y_prediction[im_index] = gaussian_filter(input=y_prediction[im_index], sigma=gauss_sigma) thresholded_image = y_prediction[im_index] > threshold # find largest connected component labels, num_labels = sk_label(thresholded_image, return_num=True) max_label_idx = -1 max_size = 0 for label_idx in range(0, num_labels + 1): if np.sum(thresholded_image[labels == label_idx]) == 0: continue current_size = np.sum(labels == label_idx) if current_size > max_size: max_size = current_size max_label_idx = label_idx if max_label_idx > -1: y_prediction[im_index] = labels == max_label_idx else: # no predicted pixels found y_prediction[im_index] = y_prediction[im_index] * 0 return y_prediction
def create_branches(in_img, k_nn): # input: skeleton image 0, 1 a = np.where(in_img != 0, 1, 0) a_nn = nearest_neighbours(in_img, k_nn) intersections = np.where(a_nn >= 3, 1, 0) a_no_branches = np.where(intersections, 0, 1) * a # mask at intersections a_keep = sk_label(a_no_branches) # remove branches return a_keep #((a_keep + intersections) > 0).astype(np.uint8)
def remove_small_blobs(self,predicted_label): """ Removes small blobs in the prediction predicted_label: Predicted labelmap as numpy array """ if not np.max(predicted_label) == 1: #TODO: Check the multiclass setting later label = np.zeros(predicted_label.shape) for c in range(1,predicted_label.max+1): one_hot = np.zeros(predicted_label.shape) one_hot[predicted_label == c] = 1 label_blob = sk_label(one_hot, neighbors = 8, background = None) label_one_hot = remove_small_objects(label_blob, min_size=500) label[label_one_hot>0] == c else: label_blob = sk_label(predicted_label, neighbors = 8, background = None) label = remove_small_objects(label_blob, min_size=500)>0 return label.astype(np.float64)
def label(mask: np.ndarray, background: int = 0, connectivity: int = 2, meta: dict = None, **kwargs): label_image = sk_label(mask, background=background, connectivity=connectivity) if meta is not None and 'yrange_ex' in meta.keys( ) and 'xrange_ex' in meta.keys(): label_image = exclude_regions(label_image, y_range_ex=meta['yrange_ex'], x_range_ex=meta['xrange_ex']) return label_image
def label_sort(in_img, cutoff=0.01): total_cnt = np.sum(in_img > 0) lab_img = sk_label(in_img) new_image = np.zeros_like(lab_img) remap_index = [] for k in np.unique(lab_img[lab_img > 0]): cnt = np.sum(lab_img == k) # get area of labelled object if cnt > total_cnt * cutoff: remap_index += [(k, cnt)] sorted_index = sorted( remap_index, key=lambda x: -x[1]) # reverse sort - largest is first for new_idx, (old_idx, idx_count) in enumerate(sorted_index, 1): #enumerate starting at id 1 new_image[lab_img == old_idx] = new_idx return new_image