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 segment_cells(image, max_cell_size): """Return segmented cells.""" image = identity(image) wall = threshold_adaptive_median(image, block_size=101) seeds = remove_small_objects(wall, min_size=100) seeds = dilate_binary(seeds) seeds = invert(seeds) seeds = remove_small_objects(seeds, min_size=5) seeds = connected_components(seeds, background=0) segmentation = watershed_with_seeds(-image, seeds=seeds) segmentation = remove_large_segments(segmentation, max_cell_size) return segmentation, wall
def preprocess_and_segment(image): image = identity(image) image = threshold_adaptive(image) image = remove_small_objects(image) image = invert(image) image = remove_small_objects(image) image = invert(image) segmentation = connected_components(image, background=False) segmentation = clear_border(segmentation) segmentation = remove_small_regions(segmentation) return segmentation
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 mask_from_large_objects(image, max_size): tmp_autowrite = AutoWrite.on AutoWrite.on = False mask = remove_small_objects(image, min_size=max_size) mask = invert(mask) AutoWrite.on = tmp_autowrite return mask
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 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 segment(line_image, dilation): lines = convert_to_signal(line_image) lines = skeletonize(lines) lines = remove_small_objects(lines, min_size=10, connectivity=2) lines = dilate_binary(lines, selem=np.ones((1, dilation))) segmentation = connected_components(lines, background=0) return segmentation
def marker_segmentation(marker_intensity3D, wall_mask3D, threshold): """Return fluorescent marker segmentation.""" marker_intensity3D = marker_intensity3D * wall_mask3D markers2D = max_intensity_projection(marker_intensity3D) markers2D = threshold_abs(markers2D, threshold) markers2D = remove_small_objects(markers2D, min_size=50) return connected_components(markers2D, background=0)
def segment_markers(image, wall, threshold): """Return segmented markers.""" image = threshold_abs(image, threshold) image = marker_in_wall(image, wall) image = remove_small_objects(image, min_size=10) segmentation = connected_components(image, background=0) return segmentation
def fill_small_holes_in_region(region, min_size): aw = AutoWrite.on AutoWrite.on = False region = invert(region) region = remove_small_objects(region, min_size=min_size) region = invert(region) AutoWrite.on = aw return region
def fill_holes(image, size): autowrite_on = AutoWrite.on AutoWrite.on = False image = invert(image) image = remove_small_objects(image, size) image = invert(image) AutoWrite.on = AutoWrite return image
def fill_small_holes(image, min_size): aw = AutoWrite.on AutoWrite.on = False image = invert(image) image = remove_small_objects(image, min_size=min_size) image = invert(image) AutoWrite.on = aw return image
def fill_holes(image, min_size): """Return image with holes filled in.""" tmp_autowrite_on = AutoWrite.on AutoWrite.on = False image = invert(image) image = remove_small_objects(image, min_size=min_size) image = invert(image) AutoWrite.on = tmp_autowrite_on return image
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 find_angle(image): image = equalize_adaptive_clahe(image) image = threshold_otsu(image) image = erosion_binary(image, selem=skimage.morphology.disk(3)) image = remove_small_objects(image, min_size=5000) segmentation = connected_components(image, background=0) properties = skimage.measure.regionprops(segmentation) angles = [p["orientation"] for p in properties] return sum(angles) / len(angles)
def cell_segmentation(wall_intensity2D, wall_mask2D, max_cell_size): """Return image segmented into cells.""" seeds = dilate_binary(wall_mask2D) seeds = invert(seeds) seeds = remove_small_objects(seeds, min_size=10) seeds = connected_components(seeds, background=0) segmentation = watershed_with_seeds(-wall_intensity2D, seeds=seeds) segmentation = remove_large_segments(segmentation, max_cell_size) return segmentation
def segment_zslice(image): """Segment a zslice.""" tmp_autowrite = AutoWrite.on AutoWrite.on = False image = identity(image) image = threshold_abs(image, 100) image = remove_small_objects(image, min_size=500) AutoWrite.on = tmp_autowrite return image
def generate_seeds(image): seeds = white_tophat(image, 10) seeds = threshold_abs(seeds, 1500) seeds = remove_small_objects(seeds, 50) selem = disk(5) seeds = dilate_binary(seeds, selem) return connected_components(seeds, background=0)
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 remove_small_objects_in_plane(im3d, min_size): tmp_auto_write_on = AutoWrite.on AutoWrite.on = False stack = [] ydim, xdim, zdim = im3d.shape for i in range(zdim): slice_hull = remove_small_objects(im3d[:, :, i], min_size) stack.append(slice_hull) AutoWrite.on = tmp_auto_write_on return np.dstack(stack).view(Image3D)
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 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
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_remove_small_objects(self): from jicbioimage.transform import remove_small_objects from jicbioimage.core.image import Image array = np.array([[0, 0, 0, 0, 1], [0, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 1, 0], [1, 1, 0, 1, 0]], dtype=np.bool) expected_con1 = np.array( [[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], dtype=np.bool) no_small = remove_small_objects(array, min_size=4) self.assertTrue(np.array_equal(expected_con1, no_small)) self.assertTrue(isinstance(no_small, Image)) expected_con2 = np.array( [[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 1, 0]], dtype=np.bool) no_small = remove_small_objects(array, min_size=4, connectivity=2) self.assertTrue(np.array_equal(expected_con2, no_small)) # The remove_small_objects function only makes sense on dtype np.bool. with self.assertRaises(TypeError): remove_small_objects(array.astype(np.uint8))
def segment(image): """Return a segmented image and rotation angle.""" angle = find_angle(image) image = rotate(image, angle) mask = create_mask(image) watershed_mask = equalize_adaptive_clahe(image) watershed_mask = smooth_gaussian(watershed_mask, sigma=(1, 0)) watershed_mask = threshold_otsu(watershed_mask) watershed_mask = apply_mask(watershed_mask, mask) n = 20 selem = np.array([0, 1, 0] * n).reshape((n, 3)) seeds = erosion_binary(watershed_mask, selem=selem) seeds = apply_mask(seeds, vertical_cuts(watershed_mask)) seeds = remove_small_objects(seeds) seeds = connected_components(seeds, connectivity=1, background=0) segmentation = watershed_with_seeds(image, seeds, mask=watershed_mask) segmentation = remove_cells_touching_border(segmentation, image) segmentation = remove_cells_touching_border(segmentation, mask) segmentation = remove_tilted_cells(segmentation) return segmentation, angle
def find_seeds(image): seeds = threshold_abs(image, 200) seeds = remove_small_objects(seeds, min_size=1000) seeds = connected_components(seeds, background=0) return seeds