def membrane_image_segmentation(img, gaussian_sigma=0.75, h_min=None, segmentation_gaussian_sigma=0.5, volume_threshold=None): voxelsize = np.array(img.voxelsize) if h_min is None: h_min = 2 if img.dtype==np.uint8 else 1000 smooth_image = nd.gaussian_filter(img, sigma=gaussian_sigma / voxelsize).astype(img.dtype) smooth_img = SpatialImage(smooth_image, voxelsize=voxelsize) ext_img = h_transform(smooth_img, h=h_min, method='min') seed_img = region_labeling(ext_img, low_threshold=1, high_threshold=h_min, method='connected_components') seg_smooth_image = nd.gaussian_filter(img, sigma=segmentation_gaussian_sigma / voxelsize).astype(img.dtype) seg_smooth_img = SpatialImage(seg_smooth_image, voxelsize=voxelsize) seg_img = segmentation(seg_smooth_img, seed_img, control='first', method='seeded_watershed') if volume_threshold is not None: seg_volumes = dict(zip(np.arange(seg_img.max()) + 1, nd.sum(np.prod(voxelsize) * np.ones_like(seg_img), seg_img, index=np.arange(seg_img.max()) + 1))) labels_to_remove = np.array(list(seg_volumes.keys()))[np.array(list(seg_volumes.values())) > volume_threshold] print("--> Removing too large labels :", labels_to_remove) for l in labels_to_remove: seg_img[seg_img == l] = 1 return seg_img
def test_plugin(): im = imread(data_path('segmentation_src.inr')) im_ref = imread(data_path('segmentation_seeded_watershed.inr')) smooth_img = linear_filtering(im, std_dev=2.0, method='gaussian_smoothing') regext_img = h_transform(smooth_img, h=5, method='h_transform_min') conn_img = region_labeling(regext_img, low_threshold=1, high_threshold=3, method='connected_components') wat_img = segmentation(smooth_img, conn_img, control='first', method='seeded_watershed') np.testing.assert_array_equal(wat_img, im_ref)
def test_plugin(): im = imread(data_path('segmentation_src.inr')) im_ref = imread(data_path('segmentation_seeded_watershed.inr')) smooth_img = linear_filtering(im, std_dev=2.0, method='gaussian_smoothing') regext_img = h_transform(smooth_img, h=5, method='h_transform_min') conn_img = region_labeling(regext_img, low_threshold=1, high_threshold=3, method='connected_components') wat_img = segmentation(smooth_img, conn_img, control='first', method='seeded_watershed') np.testing.assert_array_equal(wat_img, im_ref)
print "\n# - Automatic seed detection..." from timagetk.plugins import morphology from timagetk.plugins import h_transform from timagetk.plugins import region_labeling from timagetk.plugins import linear_filtering std_dev = 1.0 morpho_radius = 1.0 h_min = 2200 # asf_img = morphology(img2seg, max_radius=morpho_radius, method='co_alternate_sequential_filter') # ext_img = h_transform(asf_img, h=h_min, method='h_transform_min') smooth_img = linear_filtering(img2seg, std_dev=std_dev, method='gaussian_smoothing') ext_img = h_transform(smooth_img, h=h_min, method='h_transform_min') seed_img = region_labeling(ext_img, low_threshold=1, high_threshold=h_min, method='connected_components') print "Detected {} seeds!".format(len(np.unique(seed_img))) print "\n - Performing seeded watershed segmentation..." from timagetk.plugins import segmentation std_dev = 1.0 # smooth_img = linear_filtering(img2seg, std_dev=std_dev, method='gaussian_smoothing') seg_im = segmentation(smooth_img, seed_img, method='seeded_watershed') seg_im[seg_im == 0] = back_id # world.add(seg_im, 'seg', colormap='glasbey', alphamap='constant') from timagetk.io import imsave imsave(seg_img_fname, seg_im) else: from timagetk.io import imread
from timagetk.plugins import h_transform from timagetk.plugins import region_labeling from timagetk.plugins import seeded_watershed ext_img = h_transform(asf_image, h=selected_hmin, method='min', connectivity=connect) if ext_img is None: raise ValueError( "Function 'h_transform' returned a NULL image, aborting automatic segmentation!" ) seed_img = region_labeling(ext_img, low_threshold=1, high_threshold=selected_hmin, method='connected_components', connectivity=connect) if seed_img is None: raise ValueError( "Function 'region_labeling' returned a NULL image, aborting automatic segmentation!" ) n_seeds = len(np.unique(seed_img)) - 1 # '0' is in the list! print("Detected {} seeds!".format(n_seeds)) from timagetk.util import _method_check from timagetk.plugins.segmentation import WATERSHED_CONTROLS, DEFAULT_CONTROL control = _method_check(control, WATERSHED_CONTROLS, DEFAULT_CONTROL) params = '-labelchoice ' + str(control)
from timagetk.plugins import labels_post_processing except ImportError: raise ImportError('Import Error') out_path = './results/' # to save results # we consider an input image # SpatialImage instance input_img = imread(data_path('input.tif')) # optional denoising block smooth_img = linear_filtering(input_img, std_dev=2.0, method='gaussian_smoothing') asf_img = morphology(smooth_img, max_radius=3, method='co_alternate_sequential_filter') ext_img = h_transform(asf_img, h=150, method='h_transform_min') con_img = region_labeling(ext_img, low_threshold=1, high_threshold=150, method='connected_components') seg_img = segmentation(smooth_img, con_img, control='first', method='seeded_watershed') # optional post processig block pp_img = labels_post_processing(seg_img, radius=1, iterations=1, method='labels_erosion') res_name = 'example_segmentation.tif' imsave(out_path+res_name, pp_img)
def seg_pipe(img2seg, h_min, img2sub=None, iso=True, equalize=True, stretch=False, std_dev=0.8, min_cell_volume=20., back_id=1, to_8bits=False): """Define the sementation pipeline Parameters ---------- img2seg : str image to segment. h_min : int h-minima used with the h-transform function img2sub : str, optional image to subtract to the image to segment. iso : bool, optional if True (default), isometric resampling is performed after h-minima detection and before watershed segmentation equalize : bool, optional if True (default), intensity adaptative equalization is performed before h-minima detection stretch : bool, optional if True (default, False), intensity histogram stretching is performed before h-minima detection std_dev : float, optional real unit standard deviation used for Gaussian smoothing of the image to segment min_cell_volume : float, optional minimal volume accepted in the segmented image back_id : int, optional the background label to_8bits : bool, optional transform the image to segment as an unsigned 8 bits image for the h-transform and seed-labelleing steps Returns ------- seg_im : SpatialImage the labelled image obtained by seeded-watershed Notes ----- * Both 'equalize' & 'stretch' can not be True at the same time since they work on the intensity of the pixels; * Signal subtraction is performed after intensity rescaling (if any); * Linear filtering (Gaussian smoothing) is performed before h-minima transform for local minima detection; * Gaussian smoothing should be performed on isometric images, if the provided image is not isometric, we resample it before smoothing, then go back to original voxelsize; * In any case H-Transfrom is performed on the image with its native resolution to speed upd seed detection; * Same goes for connexe components detection (seed labelling); * Segmentation will be performed on the isometric images if iso is True, in such case we resample the image of detected seeds and use the isometric smoothed intensity image; """ t_start = time.time() # - Check we have only one intensity rescaling method called: try: assert equalize + stretch < 2 except AssertionError: raise ValueError( "Both 'equalize' & 'stretch' can not be True at once!") # - Check the standard deviation value for Gaussian smoothing is valid: try: assert std_dev <= 1. except AssertionError: raise ValueError( "Standard deviation for Gaussian smoothing should be superior or equal to 1!" ) ori_vxs = img2seg.voxelsize ori_shape = img2seg.shape if img2sub is not None: print "\n - Performing signal substraction..." img2seg = signal_subtraction(img2seg, img2sub) if equalize: print "\n - Performing z-slices adaptative histogram equalisation on the intensity image to segment..." img2seg = z_slice_equalize_adapthist(img2seg) if stretch: print "\n - Performing z-slices histogram contrast stretching on the intensity image to segment..." img2seg = z_slice_contrast_stretch(img2seg) print "\n - Automatic seed detection...".format(h_min) # morpho_radius = 1.0 # asf_img = morphology(img2seg, max_radius=morpho_radius, method='co_alternate_sequential_filter') # ext_img = h_transform(asf_img, h=h_min, method='h_transform_min') min_vxs = min(img2seg.voxelsize) if std_dev < min_vxs: print " -- Isometric resampling prior to Gaussian smoothing...".format( std_dev) img2seg = isometric_resampling(img2seg) print " -- Gaussian smoothing with std_dev={}...".format(std_dev) iso_smooth_img = linear_filtering(img2seg, std_dev=std_dev, method='gaussian_smoothing', real=True) if std_dev < min_vxs: print " -- Down-sampling a copy back to original voxelsize (to use with `h-transform`)..." smooth_img = resample(iso_smooth_img, ori_vxs) if not np.allclose(ori_shape, smooth_img.shape): print "WARNING: shape missmatch after down-sampling from isometric image:" print " -- original image shape: {}".format(ori_shape) print " -- down-sampled image shape: {}".format(smooth_img.shape) else: print " -- Copying original image (to use with `h-transform`)..." smooth_img = iso_smooth_img if not iso: del iso_smooth_img # no need to keep this image after this step! print " -- H-minima transform with h-min={}...".format(h_min) if to_8bits: ext_img = h_transform(smooth_img.to_8bits(), h=h_min, method='h_transform_min') else: ext_img = h_transform(smooth_img, h=h_min, method='h_transform_min') if iso: smooth_img = iso_smooth_img # no need to keep both images after this step! print " -- Region labelling: connexe components detection..." seed_img = region_labeling(ext_img, low_threshold=1, high_threshold=h_min, method='connected_components') print "Detected {} seeds!".format(len(np.unique(seed_img)) - 1) # '0' is in the list! del ext_img # no need to keep this image after this step! print "\n - Performing seeded watershed segmentation..." if iso: seed_img = isometric_resampling(seed_img, option='label') if to_8bits: seg_im = segmentation(smooth_img.to_8bits(), seed_img, method='seeded_watershed') else: seg_im = segmentation(smooth_img, seed_img, method='seeded_watershed') # seg_im[seg_im == 0] = back_id print "Detected {} labels!".format(len(np.unique(seg_im))) if min_cell_volume > 0.: from vplants.tissue_analysis.spatial_image_analysis import SpatialImageAnalysis print "\n - Performing cell volume filtering..." spia = SpatialImageAnalysis(seg_im, background=None) vol = spia.volume() too_small_labels = [ k for k, v in vol.items() if v < min_cell_volume and k != 0 ] if too_small_labels != []: print "Detected {} labels with a volume < {}µm2".format( len(too_small_labels), min_cell_volume) print " -- Removing seeds leading to small cells..." spia = SpatialImageAnalysis(seed_img, background=None) seed_img = spia.get_image_without_labels(too_small_labels) print " -- Performing final seeded watershed segmentation..." seg_im = segmentation(smooth_img, seed_img, method='seeded_watershed') # seg_im[seg_im == 0] = back_id print "Detected {} labels!".format(len(np.unique(seg_im))) print "\nDone in {}s".format(round(time.time() - t_start, 3)) return seg_im