def iais(f1, oper, f2=None, oper1=None, f3=None): from iaisbinary import iaisbinary from iaisequal import iaisequal from iaislesseq import iaislesseq from ianeg import ianeg from iathreshad import iathreshad from iabinary import iabinary if f2 == None: oper=upper(oper); if oper == 'BINARY': return iaisbinary(f1) elif oper == 'GRAY' : return not iaisbinary(f1) else: assert 0,'oper should be BINARY or GRAY, was'+oper elif oper == '==': y = iaisequal(f1, f2) elif oper == '~=': y = not iaisequal(f1,f2) elif oper == '<=': y = iaislesseq(f1,f2) elif oper == '>=': y = iaislesseq(f2,f1) elif oper == '>': y = iaisequal( ianeg( iathreshad(f2,f1)),iabinary(1)) elif oper == '<': y = iaisequal( ianeg( iathreshad(f1,f2)),iabinary(1)) else: assert 0,'oper must be one of: ==, ~=, >, >=, <, <=, it was:'+oper if oper1 != None: if oper1 == '==': y = y and iaisequal(f2,f3) elif oper1 == '~=': y = y and (not iaisequal(f2,f3)) elif oper1 == '<=': y = y and iaislesseq(f2,f3) elif oper1 == '>=': y = y and iaislesseq(f3,f2) elif oper1 == '>': y = y and iaisequal( ianeg( iathreshad(f3,f2)),iabinary(1)) elif oper1 == '<': y = y and iaisequal( ianeg( iathreshad(f2,f3)),iabinary(1)) else: assert 0,'oper1 must be one of: ==, ~=, >, >=, <, <=, it was:'+oper1 return y
def iahomothin(): from iase2hmt import iase2hmt from iabinary import iabinary Iab = iase2hmt(iabinary([[0, 0, 0], [0, 1, 0], [1, 1, 1]]), iabinary([[1, 1, 1], [0, 0, 0], [0, 0, 0]])) return Iab
def iahomothick(): from iase2hmt import iase2hmt from iabinary import iabinary Iab = iase2hmt(iabinary([[1, 1, 1], [0, 0, 0], [0, 0, 0]]), iabinary([[0, 0, 0], [0, 1, 0], [1, 1, 1]])) return Iab
def iasedisk(r=3, DIM="2D", METRIC="EUCLIDEAN", FLAT="FLAT", h=0): from iabinary import iabinary from iasecross import iasecross from iasedil import iasedil from iasesum import iasesum from iasebox import iasebox from iaintersec import iaintersec from iagray import iagray METRIC = upper(METRIC) FLAT = upper(FLAT) assert DIM=='2D','Supports only 2D structuring elements' if FLAT=='FLAT': y = iabinary([1]) else: y = int32([h]) if r==0: return y if METRIC == 'CITY-BLOCK': if FLAT == 'FLAT': b = iasecross(1) else: b = int32([[-2147483647, 0,-2147483647], [ 0, 1, 0], [-2147483647, 0,-2147483647]]) return iasedil(y,iasesum(b,r)) elif METRIC == 'CHESSBOARD': if FLAT == 'FLAT': b = iasebox(1) else: b = int32([[1,1,1], [1,1,1], [1,1,1]]) return iasedil(y,iasesum(b,r)) elif METRIC == 'OCTAGON': if FLAT == 'FLAT': b1,b2 = iasebox(1),iasecross(1) else: b1 = int32([[1,1,1],[1,1,1],[1,1,1]]) b2 = int32([[-2147483647, 0,-2147483647], [ 0, 1, 0], [-2147483647, 0,-2147483647]]) if r==1: return b1 else: return iasedil( iasedil(y,iasesum(b1,r/2)) ,iasesum(b2,(r+1)/2)) elif METRIC == 'EUCLIDEAN': v = arange(-r,r+1) x = resize(v, (len(v), len(v))) y = transpose(x) Be = iabinary(sqrt(x*x + y*y) <= (r+0.5)) if FLAT=='FLAT': return Be be = h + int32( sqrt( maximum((r+0.5)*(r+0.5) - (x*x) - (y*y),0))) be = iaintersec( iagray(Be,'int32'),be) return be else: assert 0,'Non valid metric' return B
def iasedisk(r=3, DIM="2D", METRIC="EUCLIDEAN", FLAT="FLAT", h=0): from iabinary import iabinary from iasecross import iasecross from iasedil import iasedil from iasesum import iasesum from iasebox import iasebox from iaintersec import iaintersec from iagray import iagray METRIC = METRIC.upper() FLAT = FLAT.upper() assert DIM == '2D', 'Supports only 2D structuring elements' if FLAT == 'FLAT': y = iabinary([1]) else: y = int32([h]) if r == 0: return y if METRIC == 'CITY-BLOCK': if FLAT == 'FLAT': b = iasecross(1) else: b = int32([[-2147483647, 0, -2147483647], [0, 1, 0], [-2147483647, 0, -2147483647]]) return iasedil(y, iasesum(b, r)) elif METRIC == 'CHESSBOARD': if FLAT == 'FLAT': b = iasebox(1) else: b = int32([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) return iasedil(y, iasesum(b, r)) elif METRIC == 'OCTAGON': if FLAT == 'FLAT': b1, b2 = iasebox(1), iasecross(1) else: b1 = int32([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) b2 = int32([[-2147483647, 0, -2147483647], [0, 1, 0], [-2147483647, 0, -2147483647]]) if r == 1: return b1 else: return iasedil(iasedil(y, iasesum(b1, r / 2)), iasesum(b2, (r + 1) / 2)) elif METRIC == 'EUCLIDEAN': v = arange(-r, r + 1) x = resize(v, (len(v), len(v))) y = transpose(x) Be = iabinary(sqrt(x * x + y * y) <= (r + 0.5)) if FLAT == 'FLAT': return Be be = h + int32( sqrt(maximum((r + 0.5) * (r + 0.5) - (x * x) - (y * y), 0))) be = iaintersec(iagray(Be, 'int32'), be) return be else: assert 0, 'Non valid metric' return B
def iaendpoints(OPTION="LOOP"): from iase2hmt import iase2hmt from iabinary import iabinary Iab = None OPTION = upper(OPTION) if OPTION == "LOOP": Iab = iase2hmt(iabinary([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), iabinary([[0, 0, 0], [1, 0, 1], [1, 1, 1]])) elif OPTION == "HOMOTOPIC": Iab = iase2hmt(iabinary([[0, 1, 0], [0, 1, 0], [0, 0, 0]]), iabinary([[0, 0, 0], [1, 0, 1], [1, 1, 1]])) return Iab
def iaendpoints(OPTION="LOOP"): from iase2hmt import iase2hmt from iabinary import iabinary Iab = None OPTION = OPTION.upper() if OPTION == 'LOOP': Iab = iase2hmt(iabinary([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), iabinary([[0, 0, 0], [1, 0, 1], [1, 1, 1]])) elif OPTION == 'HOMOTOPIC': Iab = iase2hmt(iabinary([[0, 1, 0], [0, 1, 0], [0, 0, 0]]), iabinary([[0, 0, 0], [1, 0, 1], [1, 1, 1]])) return Iab
def iaskelmrec(f, B=None): from iabinary import iabinary from iaintersec import iaintersec from iadil import iadil from iaunion import iaunion from 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 iaskelmrec(f, B=None): from iabinary import iabinary from iaintersec import iaintersec from iadil import iadil from iaunion import iaunion from 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 iacmp(f1, oper, f2, oper1=None, f3=None): from iaintersec import iaintersec from iabinary import iabinary if oper == '==': y = (f1==f2) elif oper == '~=': y = (f1!=f2) elif oper == '<=': y = (f1<=f2) elif oper == '>=': y = (f1>=f2) elif oper == '>': y = (f1> f2) elif oper == '<': y = (f1< f2) else: assert 0, 'oper must be one of: ==, ~=, >, >=, <, <=, it was:'+oper if oper1 != None: if oper1 == '==': y = iaintersec(y, f2==f3) elif oper1 == '~=': y = iaintersec(y, f2!=f3) elif oper1 == '<=': y = iaintersec(y, f2<=f3) elif oper1 == '>=': y = iaintersec(y, f2>=f3) elif oper1 == '>': y = iaintersec(y, f2> f3) elif oper1 == '<': y = iaintersec(y, f2< f3) else: assert 0, 'oper1 must be one of: ==, ~=, >, >=, <, <=, it was:'+oper1 y = iabinary(y) 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 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 iais(f1, oper, f2=None, oper1=None, f3=None): from iaisbinary import iaisbinary from iaisequal import iaisequal from iaislesseq import iaislesseq from ianeg import ianeg from iathreshad import iathreshad from iabinary import iabinary if f2 == None: oper = upper(oper) if oper == "BINARY": return iaisbinary(f1) elif oper == "GRAY": return not iaisbinary(f1) else: assert 0, "oper should be BINARY or GRAY, was" + oper elif oper == "==": y = iaisequal(f1, f2) elif oper == "~=": y = not iaisequal(f1, f2) elif oper == "<=": y = iaislesseq(f1, f2) elif oper == ">=": y = iaislesseq(f2, f1) elif oper == ">": y = iaisequal(ianeg(iathreshad(f2, f1)), iabinary(1)) elif oper == "<": y = iaisequal(ianeg(iathreshad(f1, f2)), iabinary(1)) else: assert 0, "oper must be one of: ==, ~=, >, >=, <, <=, it was:" + oper if oper1 != None: if oper1 == "==": y = y and iaisequal(f2, f3) elif oper1 == "~=": y = y and (not iaisequal(f2, f3)) elif oper1 == "<=": y = y and iaislesseq(f2, f3) elif oper1 == ">=": y = y and iaislesseq(f3, f2) elif oper1 == ">": y = y and iaisequal(ianeg(iathreshad(f3, f2)), iabinary(1)) elif oper1 == "<": y = y and iaisequal(ianeg(iathreshad(f2, f3)), iabinary(1)) else: assert 0, "oper1 must be one of: ==, ~=, >, >=, <, <=, it was:" + oper1 return y
def iasecross(r=1): from iasesum import iasesum from iabinary import iabinary B = iasesum( iabinary([[0,1,0], [1,1,1], [0,1,0]]),r) return B
def iaregmax(f, Bc=iasecross()): from iasubm import iasubm from iahmax import iahmax from iabinary import iabinary from iaregmin import iaregmin from ianeg import ianeg y = iasubm(f, iahmax(f,1,Bc)) return iabinary(y)
def iaregmax(f, Bc=iasecross()): from iasubm import iasubm from iahmax import iahmax from iabinary import iabinary from iaregmin import iaregmin from ianeg import ianeg y = iasubm(f, iahmax(f, 1, Bc)) return iabinary(y)
def iasebox(r=1): from iasesum import iasesum from iabinary import iabinary B = iasesum( iabinary([[1,1,1], [1,1,1], [1,1,1]]),r) return B
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 = upper(type) rec_flag = find(type, '-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.' 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 iaregmin(f, Bc=iasecross(), option="binary"): from iahmin import iahmin from iaaddm import iaaddm from iasubm import iasubm from iabinary import iabinary from iasuprec import iasuprec from iaunion import iaunion from iathreshad import iathreshad if option != "binary": raise Exception("iaregmin accepts only binary option") return iabinary(iasubm(iahmin(f,1,Bc), f))
def iatoggle(f, f1, f2, OPTION="GRAY"): from iabinary import iabinary from iasubm import iasubm from iagray import iagray from iaunion import iaunion from iaintersec import iaintersec from ianeg import ianeg y = iabinary(iasubm(f, f1), iasubm(f2, f)) if upper(OPTION) == 'GRAY': t = iagray(y) y = iaunion(iaintersec(ianeg(t), f1), iaintersec(t, f2)) 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 = upper(type) rec_flag = find(type,'-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.' 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 iaseline(l=3, theta=0): from iaset2mat import iaset2mat from iabinary import iabinary theta = pi * theta / 180 if abs(tan(theta)) <= 1: s = sign(cos(theta)) x0 = arange(0, l * cos(theta) - (s * 0.5), s) x1 = floor(x0 * tan(theta) + 0.5) else: s = sign(sin(theta)) x1 = arange(0, l * sin(theta) - (s * 0.5), s) x0 = floor(x1 / tan(theta) + 0.5) x = transpose(array([x1, x0], int32)) B = iaset2mat((x, iabinary(ones((x.shape[1], 1))))) return B
def iatoggle(f, f1, f2, OPTION="GRAY"): from iabinary import iabinary from iasubm import iasubm from iagray import iagray from iaunion import iaunion from iaintersec import iaintersec from ianeg import ianeg y=iabinary( iasubm(f,f1),iasubm(f2,f)) if upper(OPTION) == 'GRAY': t=iagray(y) y=iaunion( iaintersec( ianeg(t),f1),iaintersec(t,f2)) return y
def iaseline(l=3, theta=0): from iaset2mat import iaset2mat from iabinary import iabinary theta = pi*theta/180 if abs(tan(theta)) <= 1: s = sign(cos(theta)) x0 = arange(0, l * cos(theta)-(s*0.5),s) x1 = floor(x0 * tan(theta) + 0.5) else: s = sign(sin(theta)) x1 = arange(0, l * sin(theta) - (s*0.5),s) x0 = floor(x1 / tan(theta) + 0.5) x = transpose(array([x1,x0],int32)) B = iaset2mat((x,iabinary(ones((x.shape[1],1))))) return B
def iaserot(B, theta=45, DIRECTION="CLOCKWISE"): from iamat2set import iamat2set from iabinary import iabinary from iaset2mat import iaset2mat DIRECTION = upper(DIRECTION) if DIRECTION == "ANTI-CLOCKWISE": theta = -theta SA = iamat2set(B) theta = pi * theta / 180 (y, v) = SA if len(y) == 0: return iabinary([0]) x0 = y[:, 1] * cos(theta) - y[:, 0] * sin(theta) x1 = y[:, 1] * sin(theta) + y[:, 0] * cos(theta) x0 = int32((x0 + 0.5) * (x0 >= 0) + (x0 - 0.5) * (x0 < 0)) x1 = int32((x1 + 0.5) * (x1 >= 0) + (x1 - 0.5) * (x1 < 0)) x = transpose(array([transpose(x1), transpose(x0)])) BROT = iaset2mat((x, v)) return BROT
def iaserot(B, theta=45, DIRECTION="CLOCKWISE"): from iamat2set import iamat2set from iabinary import iabinary from iaset2mat import iaset2mat DIRECTION = upper(DIRECTION) if DIRECTION == "ANTI-CLOCKWISE": theta = -theta SA = iamat2set(B) theta = pi * theta/180 (y,v)=SA if len(y)==0: return iabinary([0]) x0 = y[:,1] * cos(theta) - y[:,0] * sin(theta) x1 = y[:,1] * sin(theta) + y[:,0] * cos(theta) x0 = int32((x0 +0.5)*(x0>=0) + (x0-0.5)*(x0<0)) x1 = int32((x1 +0.5)*(x1>=0) + (x1-0.5)*(x1<0)) x = transpose(array([transpose(x1),transpose(x0)])) BROT = iaset2mat((x,v)) return BROT
def iasecross(r=1): from iasesum import iasesum from iabinary import iabinary B = iasesum(iabinary([[0, 1, 0], [1, 1, 1], [0, 1, 0]]), r) return B