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
def find_particle(filename,show=True,debug=False): from find_mask import find_mask hole_mask = find_mask(filename,show=False) p = Pipeline(downsample=20,filename=filename) if debug: p.show() p.open(window_size=(20,20)) p.crop(factor=15) p.threshold() #do adaptive thresholding if debug: p.show() p.connected_components_iterative(full=False) #calculate the connected components p.threshold_component_size() if debug: p.show() #show the mask for debugging p.data = np.max(p.data) - p.data p.erode(factor=1) p.erode(factor=1) if debug: p.show() p.subtract(hole_mask) if debug: p.show() p.convex_hull_per_component() p.mask_original() p.show() #show the mask for debugging