Example #1
0
def find_mask(filename,show=False,outfile="output.jpg", downsample=20, compsizethresh=50,adapthresh=500,blur=10,dilation=10,erosion=1):
    p = Pipeline(filename=filename, downsample=20,dilation=10,erosion=1)

    p.open() #perform background subtraction
    blurnum = int(blur)
    blurary = (blurnum,blurnum)
    p.blur(blurary) #blur the image
    p.crop(factor=8) #crop out the black borders that are produced by the blur
    adapthreshnum = int(adapthresh)
    p.threshold(adapthreshnum) #do adaptive thresholding
    p.erode() #erode the thresholded image

    p.connected_components_iterative() #calculate the connected components
    """
    try:
        p.check_circularity() #check to make sure everything makes sense so far
    except:
        print "I think this one doesn't have the hole in it..."
        #p.save_to_file(p.saved_data,filename=outfile) #saved the masked image to a file
        return np.ones(p.data.shape) #return an empty mask
    """
    p.select_largest_component() #select the largest connected component

    p.connected_components_iterative(full=False) #calculate the connected components of the inverted image
    #p.select_largest_component() #selected the largest connected components of the inverted image (this should now be the interior of the hole)
    p.select_largest_component()

    p.dilate() #dilate the mask
    p.mask_original() #mask the original image with the calculated mask
    p.save_to_file(out=p.data, filename=outfile) #saved the masked image to a file
    if show:
        p.show(data=np.concatenate((p.saved_data,p.data),axis=1)) #show the mask for debugging

    return p.data