Пример #1
0
def track_edge(im,start=None):
    edge = np.zeros(im.shape)
    im2 = pad_image(im)
    chain = []
    dir = 6
    
    # if it exists, find first 'on' point in image
    # row,col point encoding
    try:
        on = np.nonzero(im2)
        i,j =on[0][0],on[1][0]
        chain = [i-1,j-1]
        p0 = (i,j)
        point = np.array(p0)
    except:
        print 'No figure found'
        return edge, np.array(chain)
    
    # unless a start point is specified, use the first 'on' point as the start
    if start!=None:
        p0 = start
        point = np.array(p0)
        
    iter = 0
    # loop until break or too many iterations reached
    while True:
        iter+=1
        if iter >= len(im2.flatten()):
            print '***track_edge: max iterations reached with no end in sight...'
            break
        # set next direction to look, 1 step counterclockwise from where we came
        checkdir = (dir+7) % 8
        i,j = point + map8[checkdir]
        
        # if we hit a pixel in the region, move there and add the direction to the list
        if im2[i,j]==1:
            dir = checkdir
            point = point+map8[checkdir]
            edge[i-1,j-1]=1
            chain.append(dir)
            
            # if we've hit the start, then end the loop
            if (i,j) == p0:
                break
           
        # otherwise, check the next dir
        else:
            dir+=1
    
    return edge,np.array(chain)
Пример #2
0
def label_components(im):
    # initially pad label image so that we can the first row without error
    l_im = pad_image(np.zeros(im.shape))
    equiv = {0:0.0}
    
    # raster through image
    for i in range(im.shape[0]):
        for j in range(im.shape[1]):
            if im[i,j]==1:
                # find if any neighbors have already been labeled
                # if so, then take the smalles of all neighboring labels
                # otherwise, create a new label
                nearby = []
                for dir in [1,2,3,4]:
                    dirval = l_im[i+map8[dir][0]+1,j+map8[dir][1]+1]
                    if dirval > 0:
                        nearby.append(dirval)
                if nearby==[]:
                    newval = max(equiv)+1
                    nearby.append(newval)
                else:
                    newval = min(nearby) 
                    
                l_im[i+1,j+1] = newval
            
                # adjust equivalence table, but maintain previous corrections to the table
                for n in nearby:
                    if n in equiv:
                        equiv[n] = min(newval,equiv[n])
                    else:
                        equiv[n] = newval
    
    # adjust image according to equivalence table
    # go backwards through the keys so that chains of equivalences are handled properly
    for key in equiv.keys()[::-1]:
        l_im[l_im==key] = equiv[key]
    
    return l_im[1:-1,1:-1],equiv    # return without padding
    return l_im[1:-1,1:-1],equiv    # return without padding