Beispiel #1
0
def threshold(img, thresh=None, size=9):
    """
    Segment image using a thresholding algorithm
    """
    if thresh is None:
        thresh = Stats.mode(img)
        thresh += np.std(img)

    # Take the binary image version "splitted" on the 'thresh' value
    img_bin = (img > thresh)
    
    # Clean fluctuations
    img_bin = Clean.spurious(img_bin)
    
    # Clean small objects
    img_bin = Clean.small(img_bin,size=9)
    
    return img_bin
Beispiel #2
0
def regionGrow(img, x, y, thresh=None):
    """
    Segment using a Region Growing technique
    """

    flag = 1
    x_o = x
    y_o = y
    
    if thresh is None:
        thresh = Stats.mode(img)
        thresh += np.std(img)

    # Initialize region with the seed point
    region = np.zeros(img.shape,dtype=np.bool)
    reg_old = (region==flag)

    if img[y_o,x_o] < thresh:
        return region
    
    region[y_o,x_o] = flag
    reg_cur = (region==flag)

    # For future morphological operations (MO)
    strct_elem = ndi.generate_binary_structure(2,2)

    # While region stills changes (grow), do...
    while not np.all(region == reg_old):
        reg_old = region.copy()
        reg_mean = np.mean(img[region==flag])
        #reg_area = np.where(region==flag)[0].size

        # Define pixel neighbours using MO dilation
        tmp = ndi.binary_dilation(region,strct_elem, 1)
        neigbors = tmp - region
        inds = np.where(neigbors)

        # Check for the new neighbors; do they fullfil requirements
        # to become members of the growing region?
        for y_i,x_i in zip(*inds):
            if (img[y_i,x_i] >= thresh):# and (img[y_i,x_i] <= reg_mean):
                region[y_i,x_i] = flag

    return region