def iaareaopen(f,a,Bc=iasecross()): a = -a s = f.shape g = np.zeros_like(f).ravel() f1 = np.concatenate((f.ravel(),np.array([0]))) area = -np.ones((f1.size,), np.int32) N = MT.iaNlut(s, MT.iase2off(Bc)) pontos = f1.nonzero()[0] pontos = pontos[np.lexsort((np.arange(0,-len(pontos),-1),f1[pontos]))[::-1]] for p in pontos: for v in N[p]: if f1[p] < f1[v] or (f1[p] == f1[v] and v < p): rv = find_area(area, v) if rv != p: if area[rv] > a or f1[p] == f1[rv]: area[p] = area[p] + area[rv] area[rv] = p else: area[p] = a for p in pontos[::-1]: if area[p] >= 0: g[p] = g[area[p]] else: if area[p] <= a: g[p] = f1[p] return g.reshape(s)
def iaareaopen(f, a, Bc=iasecross()): a = -a s = f.shape g = np.zeros_like(f).ravel() f1 = np.concatenate((f.ravel(), np.array([0]))) area = -np.ones((f1.size,), np.int32) N = MT.iaNlut(s, MT.iase2off(Bc)) pontos = f1.nonzero()[0] pontos = pontos[np.lexsort((np.arange(0, -len(pontos), -1), f1[pontos]))[::-1]] for p in pontos: for v in N[p]: if f1[p] < f1[v] or (f1[p] == f1[v] and v < p): rv = find_area(area, v) if rv != p: if area[rv] > a or f1[p] == f1[rv]: area[p] = area[p] + area[rv] area[rv] = p else: area[p] = a for p in pontos[::-1]: if area[p] >= 0: g[p] = g[area[p]] else: if area[p] <= a: g[p] = f1[p] return g.reshape(s)
def ialabel_unionfind(img, Bc): imshape = img.shape imsize = img.size # Offsets and neighbors offsets = MT.iase2off(Bc, 'fw') neighbors = MT.iaNlut(imshape, offsets) parents = np.arange(imsize, dtype=int) # Raster image and no-nzero pixels img = img.ravel() nonzero_nodes = np.nonzero(img)[0] img = np.concatenate((img, np.array([0]))) O = np.zeros(imsize, dtype=int) cur_label = 0 # pass 1: backward scan, forward neighbors for p in nonzero_nodes[::-1]: for nb in neighbors[p]: if img[nb]: Union(nb, p, parents) # pass 2_1: root labeled pbool = parents[nonzero_nodes] == nonzero_nodes labels = np.arange(1, pbool.sum() + 1) O[nonzero_nodes[pbool]] = labels # pass 2_2: labeling root descendants for p in nonzero_nodes[~pbool]: O[p] = O[parents[p]] return O.reshape(imshape)
def ialabelflat_unionfind(img, Bc, delta): imshape = img.shape imsize = img.size # Offsets and neighbors offsets = MT.iase2off(Bc, "fw") neighbors = MT.iaNlut(imshape, offsets) parents = np.arange(imsize, dtype=int) # Raster image and no-nzero pixels img = img.ravel() nonzero_nodes = np.nonzero(img)[0] img = np.concatenate((img, np.array([0]))) g = np.zeros(imsize, dtype=int) cur_label = 0 # pass 1: backward scan, forward neighbors for p in nonzero_nodes[::-1]: v = img[p] for nb in neighbors[p]: if img[nb] and (abs(v - img[nb]) <= delta): Union(nb, p, parents) # pass 2_1: root labeled pbool = parents[nonzero_nodes] == nonzero_nodes labels = np.arange(1, pbool.sum() + 1) g[nonzero_nodes[pbool]] = labels # pass 2_2: labeling root descendants for p in nonzero_nodes[~pbool]: g[p] = g[parents[p]] return g.reshape(imshape)