def test_erode_binary(self): from jicbioimage.transform import erode_binary from jicbioimage.core.image import Image array = np.array([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]], dtype=np.bool) expected = np.array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], dtype=np.bool) eroded = erode_binary(array) self.assertTrue(np.array_equal(expected, eroded)) self.assertTrue(isinstance(eroded, Image)) # The erode_binary function only makes sense on dtype bool. with self.assertRaises(TypeError): erode_binary(array.astype(np.uint8))
def find_grains(input_file, output_dir=None): """Return tuple of segmentaitons (grains, difficult_regions).""" name = fpath2name(input_file) name = "grains-" + name + ".png" if output_dir: name = os.path.join(output_dir, name) image = Image.from_file(input_file) intensity = mean_intensity_projection(image) image = remove_scalebar(intensity, np.mean(intensity)) image = threshold_abs(image, 75) image = invert(image) image = fill_holes(image, min_size=500) image = erode_binary(image, selem=disk(4)) image = remove_small_objects(image, min_size=500) image = dilate_binary(image, selem=disk(4)) dist = distance(image) seeds = local_maxima(dist) seeds = dilate_binary(seeds) # Merge spurious double peaks. seeds = connected_components(seeds, background=0) segmentation = watershed_with_seeds(dist, seeds=seeds, mask=image) # Need a copy to avoid over-writing original. initial_segmentation = np.copy(segmentation) # Remove spurious blobs. segmentation = remove_large_segments(segmentation, max_size=3000) segmentation = remove_small_segments(segmentation, min_size=500) props = skimage.measure.regionprops(segmentation) segmentation = remove_non_round(segmentation, props, 0.6) difficult = initial_segmentation - segmentation return segmentation, difficult
def find_grains(input_file, output_dir=None): """Return tuple of segmentaitons (grains, difficult_regions).""" name = fpath2name(input_file) name = "grains-" + name + ".png" if output_dir: name = os.path.join(output_dir, name) image = Image.from_file(input_file) intensity = mean_intensity_projection(image) # Median filter seems more robust than Otsu. # image = threshold_otsu(intensity) image = threshold_median(intensity, scale=0.8) image = invert(image) image = erode_binary(image, selem=disk(2)) image = dilate_binary(image, selem=disk(2)) image = remove_small_objects(image, min_size=200) image = fill_holes(image, min_size=50) dist = distance(image) seeds = local_maxima(dist) seeds = dilate_binary(seeds) # Merge spurious double peaks. seeds = connected_components(seeds, background=0) segmentation = watershed_with_seeds(dist, seeds=seeds, mask=image) # Remove spurious blobs. segmentation = remove_large_segments(segmentation, max_size=3000) segmentation = remove_small_segments(segmentation, min_size=100) return segmentation
def analyse_image(image): image = normalise(image) * 255 canvas = AnnotatedImage.from_grayscale(image) image = smooth_gaussian(image.astype(float), 5) image = threshold_abs(image, 30) image = erode_binary(image) image = remove_small_objects(image, 5) salem = skimage.morphology.disk(2) image = dilate_binary(image, salem) segmentation = connected_components(image, background=0) for i in segmentation.identifiers: color = pretty_color_from_identifier(i) region = segmentation.region_by_identifier(i) convex_hull = region.convex_hull outline = convex_hull.inner.border.dilate() canvas.mask_region(outline, color=color) return canvas
def generate_cross_section_mask(image): image = find_edges_sobel(image) image = threshold_otsu(image) image = dilate_binary(image, selem=skimage.morphology.disk(5)) image = remove_small_objects(image, 5000) image = fill_holes(image, 50000) image = erode_binary(image, selem=skimage.morphology.disk(10)) image = remove_small_objects(image, 50000) return image
def generate_mask(image): mask = threshold_abs(image, 6500) mask = remove_small_objects(mask, 50) mask = fill_holes(mask, 5) selem = disk(3) mask = erode_binary(mask, selem) mask = dilate_binary(mask, selem) return mask
def segment(image): cross_section_mask = generate_cross_section_mask(image) seeds = threshold_adaptive_median(image, 51) seeds = clip_mask(seeds, cross_section_mask) seeds = fill_holes(seeds, 10000) seeds = erode_binary(seeds) seeds = remove_small_objects(seeds, 10) seeds = connected_components(seeds, connectivity=1, background=0) cells = watershed_with_seeds(image, seeds=seeds, mask=cross_section_mask) return cells
def find_tubes(input_file, output_dir=None): """Return pollen tube segmentation.""" name = fpath2name(input_file) name = "tubes-" + name + ".png" if output_dir: name = os.path.join(output_dir, name) image = Image.from_file(input_file) intensity = mean_intensity_projection(image) image = find_edges_sobel(intensity) image = remove_scalebar(image, 0) image = threshold_otsu(image) image = dilate_binary(image, selem=disk(3)) image = erode_binary(image, selem=disk(3)) image = remove_small_objects(image, min_size=500) image = fill_holes(image, min_size=500) image = erode_binary(image, selem=disk(3)) image = remove_small_objects(image, min_size=200) segmentation = connected_components(image, background=0) return segmentation
def analyse_file_org(fpath, output_directory): """Analyse a single file.""" logging.info("Analysing file: {}".format(fpath)) image = Image.from_file(fpath) image = identity(image) image = select_red(image) image = invert(image) image = threshold_otsu(image) seeds = remove_small_objects(image, min_size=1000) seeds = fill_small_holes(seeds, min_size=1000) seeds = erode_binary(seeds, selem=disk(30)) seeds = connected_components(seeds, background=0) watershed_with_seeds(-image, seeds=seeds, mask=image)
def test_erode_binary_with_selem(self): from jicbioimage.transform import erode_binary selem = np.ones((3, 3)) array = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]], dtype=np.bool) expected = np.array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], dtype=np.bool) eroded = erode_binary(array, selem=selem) self.assertTrue(np.array_equal(expected, eroded))
def segment(image): """Return field plots.""" red = red_channel(image) green = green_channel(image) image = difference(red, green) mask = threshold_otsu(image) mask = remove_small_objects(mask, min_size=1000) mask = fill_small_holes(mask, min_size=100) seeds = erode_binary(mask, selem=disk(10)) seeds = remove_small_objects(seeds, min_size=100) seeds = connected_components(seeds, background=0) return watershed_with_seeds(-image, seeds=seeds, mask=mask)
def segment(image, seeds=None): """Return field plots.""" green = green_channel(image) image = sklocal(green) image = skmean(image) mask = threshold_otsu(image) mask = remove_small_objects(mask, min_size=1000) mask = fill_small_holes(mask, min_size=100) dist = distance_transform(mask) if seeds is None: seeds = erode_binary(mask, selem=disk(10)) seeds = remove_small_objects(seeds, min_size=100) seeds = connected_components(seeds, background=0) return watershed_with_seeds(image, seeds=seeds, mask=mask)
def segment_cells(wall_projection, surface, mask, **kwargs): """Return segmented cells as SegmentedImage.""" seeds = threshold_adaptive_median( wall_projection, block_size=kwargs["wall_threshold_adaptive_block_size"]) seeds = remove_small_objects( seeds, min_size=kwargs["wall_remove_small_objects_in_cell_min_size"]) seeds = invert(seeds) if "wall_erode_step" in kwargs and kwargs["wall_erode_step"]: seeds = erode_binary(seeds) seeds = remove_small_objects( seeds, min_size=kwargs["wall_remove_small_objects_in_wall_min_size"]) seeds = connected_components(seeds, connectivity=1, background=0) cells = watershed_with_seeds(-wall_projection, seeds=seeds) cells = remove_cells_not_in_mask(cells, mask) return cells