def iadist(f, Bc=iasecross(), METRIC=None): from iagray import iagray from iaintersec import iaintersec from iaisequal import iaisequal from iaero import iaero from iasebox import iasebox if METRIC is not None: METRIC = METRIC.upper() f = iagray(f, 'uint16') y = iaintersec(f, 0) if (METRIC == 'EUCLIDEAN') or (METRIC == 'EUC2'): f = int32(f) b = int32(zeros((3, 3))) i = 1 while any(f != y): a4, a2 = -4 * i + 2, -2 * i + 1 b = int32([[a4, a2, a4], [a2, 0, a2], [a4, a2, a4]]) y = f i = i + 1 f = iaero(f, b) if METRIC == 'EUCLIDEAN': y = uint16(sqrt(f) + 0.5) else: if iaisequal(Bc, iasecross()): b = int32([[-2147483647, -1, -2147483647], [-1, 0, -1], [-2147483647, -1, -2147483647]]) elif iaisequal(Bc, iasebox()): b = int32([[-1, -1, -1], [-1, 0, -1], [-1, -1, -1]]) else: b = Bc while any(f != y): y = f f = iaero(f, b) return y
def iaopentransf(f, type='OCTAGON', n=65535, Bc=iasecross(), Buser=iasecross()): from iaisbinary import iaisbinary from iabinary import iabinary from iaisequal import iaisequal from iaopen import iaopen from iasesum import iasesum from iasedisk import iasedisk from iaaddm import iaaddm from iagray import iagray from iagrain import iagrain from ialabel import ialabel assert iaisbinary(f),'Error: input image is not binary' type = type.upper() rec_flag = (type).find('-REC') if rec_flag != -1: type = type[:rec_flag] # remove the -rec suffix flag = not ((type == 'OCTAGON') or (type == 'CHESSBOARD') or (type == 'CITY-BLOCK')) if not flag: n = min(n,min(f.shape)) elif type == 'LINEAR-H': se = iabinary([1, 1, 1]) n = min(n,f.shape[1]) elif type =='LINEAR-V': se = iabinary([[1],[1],[1]]) n = min(n,f.shape[0]) elif type == 'LINEAR-45R': se = iabinary([[0, 0, 1],[0, 1, 0],[1, 0, 0]]) n = min(n,min(f.shape)) elif type == 'LINEAR-45L': se = iabinary([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) n = min(n,min(f.shape)) elif type == 'USER': se = Buser n = min(n,min(f.shape)) else: #print('Error: only accepts OCTAGON, CHESSBOARD, CITY-BLOCK, LINEAR-H, LINEAR-V, LINEAR-45R, LINEAR-45L, or USER as type, or with suffix -REC.') print('Error') return [] k = 0 y = uint16(zeros(f.shape)) a = iabinary([1]) z = iabinary([0]) while not ( iaisequal(a,z) or (k>=n)): print('processing r=',k) if flag: a = iaopen(f,iasesum(se,k)) else: a = iaopen(f,iasedisk(k,'2D',type)) y = iaaddm(y, iagray(a,'uint16',1)) k = k+1 if rec_flag != -1: y = iagrain( ialabel(f,Bc),y,'max') return y
def iaopenrecth(f, bero=None, bc=None): from ia870.iasubm import iasubm from ia870.iaopenrec import iaopenrec from ia870.iasecross import iasecross if bero is None: bero = iasecross() if bc is None: bc = iasecross() y = iasubm(f, iaopenrec( f, bero, bc)) return y
def iapatspec(f, type='OCTAGON', n=65535, Bc=None, Buser=None): from ia870.iaisbinary import iaisbinary from ia870.iaopentransf import iaopentransf from ia870.iahistogram import iahistogram from ia870.iasecross import iasecross if Bc is None: Bc = iasecross(None) if Buser is None: Buser = iasecross(None) assert iaisbinary(f),'Error: input image is not binary' g=iaopentransf(f,type,n,Bc,Buser) h=iahistogram(g) h=h[1:] return h
def iaasf(f, SEQ="OC", b=None, n=1): from ia870.iasesum import iasesum from ia870.iaopen import iaopen from ia870.iaclose import iaclose from ia870.iasecross import iasecross if b is None: b = iasecross(None) SEQ = SEQ.upper() y = f if SEQ == 'OC': for i in range(1, n + 1): nb = iasesum(b, i) y = iaopen(iaclose(y, nb), nb) elif SEQ == 'CO': for i in range(1, n + 1): nb = iasesum(b, i) y = iaclose(iaopen(y, nb), nb) elif SEQ == 'OCO': for i in range(1, n + 1): nb = iasesum(b, i) y = iaopen(iaclose(iaopen(y, nb), nb), nb) elif SEQ == 'COC': for i in range(1, n + 1): nb = iasesum(b, i) y = iaclose(iaopen(iaclose(y, nb), nb), nb) return y
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 iaedgeoff(f, Bc=iasecross()): from ia870.iaframe import iaframe from ia870.iasubm import iasubm from ia870.iainfrec import iainfrec edge = iaframe(f) return iasubm( f, iainfrec(edge, f, Bc))
def iahmax(f, h=1, Bc=iasecross()): from ia870.iasubm import iasubm from ia870.iainfrec import iainfrec g = iasubm(f, h) y = iainfrec(g, f, Bc) return y
def iaskelm(f, B=iasecross(), option="binary"): from ia870.iaisbinary import iaisbinary from ia870.ialimits import ialimits from ia870.iagray import iagray from ia870.iaintersec import iaintersec from ia870.iasesum import iasesum from ia870.iaero import iaero from ia870.iaisequal import iaisequal from ia870.iaopenth import iaopenth from ia870.iasedil import iasedil from ia870.iaunion import iaunion from ia870.iabinary import iabinary from ia870.iapad import iapad from ia870.iaunpad import iaunpad assert iaisbinary(f),'Input binary image only' option = option.upper() f = iapad(f,B) print(f) k1,k2 = ialimits(f) y = iagray( iaintersec(f, k1),'uint16') iszero = asarray(y) nb = iasesum(B,0) for r in range(1,65535): ero = iaero( f, nb) if iaisequal(ero, iszero): break f1 = iaopenth( ero, B) nb = iasedil(nb, B) y = iaunion(y, iagray(f1,'uint16',r)) if option == 'BINARY': y = iabinary(y) y = iaunpad(y,B) return y
def iaclohole(f, Bc=iasecross()): from iaframe import iaframe from ianeg import ianeg from iainfrec import iainfrec delta_f = iaframe(f) return ianeg(iainfrec(delta_f, ianeg(f), Bc))
def iahmin(f, h=1, Bc=iasecross()): from ia870.iaaddm import iaaddm from ia870.iasuprec import iasuprec g = iaaddm(f, h) y = iasuprec(g, f, Bc) return y
def iaregmax(f, Bc=iasecross()): from ia870.iasubm import iasubm from ia870.iahmax import iahmax from ia870.iabinary import iabinary from ia870.iaregmin import iaregmin from ia870.ianeg import ianeg y = iasubm(f, iahmax(f, 1, Bc)) return iabinary(y)
def iacloseth(f, b=None): from ia870.iasubm import iasubm from ia870.iaclose import iaclose from ia870.iasecross import iasecross if b is None: b = iasecross() y = iasubm(iaclose(f, b), f) return y
def ialastero(f, B=iasecross()): from iaisbinary import iaisbinary from iadist import iadist from iaregmax import iaregmax assert iaisbinary(f), 'Can only process binary images' dt = iadist(f, B) y = iaregmax(dt, B) return y
def iaunpad(f, B=iasecross()): from ia870.iamat2set import iamat2set from ia870.iaseshow import iaseshow i,v=iamat2set( iaseshow(B)); mni=minimum.reduce(i) mxi=maximum.reduce(i) g = f[-mni[0]:f.shape[0]-mxi[0], -mni[1]:f.shape[1]-mxi[1]] return g
def iawatershed(f, Bc=iasecross(), option='LINES'): from ia870 import iasubm, iaero, iabinary from ia870.ipdp import se2offset offset = se2offset(Bc) w = connectedComponents(f, offset) if option.upper() == 'LINES': w = iasubm(w, iaero(w)) w = iabinary(w) return w
def iacdil(f, g, b=iasecross(), n=1): from iaintersec import iaintersec from iadil import iadil from iaisequal import iaisequal y = iaintersec(f, g) for i in range(n): aux = y y = iaintersec(iadil(y, b), g) if iaisequal(y, aux): break return y
def iacero(f, g, b=iasecross(), n=1): from ia870.iaunion import iaunion from ia870.iaero import iaero from ia870.iaisequal import iaisequal y = iaunion(f, g) for i in range(n): aux = y y = iaunion(iaero(y, b), g) if iaisequal(y, aux): break return y
def iaasfrec(f, SEQ="OC", b=iasecross(), bc=iasecross(), n=1): from iasesum import iasesum from iacloserec import iacloserec from iaopenrec import iaopenrec SEQ = SEQ.upper() y = f if SEQ == 'OC': for i in range(1, n + 1): nb = iasesum(b, i) y = iacloserec(y, nb, bc) y = iaopenrec(y, nb, bc) elif SEQ == 'CO': for i in range(1, n + 1): nb = iasesum(b, i) y = iaopenrec(y, nb, bc) y = iacloserec(y, nb, bc) else: assert 0, 'Only accepts OC or CO for SEQ parameter' return y
def iaregmin(f, Bc=iasecross(), option="binary"): from ia870.iahmin import iahmin from ia870.iaaddm import iaaddm from ia870.iasubm import iasubm from ia870.iabinary import iabinary from ia870.iasuprec import iasuprec from ia870.iaunion import iaunion from ia870.iathreshad import iathreshad if option != "binary": raise Exception("iaregmin accepts only binary option") return iabinary(iasubm(iahmin(f,1,Bc), f))
def iapad(f, B=iasecross(), value=0): from ia870.iamat2set import iamat2set from ia870.iaseshow import iaseshow i, v = iamat2set(iaseshow(B)) mni = i.min(axis=0) mxi = i.max(axis=0) f = asarray(f) if size(f.shape) == 1: f = f[newaxis, :] g = (value * ones(array(f.shape) + mxi - mni)).astype(f.dtype) g[-mni[0]:g.shape[0] - mxi[0], -mni[1]:g.shape[1] - mxi[1]] = f return g
def iaskelmrec(f, B=None): from ia870.iabinary import iabinary from ia870.iaintersec import iaintersec from ia870.iadil import iadil from ia870.iaunion import iaunion from ia870.iasecross import iasecross if B is None: B = iasecross(None) y = iabinary(iaintersec(f, 0)) for r in range(max(ravel(f)), 1, -1): y = iadil(iaunion(y, iabinary(f, r)), B) y = iaunion(y, iabinary(f, 1)) 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 iainpos(f, g, bc=iasecross()): from ia870.iaisbinary import iaisbinary from ia870.iagray import iagray from ia870.ianeg import ianeg from ia870.iadatatype import iadatatype from ia870.ialimits import ialimits from ia870.iasuprec import iasuprec from ia870.iaintersec import iaintersec from ia870.iaunion import iaunion assert iaisbinary(f), 'First parameter must be binary image' fg = iagray(ianeg(f), iadatatype(g)) k1 = ialimits(g)[1] - 1 y = iasuprec(fg, iaintersec(iaunion(g, 1), k1, fg), bc) return y
def iaareaopen_eq(f, a, Bc=iasecross()): if f.dtype == np.bool: fr = MT.ialabel(f, Bc) # binary area open, use area measurement g = MT.iablob(fr, 'area') y = g >= a else: y = np.zeros_like(f) k1 = f.min() k2 = f.max() for k in range(k1, k2 + 1): # gray-scale, use thresholding decomposition fk = (f >= k) fo = MT.iaareaopen(fk, a, Bc) if not fo.any(): break y = MT.iaunion(y, MT.iagray(fo, f.dtype, k)) return y
def iainfrec(m, f, Bc=iasecross()): h, w = Bc.shape hc, wc = h / 2, w / 2 B = Bc.copy() off = np.transpose(B.nonzero()) - np.array([hc, wc]) i = off[:, 0] * w + off[:, 1] Nids = MT.iaNlut(f.shape, off) x, y = np.where(Nids == f.size) Nids[x, y] = x Nids_pos = Nids[:, i < 0] #de acordo com a convenção em Vincent93 Nids_neg = Nids[:, i > 0] #de acordo com a convenção em Vincent93 I = f.flatten() J = m.flatten() D = np.nonzero(J)[0] V = np.zeros(f.size, np.bool) #para controle de inserção na fila fila = [] for p in D: Jq = J[p] for q in Nids_pos[p]: Jq = max(Jq, J[q]) if (J[q] < J[p]) and (J[q] < I[q]) and ~V[p]: fila.append(p) V[p] = True J[p] = min(Jq, I[p]) for p in D[::-1]: Jq = J[p] for q in Nids_neg[p]: Jq = max(Jq, J[q]) if (J[q] < J[p]) and (J[q] < I[q]) and ~V[p]: fila.append(p) V[p] = True J[p] = min(Jq, I[p]) while fila: p = fila.pop(0) for q in Nids[p]: if J[q] < J[p] and I[q] != J[q]: J[q] = min(J[p], I[q]) fila.append(q) return J.reshape(f.shape)
def iagdist(f, g, Bc=iasecross(), METRIC=None): from ia870.ianeg import ianeg from ia870.iagray import iagray from ia870.iaintersec import iaintersec from ia870.iaisequal import iaisequal from ia870.iacero import iacero from ia870.iaaddm import iaaddm from ia870.iaunion import iaunion assert METRIC is None, 'Does not support EUCLIDEAN' fneg, gneg = ianeg(f), ianeg(g) y = iagray(gneg, 'uint16', 1) ero = iaintersec(y, 0) aux = y i = 1 while (ero != aux).any(): aux = ero ero = iacero(gneg, fneg, Bc, i) y = iaaddm(y, iagray(ero, 'uint16', 1)) i = i + 1 y = iaunion(y, iagray(ero, 'uint16')) return y
def iaopenrec(f, bero=iasecross(), bc=iasecross()): from iainfrec import iainfrec from iaero import iaero return iainfrec(iaero(f, bero), f, bc)
def iaareaclose(f, a, Bc=iasecross()): from ianeg import ianeg from iaareaopen import iaareaopen y = ianeg( iaareaopen( ianeg(f),a,Bc)) return y
def iatz(f, Bc=iasecross()): from ipdp import se2offset offset = se2offset(Bc) return tieZone(f, offset)