Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def ialabel_unionfind(img, Bc):
    imshape = img.shape
    imsize = img.size

    # Offsets and neighbors
    offsets = MT.iase2off(Bc, 'fw')
    neighbors = MT.iaNlut(imshape, offsets)

    parents = np.arange(imsize, dtype=int)

    # Raster image and no-nzero pixels
    img = img.ravel()
    nonzero_nodes = np.nonzero(img)[0]
    img = np.concatenate((img, np.array([0])))
    O = np.zeros(imsize, dtype=int)
    cur_label = 0
    # pass 1: backward scan, forward neighbors
    for p in nonzero_nodes[::-1]:
        for nb in neighbors[p]:
            if img[nb]:
                Union(nb, p, parents)
    # pass 2_1: root labeled
    pbool = parents[nonzero_nodes] == nonzero_nodes
    labels = np.arange(1, pbool.sum() + 1)
    O[nonzero_nodes[pbool]] = labels
    # pass 2_2: labeling root descendants
    for p in nonzero_nodes[~pbool]:
        O[p] = O[parents[p]]
    return O.reshape(imshape)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
def ialabelflat_unionfind(img, Bc, delta):
    imshape = img.shape
    imsize = img.size

    # Offsets and neighbors
    offsets = MT.iase2off(Bc, "fw")
    neighbors = MT.iaNlut(imshape, offsets)

    parents = np.arange(imsize, dtype=int)

    # Raster image and no-nzero pixels
    img = img.ravel()
    nonzero_nodes = np.nonzero(img)[0]
    img = np.concatenate((img, np.array([0])))
    g = np.zeros(imsize, dtype=int)
    cur_label = 0
    # pass 1: backward scan, forward neighbors
    for p in nonzero_nodes[::-1]:
        v = img[p]
        for nb in neighbors[p]:
            if img[nb] and (abs(v - img[nb]) <= delta):
                Union(nb, p, parents)
    # pass 2_1: root labeled
    pbool = parents[nonzero_nodes] == nonzero_nodes
    labels = np.arange(1, pbool.sum() + 1)
    g[nonzero_nodes[pbool]] = labels
    # pass 2_2: labeling root descendants
    for p in nonzero_nodes[~pbool]:
        g[p] = g[parents[p]]
    return g.reshape(imshape)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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)