def _calc_grid(self) -> None: # converting the placement grid to our own kind of grid # cleaning the grid and then searching for 2x2 patterned regions grid = binary_fill_holes(self.placement_arr).astype(int) # for our grid, mineral walls are considered as a barrier between regions for point in self.resource_blockers: grid[int(point[1])][int(point[0])] = 0 for n in point.neighbors4: point_ = Point2((n.rounded[0], n.rounded[1])) if point_[0] < grid.shape[1] and point_[1] < grid.shape[0]: grid[int(point_[1])][int(point_[0])] = 0 s = generate_binary_structure(BINARY_STRUCTURE, BINARY_STRUCTURE) labeled_array, num_features = ndlabel(grid, structure=s) # for some operations the array must have same numbers or rows and cols, adding # zeros to fix that # rows, cols = labeled_array.shape # self.region_grid = np.append(labeled_array, np.zeros((abs(cols - rows), cols)), axis=0) self.region_grid = labeled_array.astype(int) self.regions_labels = np.unique(self.region_grid) vb_points = self._vision_blockers # some maps has no vision blockers if len(vb_points): vision_blockers_indices = self.points_to_indices(vb_points) vision_blockers_array = np.zeros(self.region_grid.shape, dtype="int") vision_blockers_array[vision_blockers_indices] = 1 vb_labeled_array, vb_num_features = ndlabel(vision_blockers_array) self.vision_blockers_grid = vb_labeled_array self.vision_blockers_labels = np.unique(vb_labeled_array)
def thresholdCenterlines(nms, tLow=0.012, tHigh=0.12, bimodal=True): """ Uses a continuity-preserving hysteresis thresholding to classify centerlines. Inputs: nms -- Non-maxima suppressed singularity index response Keyword Arguments: bimodal -- true if the areas of rivers in the image are sufficiently large that the distribution of ψ is bimodal tLow -- lower threshold (automatically set if bimodal=True) tHigh -- higher threshold (automatically set if bimodal=True) Returns: centerlines -- a binary matrix that indicates centerline locations """ if bimodal: #Otsu's algorithm nms = preprocess.double2im(nms, 'uint8') tHigh,_ = cv2.threshold(nms, nms.min(), nms.max(), cv2.THRESH_OTSU) tLow = tHigh * 0.1 strongCenterline = nms >= tHigh centerlineCandidate = nms >= tLow # Find connected components that has at least one strong centerline pixel strel = np.ones((3, 3), dtype=bool) cclabels, numcc = ndlabel(centerlineCandidate, strel) sumstrong = ndsum(strongCenterline, cclabels, list(range(1, numcc+1))) centerlines = np.hstack((0, sumstrong > 0)).astype('bool') centerlines = centerlines[cclabels] return centerlines
def get_largest_component(lab): """Get largest connected component. Given a multi-class labeling, leave the largest connected component for each class. """ classes = np.unique(lab) classes = np.delete(classes, np.argwhere(classes == 0)) pruned_lab = np.zeros(lab.shape, dtype=lab.dtype) for c in classes: print("Finding largest connected component in class {}".format(c)) # make it black and white bw = np.zeros(lab.shape) bw[lab == c] = 1 # 26 connectivity for 3D images conn = np.ones((3, 3, 3)) # clustered_lab.shape = bw.shape clustered_lab, n_comps = ndlabel(bw, conn) # sort components by volume from smallest to largest (skip zero) comp_volumes = [np.sum(clustered_lab == i) for i in range(1, n_comps)] comp_labels = np.argsort(comp_volumes) # pick component with largest volume (not counting background) largest_comp_label = 1 + comp_labels[-1] # keep the component in the output pruned_lab[clustered_lab == largest_comp_label] = c return pruned_lab
def thresholdCenterlines(nms, tLow=0.012, tHigh=0.12, bimodal=True): """ Uses a continuity-preserving hysteresis thresholding to classify centerlines. Inputs: nms -- Non-maxima suppressed singularity index response Keyword Arguments: bimodal -- true if the areas of rivers in the image are sufficiently large that the distribution of ψ is bimodal tLow -- lower threshold (automatically set if bimodal=True) tHigh -- higher threshold (automatically set if bimodal=True) Returns: centerlines -- a binary matrix that indicates centerline locations """ if bimodal: #Otsu's algorithm nms = preprocess.double2im(nms, 'uint8') tHigh,_ = cv2.threshold(nms, nms.min(), nms.max(), cv2.THRESH_OTSU) tLow = tHigh * 0.1 strongCenterline = nms >= tHigh centerlineCandidate = nms >= tLow # Find connected components that has at least one strong centerline pixel strel = np.ones((3, 3), dtype=bool) cclabels, numcc = ndlabel(centerlineCandidate, strel) sumstrong = ndsum(strongCenterline, cclabels, range(1, numcc+1)) centerlines = np.hstack((0, sumstrong > 0)).astype('bool') centerlines = centerlines[cclabels] return centerlines
def biggest_region_3D(array): if len(array.shape) == 4: im_np = np.squeeze(array) else: im_np = array struct = np.full((3, 3, 3), 1) c = 0 maxn = im_np.max() arr = np.where(im_np >= (maxn - 0.2), 1, 0) lab, num_reg = ndlabel(arr, structure=struct) h = np.zeros(num_reg + 1) for i in range(num_reg): z = np.where(lab == (i + 1), 1, 0) h[i + 1] = z.sum() if h[i + 1] == h.max(): c = i + 1 lab = np.where(lab == c, 1, 0) return lab