def iadil(f, b=None): from iamat2set import iamat2set from ialimits import ialimits from iaisbinary import iaisbinary from iaintersec import iaintersec from iagray import iagray from iaadd4dil import iaadd4dil from iasecross import 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 iadil(f, b=None): from iamat2set import iamat2set from ialimits import ialimits from iaisbinary import iaisbinary from iaintersec import iaintersec from iagray import iagray from iaadd4dil import iaadd4dil from iasecross import 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 ianeg(f): from ialimits import ialimits if ialimits(f)[0] == (-ialimits(f)[1]): y = -f else: y = ialimits(f)[0] + ialimits(f)[1] - f y = y.astype(f.dtype) return y
def iaframe(f, WT=1, HT=1, DT=0, k1=None, k2=None): from iaunion import iaunion from iaintersec import iaintersec from ialimits import ialimits if k1 is None: k1 = ialimits(f)[1] if k2 is None: k2 = ialimits(f)[0] assert len(f.shape)==2,'Supports 2D only' y = iaintersec(f,k2) y[:,0:WT] = k1 y[:,-WT:] = k1 y[0:HT,:] = k1 y[-HT:,:] = k1 return y
def iaframe(f, WT=1, HT=1, DT=0, k1=None, k2=None): from iaunion import iaunion from iaintersec import iaintersec from ialimits import ialimits if k1 is None: k1 = ialimits(f)[1] if k2 is None: k2 = ialimits(f)[0] assert len(f.shape) == 2, 'Supports 2D only' y = iaintersec(f, k2) y[:, 0:WT] = k1 y[:, -WT:] = k1 y[0:HT, :] = k1 y[-HT:, :] = k1 return y
def iaseintersec(B1, B2): from ialimits import ialimits from ia870 import iamat2set, iaset2mat assert B1.dtype == B2.dtype, \ 'iaseintersec: Cannot have different datatypes: \ %s and %s' % (str(B1.dtype), str(B2.dtype)) type1 = B1.dtype #if len(B1) == 0: return B2 if len(B1.shape) == 1: B1 = B1[newaxis, :] if len(B2.shape) == 1: B2 = B2[newaxis, :] if B1.shape != B2.shape: inf = ialimits(B1)[0] h1, w1 = B1.shape h2, w2 = B2.shape H, W = max(h1, h2), max(w1, w2) Hc, Wc = (H - 1) / 2, (W - 1) / 2 # center BB1, BB2 = asarray(B1), asarray(B2) B1, B2 = inf * ones((H, W)), inf * ones((H, W)) dh1s, dh1e = (h1 - 1) / 2, (h1 - 1) / 2 + ( h1 + 1) % 2 # deal with even and odd dimensions dw1s, dw1e = (w1 - 1) / 2, (w1 - 1) / 2 + (w1 + 1) % 2 dh2s, dh2e = (h2 - 1) / 2, (h2 - 1) / 2 + (h2 + 1) % 2 dw2s, dw2e = (w2 - 1) / 2, (w2 - 1) / 2 + (w2 + 1) % 2 B1[Hc - dh1s:Hc + dh1e + 1, Wc - dw1s:Wc + dw1e + 1] = BB1 B2[Hc - dh2s:Hc + dh2e + 1, Wc - dw2s:Wc + dw2e + 1] = BB2 B = minimum(B1, B2).astype(type1) i, v = iamat2set(B) B = iaset2mat((i, v)) return B
def iaskelm(f, B=iasecross(), option="binary"): from iaisbinary import iaisbinary from ialimits import ialimits from iagray import iagray from iaintersec import iaintersec from iasesum import iasesum from iaero import iaero from iaisequal import iaisequal from iaopenth import iaopenth from iasedil import iasedil from iaunion import iaunion from iabinary import iabinary from iapad import iapad from iaunpad import iaunpad assert iaisbinary(f),'Input binary image only' option = upper(option) 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 iaskelm(f, B=iasecross(), option="binary"): from iaisbinary import iaisbinary from ialimits import ialimits from iagray import iagray from iaintersec import iaintersec from iasesum import iasesum from iaero import iaero from iaisequal import iaisequal from iaopenth import iaopenth from iasedil import iasedil from iaunion import iaunion from iabinary import iabinary from iapad import iapad from iaunpad import iaunpad assert iaisbinary(f), 'Input binary image only' option = upper(option) 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 iaseunion(B1, B2): from ialimits import ialimits if B1.dtype != B2.dtype: print 'B1=',B1 print 'B2=',B2 assert B1.dtype == B2.dtype, \ 'iaseunion: Cannot have different datatypes: \ %s and %s' % (str(B1.dtype), str(B2.dtype)) type1 = B1.dtype #if len(B1) == 0: return B2 if len(B1.shape) == 1: B1 = B1[newaxis,:] if len(B2.shape) == 1: B2 = B2[newaxis,:] if B1.shape <> B2.shape: inf = ialimits(B1)[0] h1,w1 = B1.shape h2,w2 = B2.shape H,W = max(h1,h2),max(w1,w2) Hc,Wc = (H-1)/2,(W-1)/2 # center BB1,BB2 = asarray(B1),asarray(B2) B1, B2 = inf * ones((H,W)), inf *ones((H,W)) dh1s , dh1e = (h1-1)/2 , (h1-1)/2 + (h1+1)%2 # deal with even and odd dimensions dw1s , dw1e = (w1-1)/2 , (w1-1)/2 + (w1+1)%2 dh2s , dh2e = (h2-1)/2 , (h2-1)/2 + (h2+1)%2 dw2s , dw2e = (w2-1)/2 , (w2-1)/2 + (w2+1)%2 B1[ Hc-dh1s : Hc+dh1e+1 , Wc-dw1s : Wc+dw1e+1 ] = BB1 B2[ Hc-dh2s : Hc+dh2e+1 , Wc-dw2s : Wc+dw2e+1 ] = BB2 B = maximum(B1,B2).astype(type1) return B
def iaaddm(f1, f2): from ialimits import ialimits if type(f2) is array: assert f1.dtype == f2.dtype, 'Cannot have different datatypes:' k1,k2 = ialimits(f1) y = clip(f1.astype(int64)+f2, k1, k2) y = y.astype(f1.dtype) return y
def iaaddm(f1, f2): from ialimits import ialimits if type(f2) is array: assert f1.dtype == f2.dtype, 'Cannot have different datatypes:' k1, k2 = ialimits(f1) y = clip(f1.astype(int64) + f2, k1, k2) y = y.astype(f1.dtype) return y
def iaadd4dil(f, c): from ialimits import ialimits if not c: return f if f.dtype == 'float64': y = f + c else: y = asarray(f, int64) + c k1, k2 = ialimits(f) y = ((f == k1) * k1) + ((f != k1) * y) y = clip(y, k1, k2) a = y.astype(f.dtype) return a
def iaadd4dil(f, c): from ialimits import ialimits if not c: return f if f.dtype == "float64": y = f + c else: y = asarray(f, int64) + c k1, k2 = ialimits(f) y = ((f == k1) * k1) + ((f != k1) * y) y = clip(y, k1, k2) a = y.astype(f.dtype) return a
def iaimg2se(fd, FLAT="FLAT", f=None): from iaisbinary import iaisbinary from iaseshow import iaseshow from ialimits import ialimits fd = (fd > 0) #assert iaisbinary(fd),'First parameter must be binary' FLAT = upper(FLAT) if FLAT == 'FLAT': return iaseshow(fd) else: B = choose(fd, ( ialimits(int32([0]))[0]*ones(fd.shape),f) ) B = iaseshow(int32(B),'NON-FLAT') return B
def iaimg2se(fd, FLAT="FLAT", f=None): from iaisbinary import iaisbinary from iaseshow import iaseshow from ialimits import ialimits fd = (fd > 0) #assert iaisbinary(fd),'First parameter must be binary' FLAT = upper(FLAT) if FLAT == 'FLAT': return iaseshow(fd) else: B = choose(fd, (ialimits(int32([0]))[0] * ones(fd.shape), f)) B = iaseshow(int32(B), 'NON-FLAT') return B
def iamat2set(A): from ialimits import ialimits if len(A.shape) == 1: A = A[newaxis, :] offsets = nonzero(ravel(A) ^ ialimits(A)[0]) if type(offsets) == type(()): offsets = offsets[0] # for compatibility with numarray if len(offsets) == 0: return ([], []) (h, w) = A.shape x = range(2) x[0] = offsets / w - (h - 1) / 2 x[1] = offsets % w - (w - 1) / 2 x = transpose(x) CV = x, ravel(A)[offsets] return CV
def iamat2set(A): from ialimits import ialimits if len(A.shape) == 1: A = A[newaxis,:] offsets = nonzero(ravel(A) - ialimits(A)[0]) if type(offsets) == type(()): offsets = offsets[0] # for compatibility with numarray if len(offsets) == 0: return ([],[]) (h,w) = A.shape x = range(2) x[0] = offsets/w - (h-1)/2 x[1] = offsets%w - (w-1)/2 x = transpose(x) CV = x,ravel(A)[offsets] return CV
def iainpos(f, g, bc=iasecross()): from iaisbinary import iaisbinary from iagray import iagray from ianeg import ianeg from iadatatype import iadatatype from ialimits import ialimits from iasuprec import iasuprec from iaintersec import iaintersec from 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 iainpos(f, g, bc=iasecross()): from iaisbinary import iaisbinary from iagray import iagray from ianeg import ianeg from iadatatype import iadatatype from ialimits import ialimits from iasuprec import iasuprec from iaintersec import iaintersec from 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 iaset2mat(A): from iabinary import iabinary from ialimits import ialimits if len(A) == 2: x, v = A v = asarray(v) elif len(A) == 1: x = A[0] v = ones((len(x), ), bool) else: raise TypeError, 'Argument must be a tuple of length 1 or 2' if len(x) == 0: return array([0]).astype(v.dtype) if len(x.shape) == 1: x = x[newaxis, :] dh, dw = abs(x).max(0) h, w = (2 * dh) + 1, (2 * dw) + 1 M = ones((h, w)) * ialimits(v)[0] offset = x[:, 0] * w + x[:, 1] + (dh * w + dw) M.flat[offset] = v M = M.astype(v.dtype) return M
def iaset2mat(A): from iabinary import iabinary from ialimits import ialimits if len(A) == 2: x, v = A v = asarray(v) elif len(A) == 1: x = A[0] v = ones((len(x),),bool) else: raise TypeError, 'Argument must be a tuple of length 1 or 2' if len(x) == 0: return array([0]).astype(v.dtype) if len(x.shape) == 1: x = x[newaxis,:] dh, dw = abs(x).max(0) h,w = (2*dh) + 1, (2*dw) + 1 M=ones((h, w)) * ialimits(v)[0] offset = x[:,0] * w + x[:,1] + (dh*w + dw) M.flat[offset] = v M = M.astype(v.dtype) return M