Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def iasecrop(f):
    import numpy as np
    from ia870 import ialimits, iagray
    k1,k2 = ialimits(f)
    if not f.shape[0] & 1: f = np.vstack([k1 * np.ones((1,f.shape[1]),f.dtype),f])
    if not f.shape[1] & 1: f = np.hstack([k1 * np.ones((f.shape[0],1),f.dtype),f])
    h,w = f.shape
    h1 = (f != k1).max(1).nonzero()[0]
    dh = min([h1[0],h-h1[-1]-1])
    fh = f[dh:h-dh,:]
    w1 = (fh != k1).max(0).nonzero()[0]
    dw = min([w1[0],w-w1[-1]-1])
    return fh[:,dw:w-dw]
Ejemplo n.º 3
0
def iagray(f, TYPE="uint8", k1=None):
    from ia870 import iabinary, iais, iamaxleveltype, ianeg, ialimits

    #f = (f > 0)
    ff = array([0],TYPE)
    kk1,kk2 = ialimits(ff)
    if k1!=None:
        kk2=k1
    if   TYPE == 'uint8'  : y = where(f,kk2,kk1).astype(uint8)
    elif TYPE == 'uint16' : y = where(f,kk2,kk1).astype(uint16)
    elif TYPE == 'int32'  : y = where(f,kk2,kk1).astype(int32)
    elif TYPE == 'int64'  : y = where(f,kk2,kk1).astype(int64)
    elif TYPE == 'float64': y = where(f,kk2,kk1).astype(float64)
    else:
        assert 0, 'type not supported:'+TYPE
    return y
Ejemplo n.º 4
0
def iagray(f, TYPE="uint8", k1=None):
    from ia870 import iabinary, iais, iamaxleveltype, ianeg, ialimits

    #f = (f > 0)
    ff = array([0], TYPE)
    kk1, kk2 = ialimits(ff)
    if k1 != None:
        kk2 = k1
    if TYPE == 'uint8': y = where(f, kk2, kk1).astype(uint8)
    elif TYPE == 'uint16': y = where(f, kk2, kk1).astype(uint16)
    elif TYPE == 'int32': y = where(f, kk2, kk1).astype(int32)
    elif TYPE == 'int64': y = where(f, kk2, kk1).astype(int64)
    elif TYPE == 'float64': y = where(f, kk2, kk1).astype(float64)
    else:
        assert 0, 'type not supported:' + TYPE
    return y
Ejemplo n.º 5
0
def iaset2mat(A):
    from ia870 import iabinary
    from ia870 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:
        print('Error: 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), v.dtype) * ialimits(v)[0]
    offset = x[:, 0] * w + x[:, 1] + (dh * w + dw)
    M.flat[offset] = v
    M = M.astype(v.dtype)

    return M