def generate_subRegions(img, patchSize, region_patch_ratio, eraseMap, k, minSize, sigma, thresh=0, radius=220, centers = np.array([219.5, 219.5])): im = img[:, :, 0] F0, n_region = segment.segment_label(img, sigma, k, minSize) eraseLabels = set(list(F0[np.where(eraseMap == 1)].flatten())) region_patch_list = [[] for i in range(n_region)] for l in range(n_region): if l in eraseLabels: region_patch_list[l] = [] else: region_patch_centers = list(np.argwhere(F0 == l)) if len(region_patch_centers) == 0: continue region_values = im[np.where(F0 == l)] region_mean = region_values.mean() # print thresh if region_mean < thresh: region_patch_list[l] = [] else: hw = patchSize / 2 region_patch_gride = np.zeros((len(region_patch_centers), 4)) region_patch_gride[:, :2] = np.array(region_patch_centers) - hw region_patch_gride[:, 2:] = patchSize patch_list = list(region_patch_gride) for ii in range(len(region_patch_centers)): ll = patch_list[ii] if np.random.rand(1, )[0] < region_patch_ratio: if isWithinCircle(ll, centers, radius): region_patch_list[l].append(ll) return F0, region_patch_list, eraseLabels
def hierarchical_segmentation(I, k = 100, feature_mask = features.SimilarityMask(1, 1, 1, 1)): F0, n_region = segment.segment_label(I, 0.8, k, 100) adj_mat, A0 = _calc_adjacency_matrix(F0, n_region) feature_extractor = features.Features(I, F0, n_region) # stores list of regions sorted by their similarity S = _build_initial_similarity_set(A0, feature_extractor) # stores region label and its parent (empty if initial). R = {i : () for i in range(n_region)} A = [A0] # stores adjacency relation for each step F = [F0] # stores label image for each step # greedy hierarchical grouping loop while len(S): (s, (i, j)) = S.pop() t = feature_extractor.merge(i, j) # record merged region (larger region should come first) R[t] = (i, j) if feature_extractor.size[j] < feature_extractor.size[i] else (j, i) Ak = _new_adjacency_dict(A[-1], i, j, t) A.append(Ak) S = _merge_similarity_set(feature_extractor, Ak, S, i, j, t) F.append(_new_label_image(F[-1], i, j, t)) # bounding boxes for each hierarchy L = feature_extractor.bbox return (R, F, L)
def detect_regions(imgFile, eraseMap, k, minSize, sigma, thresh=0, imResize=None): img = skimage.io.imread(imgFile) if imResize is not None: img = imresize(img, (imResize, imResize)) if len(img.shape) == 2: img = skimage.color.gray2rgb(img) im = img[:, :, 0] F0, n_region = segment.segment_label(img, sigma, k, minSize) eraseLabels = set(list(F0[numpy.where(eraseMap == 1)].flatten())) filterout_labels = [] for l in range(n_region): region_values = im[np.where(F0 == l)] region_mean = region_values.mean() # print thresh if region_mean < thresh: filterout_labels.append(l) detect_regions = numpy.ones(F0.shape).astype(numpy.uint8) removeLabels = list(eraseLabels) + filterout_labels for l in removeLabels: detect_regions[np.where(F0 == l)] = 0 return detect_regions, F0, removeLabels
def hierarchical_segmentation(I, k=100, feature_mask=features.SimilarityMask(1, 1, 1, 1)): F0, n_region = segment.segment_label(I, 0.8, k, 100) adj_mat, A0 = _calc_adjacency_matrix(F0, n_region) feature_extractor = features.Features(I, F0, n_region) # stores list of regions sorted by their similarity S = _build_initial_similarity_set(A0, feature_extractor) # stores region label and its parent (empty if initial). R = {i: () for i in range(n_region)} A = [A0] # stores adjacency relation for each step F = [F0] # stores label image for each step # greedy hierarchical grouping loop while len(S): (s, (i, j)) = S.pop() t = feature_extractor.merge(i, j) # record merged region (larger region should come first) R[t] = ( i, j) if feature_extractor.size[j] < feature_extractor.size[i] else ( j, i) Ak = _new_adjacency_dict(A[-1], i, j, t) A.append(Ak) S = _merge_similarity_set(feature_extractor, Ak, S, i, j, t) F.append(_new_label_image(F[-1], i, j, t)) # bounding boxes for each hierarchy L = feature_extractor.bbox return (R, F, L)
def hierarchical_segmentation(I, k = 100, feature_mask = features.SimilarityMask(1, 1, 1, 1), eraseMap=None): # F0, n_region = segment.segment_label(I, 0.8, k, 100) F0, n_region = segment.segment_label(I, 0.5, k, 500) n_region = F0.max() + 1 # ++ calculate outside region labels if eraseMap is not None: eraseLabels = set(list(F0[numpy.where(eraseMap == 1)].flatten())) else: eraseLabels = [] adj_mat, A0 = _calc_adjacency_matrix(F0, n_region) feature_extractor = features.Features(I, F0, n_region) # stores list of regions sorted by their similarity S = _build_initial_similarity_set(A0, feature_extractor) # ++ pop out regions outside circle if eraseMap is not None: ii = len(S) - 1 while ii >= 0: if (S[ii][1][0] in eraseLabels) or (S[ii][1][1] in eraseLabels): S.pop(ii) ii -= 1 # stores region label and its parent (empty if initial). R = {i : () for i in range(n_region)} A = [A0] # stores adjacency relation for each step F = [F0] # stores label image for each step # greedy hierarchical grouping loop while len(S): (s, (i, j)) = S.pop() t = feature_extractor.merge(i, j) # record merged region (larger region should come first) R[t] = (i, j) if feature_extractor.size[j] < feature_extractor.size[i] else (j, i) Ak = _new_adjacency_dict(A[-1], i, j, t) A.append(Ak) S = _merge_similarity_set(feature_extractor, Ak, S, i, j, t) # ++ pop out regions outside circle if eraseMap is not None: ii = len(S) - 1 while ii >= 0: if (S[ii][1][0] in eraseLabels) or (S[ii][1][1] in eraseLabels): S.pop(ii) ii -= 1 F.append(_new_label_image(F[-1], i, j, t)) # bounding boxes for each hierarchy L = feature_extractor.bbox L_regions = {} for r, l in R.iteritems(): if r < n_region: L_regions[r] = [r] else: ll = [] if l[0] >= n_region: ll = ll + L_regions[l[0]] else: ll.append(l[0]) if l[1] >= n_region: ll = ll + L_regions[l[1]] else: ll.append(l[1]) L_regions[r] = ll return (R, F, L, L_regions, eraseLabels)
def generate_subRegions(imgFileOrImg, patchSize, region_patch_ratio, eraseMap, k, minSize, sigma, radius=220, centers=np.array([219.5, 219.5]), thresh=None, isSaveDetection=False, diffResolution=False, returnFilteroutLabels=False): if isinstance(imgFileOrImg, str): img = skimage.io.imread(imgFileOrImg) if len(img.shape) == 2: img = skimage.color.gray2rgb(img) else: img = imgFileOrImg # im = rgb2gray(img) # print im.max(), im.min() im = img[:, :, 0] if thresh is None: thresh = 0 F0, n_region = segment.segment_label(img, sigma, k, minSize) eraseLabels = set(list(F0[numpy.where(eraseMap == 1)].flatten())) # plt.imshow(eraseMap) # plt.show() # print eraseLabels region_patch_list = [[] for i in range(n_region)] filterout_labels = [] for l in range(n_region): if l in eraseLabels: region_patch_list[l] = [] else: region_patch_centers = list(np.argwhere(F0 == l)) if len(region_patch_centers) == 0: continue region_values = im[np.where(F0 == l)] region_mean = region_values.mean() # print thresh if region_mean < thresh: region_patch_list[l] = [] filterout_labels.append(l) else: hw = patchSize / 2 region_patch_gride = np.zeros((len(region_patch_centers), 4)) if not diffResolution: region_patch_gride[:, :2] = np.array( region_patch_centers) - hw region_patch_gride[:, 2:] = patchSize patch_list = list(region_patch_gride) for ii in range(len(region_patch_centers)): if not diffResolution: ll = patch_list[ii] if np.random.rand(1, )[0] < region_patch_ratio: if diffResolution: patchSize = np.array( esg.centerArr2sizeList( region_patch_centers[ii])) ll = np.zeros((1, 4)) ll[:, :2] = region_patch_centers[ii] ll[:, 2:] = patchSize ll = list(ll)[0] if esg.isWithinCircle(ll, centers, radius): region_patch_list[l].append(ll) if isSaveDetection and (thresh != 0): folder = '../../Data/Results/regionDetection/' alpha = 0.6 colors = numpy.random.randint(0, 255, (n_region, 3)) print eraseLabels for e in eraseLabels: colors[e] = 0 color_regions_before = colors[F0] result_before = (color_regions_before * alpha + img * (1. - alpha)).astype(numpy.uint8) print filterout_labels for e in filterout_labels: colors[e] = 0 color_regions_after = colors[F0] result_after = (color_regions_after * alpha + img * (1. - alpha)).astype(numpy.uint8) imsave(folder + 'before.jpg', result_before) imsave(folder + 'after.jpg', result_after) if returnFilteroutLabels: return F0, region_patch_list, eraseLabels, filterout_labels else: return F0, region_patch_list, eraseLabels