def window_histogram(f,pl, Bc, bins = []): """ This function computes the histogram for each point in pl in a window defined by Bc. Input: f -> int32:Input image. pl -> int32: points list nx2 or nx3 array Bc -> bool: Structuring element defining the region where the histogram will be computed. bins -> integer defining the bins range Output: hist -> int32: nxbins array with the histograms of every point in the list """ if bins == []: bins = f.max()+1 ndim = f.ndim if ndim == 2: f = f.reshape(1,f.shape[0],f,shape[1]) f = f.astype(np.int32) off = se2off(Bc) if ndim == 2: off = np.concatenate((np.zeros((off.shape[0],1), dtype = np.int32),off), axis = 1) pl = np.concatenate((np.zeros((pl.shape[0],1), dtype = np.int32),off), axis = 1) hists = np.zeros((pl.shape[0],bins), dtype = np.int32) watershed_c.window_histogram_aux_c(f,off,pl,hists) return hists
def build_max_tree(f, Bc, option=0): ndim = f.ndim off = se2off(Bc) # Array of offsets that defines the structuring element parent = np.empty(f.size, dtype=np.int32) parent[:] = -1 # parent array zpar = np.empty(f.size, dtype=np.int32) #auxiliary array to store level roots ftype = f.dtype if (ftype == np.uint8): fu16 = f.astype(np.uint16) else: fu16 = f flat_img = fu16.ravel() MAX = flat_img.max() S_rev = max_tree_c_01.counting_sort_c(int(MAX), flat_img) #image sorting #Max-tree construction if ndim == 2: H, W = f.shape max_tree_c_01.union_find2d_c(H, W, off, parent, zpar, S_rev, flat_img) elif ndim == 3: L, M, N = f.shape max_tree_c_01.union_find3d_c(L, M, N, off, parent, zpar, S_rev, flat_img) else: print "Invalid option" return # Tree canocalization max_tree_c_01.canonicalize_c(flat_img, parent, S_rev) if option == 0: return parent, S_rev # returns usual max-tree representation else: node_index = np.empty(f.shape, dtype=np.int32) node_index[:] = -1 if ndim == 2: # node array/node index representation node_array = max_tree_c_01.computeNodeArray2d_c( parent, flat_img, S_rev, node_index) else: node_array = max_tree_c_01.computeNodeArray3d_c( parent, flat_img, S_rev, node_index) node_array = node_array.T.copy() return parent, S_rev, node_array, node_index
def build_max_tree(f, Bc, option = 0): ndim = f.ndim off = se2off(Bc) # Array of offsets that defines the structuring element parent = np.empty(f.size, dtype = np.int32) parent[:] = -1 # parent array zpar = np.empty(f.size, dtype = np.int32) #auxiliary array to store level roots ftype = f.dtype if (ftype == np.uint8): fu16 = f.astype(np.uint16) else: fu16 = f flat_img = fu16.ravel() MAX = flat_img.max() S_rev = max_tree_c_01.counting_sort_c(int(MAX),flat_img) #image sorting #Max-tree construction if ndim == 2: H,W = f.shape max_tree_c_01.union_find2d_c(H,W,off,parent,zpar,S_rev,flat_img) elif ndim == 3: L,M,N = f.shape max_tree_c_01.union_find3d_c(L,M,N,off,parent,zpar,S_rev,flat_img) else: print "Invalid option" return # Tree canocalization max_tree_c_01.canonicalize_c(flat_img,parent,S_rev) if option == 0: return parent,S_rev # returns usual max-tree representation else: node_index = np.empty(f.shape, dtype = np.int32) node_index[:] = -1 if ndim == 2: # node array/node index representation node_array = max_tree_c_01.computeNodeArray2d_c(parent,flat_img,S_rev,node_index) else: node_array = max_tree_c_01.computeNodeArray3d_c(parent,flat_img,S_rev,node_index) node_array = node_array.T.copy() return parent,S_rev,node_array,node_index
def tz_ws(img,markers,Bc): """ Tie-zone watershed from markers. Input: img -> 2d or 3d, uint8 or uint16 image markers -> uint16 image, same number of dimensions of img Bc -> Boolean array defining connectivity, same number of dimensions as img. Output: seg -> uint16 image same number of dimensions as img. The tie-zones receive the label 0. """ D2 = False if img.ndim == 2: temp_H,temp_W = img.shape img = img.reshape(1,temp_H,temp_W) markers = markers.reshape(1,temp_H,temp_W) D2 = True off = se2off(Bc) #offsets if off.shape[1] == 2: off = np.concatenate((np.zeros((off.shape[0],1), dtype = np.int32),off), axis = 1) ftype = img.dtype mtype = markers.dtype if (mtype != np.int32): warnings.warn("markers should be int32, forcing conversion") markers = markers.astype(np.int32) if (ftype == np.uint8): img = img.astype(np.uint16) seg = markers.copy() ii32 = np.iinfo(np.int32).max # infinity P = np.empty(img.shape, dtype = np.int32) P[:] = -1 C1 = np.empty(img.shape, dtype = np.int32) C1[:] = ii32 C2 = np.zeros(img.shape, dtype = np.int32) done =np.zeros(img.shape, dtype = np.int32) watershed_c.tz_ws_c(ii32,off,img,seg,C1,C2,done,P) if D2: L,M,N = seg.shape seg = seg.reshape(M,N) return seg
def __init__(self, img=None, Bc=None, option='max_tree'): if option == 'max_tree': _, _, self.node_array, self.node_index, = build_max_tree(img, Bc, option=1) elif option == 'tree_of_shapes': print "Error: Option not implemented yet" else: print "Error: invalid option" return self.Bc = Bc self.shape = img.shape self._children_list = [] self._cum_children_hist = [] self._children_updated = False self._sb = [] self._cum_sb_hist = [] self._sb_updated = False self.off = se2off(Bc) self.ftype = img.dtype self.get_children_aux = get_children_aux_c self.get_ancestors_aux = get_ancestors_aux_c self.get_descendants_aux = get_descendants_aux_c self.get_sub_branches_aux = get_sub_branches_aux_c self.prune_aux = prune_aux_c self.contract_dr_aux = contract_dr_aux_c self.update_nchild_aux = update_nchild_aux_c self.remove_node_array_lines_aux = remove_node_array_lines_c self.rec_connected_component_2d_aux = rec_connected_component_2d_c self.rec_connected_component_3d_aux = rec_connected_component_3d_c self.get_image_aux_2d_aux = get_image_aux_2d_c self.get_image_aux_3d_aux = get_image_aux_3d_c self.lut_node_index_3d_aux = lut_node_index_3d_c self.lut_node_index_2d_aux = lut_node_index_2d_c self.get_bif_ancestor_aux = get_bif_ancestor_aux_c self.compute_node_gray_avg_aux = compute_node_gray_avg_aux_c self.compute_node_gray_var_aux = compute_node_gray_var_aux_c self.compute_eccentricity_aux = compute_eccentricity_aux_c self.compute_hist_aux = compute_hist_aux_c
def __init__(self,img = None, Bc = None,option = 'max_tree'): if option == 'max_tree': _,_,self.node_array,self.node_index, = build_max_tree(img,Bc, option = 1) elif option == 'tree_of_shapes': print "Error: Option not implemented yet" else: print "Error: invalid option" return self.Bc = Bc self.shape = img.shape self._children_list = [] self._cum_children_hist = [] self._children_updated = False self._sb = [] self._cum_sb_hist = [] self._sb_updated = False self.off = se2off(Bc) self.ftype = img.dtype self.get_children_aux = get_children_aux_c self.get_ancestors_aux = get_ancestors_aux_c self.get_descendants_aux = get_descendants_aux_c self.get_sub_branches_aux = get_sub_branches_aux_c self.prune_aux = prune_aux_c self.contract_dr_aux = contract_dr_aux_c self.update_nchild_aux = update_nchild_aux_c self.remove_node_array_lines_aux = remove_node_array_lines_c self.rec_connected_component_2d_aux = rec_connected_component_2d_c self.rec_connected_component_3d_aux = rec_connected_component_3d_c self.get_image_aux_2d_aux = get_image_aux_2d_c self.get_image_aux_3d_aux = get_image_aux_3d_c self.lut_node_index_3d_aux = lut_node_index_3d_c self.lut_node_index_2d_aux = lut_node_index_2d_c self.get_bif_ancestor_aux = get_bif_ancestor_aux_c self.compute_node_gray_avg_aux = compute_node_gray_avg_aux_c self.compute_node_gray_var_aux = compute_node_gray_var_aux_c self.compute_eccentricity_aux = compute_eccentricity_aux_c self.compute_hist_aux = compute_hist_aux_c