def process_image(image): tic = time.clock() # rescale intensity p2, p98 = np.percentile(image, (1, 99.9)) image = rescale_intensity(1.0*image, in_range=(p2, p98)) # do simple filter based on color value thresh = 0.5*threshold_func(image) filtered_image = np.zeros_like(image,dtype=np.uint8) # set up all-zero image filtered_image[image > thresh] = 1 # filtered values set to 1 # perform watershed transform to split clusters distance = ndi.distance_transform_edt(filtered_image) local_maxi = peak_local_max(distance, indices=False, footprint=morphology.square(7), labels=filtered_image, exclude_border=False) markers = ndi.label(local_maxi)[0] # segment and label particles labels = morphology.watershed(-distance, markers, mask=filtered_image) backup_labels = labels.copy() # remove boundaries and restore any small particles deleted in this process labels[find_boundaries(labels)] = 0 for i in np.unique(backup_labels)[1:]: if np.count_nonzero(labels[backup_labels == i]) == 0: labels[backup_labels == i] = i toc = time.clock() procTime = toc - tic return image, labels, procTime
p2, p98 = np.percentile(image, (1, 99.9)) image = rescale_intensity(1.0*image, in_range=(p2, p98)) # image = image[10:70,55:110] # truth = truth[10:70,55:110] # image = image[0:30,370:430] # truth = truth[0:30,370:430] # perform canny edge detection on image. Scale to max pixel value first max_pixel_value = np.max(image) edges = canny(image/float(max_pixel_value), sigma=1.0) # do simple filter based on color value thresh = 0.5*threshold_func(image) filtered_image = np.zeros_like(image,dtype=np.uint8) # set up all-zero image filtered_image[image > thresh] = 1 # filtered values set to 1 filtered_image1 = ndi.binary_fill_holes(edges) distance = ndi.distance_transform_edt(filtered_image) # label features and convert to rgb image labeled_particles, num_features = ndi.label(filtered_image) # labeled_slices = ndi.find_objects(labeled_particles) # # tot_features = num_features # for i in range(num_features): # tot_features = apply_watershed(labeled_particles[labeled_slices[i]],i+1, tot_features) image_label_overlay = label2rgb(labeled_particles, bg_label=0)