def tieZone(im, offsets): import numpy as np from ipdp import wsImage from ipdp import findMinima from ipdp import wsHeapQueue # initialise variables ws = wsImage(im) N, im, lab, D = ws.begin(offsets) # find minima M = findMinima(im, N, D) # create the working images done = ws.makeWorkCopy(False) c1 = ws.makeWorkCopy(np.inf) c2 = ws.makeWorkCopy(0) par = ws.makeWorkCopy(MASK) lab[:] = MASK queue = wsHeapQueue() for m in range(len(M)): for p in M[m]: c1[p] = im[p] lab[p] = m + 1 par[p] = p queue.push(p, im[p]) while not queue.empty(): p = queue.pop() done[p] = True for q in N(p): if done[q]: continue c = max(c1[p], im[q]) if c < c1[q]: if c1[q] < np.inf: if queue.contains(q, c1[q]): queue.remove(q, c1[q]) c1[q] = c lab[q] = lab[p] par[q] = p queue.push(q, c1[q]) if c == c1[p]: c2[q] = c2[p] + 1 elif c == c1[q] and lab[q] != lab[p]: if c == c1[p]: if c2[q] == c2[p] + 1: lab[q] = TIE_ZONE else: lab[q] = TIE_ZONE return ws.end()
def tieZone(im, offsets): import numpy as np from ipdp import wsImage from ipdp import findMinima from ipdp import wsHeapQueue # initialise variables ws = wsImage(im) N, im, lab, D = ws.begin(offsets) # find minima M = findMinima(im, N, D) # create the working images done = ws.makeWorkCopy(False) c1 = ws.makeWorkCopy(np.inf) c2 = ws.makeWorkCopy(0) par = ws.makeWorkCopy(MASK) lab[:] = MASK queue = wsHeapQueue() for m in xrange(len(M)): for p in M[m]: c1[p] = im[p] lab[p] = m+1 par[p] = p queue.push(p, im[p]) while not queue.empty(): p = queue.pop() done[p] = True for q in N(p): if done[q]: continue c = max(c1[p], im[q]) if c < c1[q]: if c1[q] < np.inf: if queue.contains(q, c1[q]): queue.remove(q, c1[q]) c1[q] = c lab[q] = lab[p] par[q] = p queue.push(q, c1[q]) if c == c1[p]: c2[q] = c2[p] + 1 elif c == c1[q] and lab[q] != lab[p]: if c == c1[p]: if c2[q] == c2[p] + 1: lab[q] = TIE_ZONE else: lab[q] = TIE_ZONE return ws.end()
def ift_m(im, offsets, M): from ipdp import wsImage # initialise variables ws = wsImage(im) N, im, lab, D = ws.begin(offsets) wsM = wsImage(M) NM, imM, labM, DM = wsM.begin(offsets) # make the set Mset = dict() for p in D: if imM[p] > 0: if Mset.has_key(imM[p]): Mset[imM[p]].append(p) else: Mset[imM[p]] = [p] ift_k(ws, im, Mset.values(), N, D, lab) return ws.end()
def connectedComponents(im, offsets): from ipdp import wsImage from ipdp import wsQueue # initialise variables ws = wsImage(im) N, im, lab, D = ws.begin(offsets) lab[:] = MASK adr = ws.makeWorkCopy(0) queue = wsQueue() def find(p): q = p while adr[q] != q: q = adr[q] u = p while adr[u] != q: v = adr[u] adr[u] = q u = v return q # step 1 for p in D: q = p for u in N(p): if im[u] < im[q]: q = u if q != p: adr[p] = q else: adr[p] = PLATEAU # step 2 for p in D: if adr[p] != PLATEAU: continue for q in N(p): if adr[q] == PLATEAU or im[q] != im[p]: continue queue.push(q) while not queue.empty(): p = queue.pop() for q in N(p): if adr[q] != PLATEAU or im[q] != im[p]: continue adr[q] = p queue.push(q) # step 3 for p in D: if adr[p] != PLATEAU: continue adr[p] = p for q in N(p): if q > p or im[q] != im[p]: continue u = find(p) v = find(q) adr[u] = adr[v] = min(u, v) # step 4 basins = 1 for p in D: r = find(p) adr[p] = r if lab[r] == MASK: lab[r] = basins basins += 1 lab[p] = lab[r] return ws.end()
def connectedComponents(im, offsets): from ipdp import wsImage from ipdp import wsQueue # initialise variables ws = wsImage(im) N, im, lab, D = ws.begin(offsets) lab[:] = MASK adr = ws.makeWorkCopy(0) queue = wsQueue() def find(p): q = p while adr[q] != q: q = adr[q] u = p while adr[u] != q: v = adr[u] adr[u] = q u = v return q # step 1 for p in D: q = p for u in N(p): if im[u] < im[q]: q = u if q != p: adr[p] = q else: adr[p] = PLATEAU # step 2 for p in D: if adr[p] != PLATEAU: continue for q in N(p): if adr[q] == PLATEAU or im[q] != im[p]: continue queue.push(q) while not queue.empty(): p = queue.pop() for q in N(p): if adr[q] != PLATEAU or im[q] != im[p]: continue adr[q] = p queue.push(q) # step 3 for p in D: if adr[p] != PLATEAU: continue adr[p] = p for q in N(p): if q > p or im[q] != im[p]: continue u = find(p) v = find(q) adr[u] = adr[v] = min(u,v) # step 4 basins = 1 for p in D: r = find(p) adr[p] = r if lab[r] == MASK: lab[r] = basins basins += 1 lab[p] = lab[r] return ws.end()