def iasedil(B1, B2): from ia870 import ialimits, iagray, iabinary, iaisbinary, iamat2set, iaadd4dil, iasetrans, iaseunion assert (iaisbinary(B1) or (B1.dtype == int32) or (B1.dtype == int64) or (B1.dtype == float64)) and \ (iaisbinary(B2) or (B2.dtype == int32) or (B2.dtype == int64) or (B2.dtype == float64)), \ 'iasedil: s.e. must be binary, int32, int64 or float64' if len(B1.shape) == 1: B1 = B1[newaxis, :] if len(B2.shape) == 1: B2 = B2[newaxis, :] if B1.dtype == 'bool' and B2.dtype == 'bool': Bo = iabinary([0]) else: Bo = array(ialimits(B1)[0]).reshape(1) if iaisbinary(B1): Bo = array(ialimits(B2)[0]).reshape(1) B1 = iagray(B1, B2.dtype, 0) if iaisbinary(B2): Bo = array(ialimits(B1)[0]).reshape(1) B2 = iagray(B2, B1.dtype, 0) x, v = iamat2set(B2) if len(x): for i in range(x.shape[0]): s = iaadd4dil(B1, v[i]) st = iasetrans(s, x[i]) Bo = iaseunion(Bo, st) return Bo
def iaseshow(B, option="NORMAL"): from ia870 import iaisbinary, iaintersec, iagray, iabinary from ia870 import iasedil, iaero, iabshow option = upper(option) if option == 'NON-FLAT': y = array([0], int32) if iaisbinary(B): B = iaintersec(iagray(B, 'int32'), 0) elif option == 'NORMAL': if iaisbinary(B): y = iabinary([1]) else: y = array([0], int32) elif option == 'EXPAND': assert iaisbinary(B), 'This option is only available with flat SE' y = iasedil(iabinary([1]), B) b1 = iabinary(y >= 0) b0 = b1 < 0 b0[shape(b0)[0] / 2, shape(b0)[1] / 2] = 1 y = iabshow(b1, y, b0) return y else: print('iaseshow: not a valid flag: NORMAL, EXPAND or NON-FLAT') y = iasedil(y, B) return y
def iasedil(B1, B2): from ia870 import ialimits, iagray, iabinary, iaisbinary, iamat2set, iaadd4dil, iasetrans, iaseunion assert (iaisbinary(B1) or (B1.dtype == int32) or (B1.dtype == int64) or (B1.dtype == float64)) and \ (iaisbinary(B2) or (B2.dtype == int32) or (B2.dtype == int64) or (B2.dtype == float64)), \ 'iasedil: s.e. must be binary, int32, int64 or float64' if len(B1.shape) == 1: B1 = B1[newaxis,:] if len(B2.shape) == 1: B2 = B2[newaxis,:] if B1.dtype=='bool' and B2.dtype == 'bool': Bo = iabinary([0]) else: Bo = array(ialimits(B1)[0]).reshape(1) if iaisbinary(B1): Bo = array(ialimits(B2)[0]).reshape(1) B1 = iagray(B1,B2.dtype,0) if iaisbinary(B2): Bo = array(ialimits(B1)[0]).reshape(1) B2 = iagray(B2,B1.dtype,0) x,v = iamat2set(B2) if len(x): for i in range(x.shape[0]): s = iaadd4dil(B1,v[i]) st= iasetrans(s,x[i]) Bo = iaseunion(Bo,st) return Bo
def iaseshow(B, option="NORMAL"): from ia870 import iaisbinary, iaintersec, iagray, iabinary from ia870 import iasedil, iaero, iabshow option = upper(option) if option == "NON-FLAT": y = array([0], int32) if iaisbinary(B): B = iaintersec(iagray(B, "int32"), 0) elif option == "NORMAL": if iaisbinary(B): y = iabinary([1]) else: y = array([0], int32) elif option == "EXPAND": assert iaisbinary(B), "This option is only available with flat SE" y = iasedil(iabinary([1]), B) b1 = iabinary(y >= 0) b0 = b1 < 0 b0[shape(b0)[0] / 2, shape(b0)[1] / 2] = 1 y = iabshow(b1, y, b0) return y else: print "iaseshow: not a valid flag: NORMAL, EXPAND or NON-FLAT" y = iasedil(y, B) return y
def iadil(f, b=None): from ia870 import iamat2set, ialimits, iaisbinary from ia870 import iaintersec, iagray, iaadd4dil, iasecross if b is None: b = iasecross() if len(f.shape) == 1: f = f[newaxis, :] h, w = f.shape x, v = iamat2set(b) if len(x) == 0: y = (ones((h, w)) * ialimits(f)[0]).astype(f.dtype) else: if iaisbinary(v): v = iaintersec(iagray(v, 'int32'), 0) mh, mw = max(abs(x)[:, 0]), max(abs(x)[:, 1]) y = (ones((h + 2 * mh, w + 2 * mw)) * ialimits(f)[0]).astype(f.dtype) for i in range(x.shape[0]): if v[i] > -2147483647: y[mh + x[i, 0]:mh + x[i, 0] + h, mw + x[i, 1]:mw + x[i, 1] + w] = maximum( y[mh + x[i, 0]:mh + x[i, 0] + h, mw + x[i, 1]:mw + x[i, 1] + w], iaadd4dil(f, v[i])) y = y[mh:mh + h, mw:mw + w] return y
def iasesum(B, N=1): from ia870 import iaisbinary, iabinary, iasedil if N==0: if iaisbinary(B): return iabinary([1]) else: return array([0],int32) # identity NB = B for i in range(N-1): NB = iasedil(NB,B) return NB
def iasesum(B, N=1): from ia870 import iaisbinary, iabinary, iasedil if N == 0: if iaisbinary(B): return iabinary([1]) else: return array([0], int32) # identity NB = B for i in range(N - 1): NB = iasedil(NB, B) return NB
def iacwatershed(f, g, Bc=iasecross(), option='LINES'): from ia870.ipdp import se2offset if iaisbinary(g): g = ialabel(g, Bc) offset = se2offset(Bc) w = ift_m(f, offset, g) if option == 'LINES': w = iasubm(w, iaero(w)) w = iabinary(w) return w
def iacwatershed(f, g, Bc=iasecross(), option='LINES'): from ipdp import se2offset if iaisbinary(g): g = ialabel(g, Bc) offset = se2offset(Bc) w = ift_m(f, offset, g) if option == 'LINES': w = iasubm(w, iaero(w)) w = iabinary(w) return w
def ialabel_rec(f, Bc=iasecross()): assert MT.iaisbinary(f), 'Can only label binary image' faux = f.copy() label = 1 y = MT.iagray(f, 'uint16', 0) # zero image (output) x = faux.ravel().nonzero() # get list of unlabeled pixel while len(x[0]): fmark = np.zeros_like(f) fmark.flat[x[0][0]] = 1 # get the first unlabeled pixel r = MT.iainfrec(fmark, faux, Bc) # detects all pixels connected to it faux -= r # remove them from faux r = MT.iagray(r, 'uint16', label) # label them with the value label y = MT.iaunion(y, r) # merge them with the labeled image label = label + 1 x = faux.ravel().nonzero() # get list of unlabeled pixel return y
def ialabel_rec(f, Bc=iasecross()): assert MT.iaisbinary(f),'Can only label binary image' faux=f.copy() label = 1 y = MT.iagray( f,'uint16',0) # zero image (output) x = faux.ravel().nonzero() # get list of unlabeled pixel while len(x[0]): fmark = np.zeros_like(f) fmark.flat[x[0][0]] = 1 # get the first unlabeled pixel r = MT.iainfrec( fmark, faux, Bc) # detects all pixels connected to it faux -= r # remove them from faux r = MT.iagray( r,'uint16',label) # label them with the value label y = MT.iaunion( y, r) # merge them with the labeled image label = label + 1 x = faux.ravel().nonzero() # get list of unlabeled pixel return y
def iagshow(X, X1=None, X2=None, X3=None, X4=None, X5=None, X6=None): from ia870 import iaisbinary, iagray, iaunion from ia870 import iaintersec, ianeg, iaconcat if iaisbinary(X): X = iagray(X, 'uint8') r = X g = X b = X if X1 is not None: # red 1 0 0 assert iaisbinary(X1), 'X1 must be binary overlay' x1 = iagray(X1, 'uint8') r = iaunion(r, x1) g = iaintersec(g, ianeg(x1)) b = iaintersec(b, ianeg(x1)) if X2 is not None: # green 0 1 0 assert iaisbinary(X2), 'X2 must be binary overlay' x2 = iagray(X2, 'uint8') r = iaintersec(r, ianeg(x2)) g = iaunion(g, x2) b = iaintersec(b, ianeg(x2)) if X3 is not None: # blue 0 0 1 assert iaisbinary(X3), 'X3 must be binary overlay' x3 = iagray(X3, 'uint8') r = iaintersec(r, ianeg(x3)) g = iaintersec(g, ianeg(x3)) b = iaunion(b, x3) if X4 is not None: # magenta 1 0 1 assert iaisbinary(X4), 'X4 must be binary overlay' x4 = iagray(X4, 'uint8') r = iaunion(r, x4) g = iaintersec(g, ianeg(x4)) b = iaunion(b, x4) if X5 is not None: # yellow 1 1 0 assert iaisbinary(X5), 'X5 must be binary overlay' x5 = iagray(X5, 'uint8') r = iaunion(r, x5) g = iaunion(g, x5) b = iaintersec(b, ianeg(x5)) if X6 is not None: # cyan 0 1 1 assert iaisbinary(X6), 'X6 must be binary overlay' x6 = iagray(X6, 'uint8') r = iaintersec(r, ianeg(x6)) g = iaunion(g, x6) b = iaunion(b, x6) return np.array([r, g, b]) return Y
def iathin(f, Iab=None, n=-1, theta=45, DIRECTION="CLOCKWISE"): import ia870 as MT if Iab is None: Iab = MT.iahomothin() DIRECTION = upper(DIRECTION) assert MT.iaisbinary(f),'f must be binary image' if n == -1: n = product(f.shape) y = f zero = MT.iaintersec(f,0) for i in range(n): aux = zero for t in range(0,360,theta): sup = MT.iasupgen( y, MT.iainterot(Iab, t, DIRECTION)) aux = MT.iaunion( aux, sup) #y -= sup y = MT.iasubm( y, sup) #if iaisequal(aux,zero): break if not aux.any(): break return y