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 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 test_connected_components_connectivity_option(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.segment import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 1, 1, 1],
                       [0, 0, 1, 1, 1],
                       [0, 0, 1, 1, 1]], dtype=np.uint8)

        segmentation = connected_components(ar, connectivity=1)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2, 3, 4]))

        segmentation = connected_components(ar, connectivity=2)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2]))
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 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 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_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
Example #8
0
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 vertical_cuts(thresholded_image):
    """Return vertical cuts to separate fused seeds."""
    n = 50
    selem = np.array([0, 1, 0] * n).reshape((n, 3))
    cuts = invert_binary(thresholded_image)
    cuts = connected_components(cuts, background=0)
    cuts = remove_large_regions(cuts, max_size=500).astype(bool)
    cuts = dilation_binary(cuts, selem=selem)
    cuts = invert_binary(cuts)
    return cuts
Example #11
0
def segment3D(microscopy_collection, series, threshold):
    """Return segmented plasmodesmata in 3D."""
    zstack = microscopy_collection.zstack_array(s=series)
    segmentation = np.zeros(zstack.shape, dtype=bool)
    ziterator = microscopy_collection.zstack_proxy_iterator(s=series)
    for z, proxy_image in enumerate(ziterator):
        image = proxy_image.image
        image = threshold_abs(image, threshold)
        segmentation[:, :, z] = image
    return connected_components(segmentation, background=0)
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)
Example #13
0
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 create_mask(image):
    """Return a mask for the region of interest."""
    selem = skimage.morphology.disk(2)
    im = equalize_adaptive_clahe(image)
    im = threshold_otsu(im)
    im = erosion_binary(im, selem)
    mask = np.ones(im.shape, dtype=bool)
    segmentation = connected_components(im, background=0)
    for i in segmentation.identifiers:
        region = segmentation.region_by_identifier(i)
        if region.area > 5000:
            mask[np.where(region.convex_hull)] = False
    return Image.from_array(mask)
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
Example #16
0
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 test_connected_components_acts_like_a_transform(self):
     from jicbioimage.segment import connected_components
     from jicbioimage.core.image import Image
     ar = np.array([[1, 1, 0, 0, 0],
                    [1, 1, 0, 0, 0],
                    [0, 0, 0, 0, 0],
                    [0, 0, 2, 2, 2],
                    [0, 0, 2, 2, 2]], dtype=np.uint8)
     im = Image.from_array(ar)
     self.assertEqual(len(im.history), 1)
     segmentation = connected_components(im)
     self.assertEqual(len(segmentation.history), 2)
     self.assertEqual(segmentation.history[-1],
                      "Applied connected_components transform")
Example #18
0
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 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)
Example #21
0
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 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_component_centroids(image):

    ccs = connected_components(image, background=0)

    return [map(int, r.centroid) for r in yield_regions(ccs)]
Example #25
0
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