コード例 #1
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)
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
    def test_watershed_with_seeds_mask_option(self):
        from jicbioimage.segment import watershed_with_seeds
        ar = np.array([[0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 9, 0, 0],
                       [9, 9, 9, 9, 9, 9],
                       [0, 0, 0, 9, 0, 0],
                       [0, 0, 0, 9, 0, 0]], dtype=np.uint8)

        sd = np.array([[1, 0, 0, 0, 0, 2],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [3, 0, 0, 0, 0, 4]], dtype=np.uint8)

        ma = np.array([[1, 1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1, 1],
                       [1, 1, 1, 1, 1, 1],
                       [1, 1, 1, 0, 0, 0],
                       [1, 1, 1, 0, 0, 0]], dtype=bool)

        segmentation = watershed_with_seeds(ar, seeds=sd, mask=ma)
        self.assertEqual(segmentation.identifiers, set([1, 2, 3]))
        mask_size = len(segmentation[np.where(segmentation == 0)])
        self.assertEqual(mask_size, 6)
コード例 #5
0
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
コード例 #6
0
def segment(zstack):
    image = max_intensity_projection(zstack)

    seeds = generate_seeds(image)
    mask = generate_mask(image)

    segmentation = watershed_with_seeds(image, seeds=seeds, mask=mask)
    annotate_segmentation(image, segmentation)

    return segmentation
コード例 #7
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 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
コード例 #9
0
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)
コード例 #10
0
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)
コード例 #11
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
コード例 #12
0
def analyse_file(fpath, output_directory, test_data_only=False):
    """Analyse a single file."""
    logging.info("Analysing file: {}".format(fpath))
    AutoName.directory = output_directory

    image = Image.from_file(fpath)

    negative = get_negative_single_channel(image)
    seeds = find_seeds(negative)
    mask = find_mask(negative)

    eaten_leaf_segmentation = watershed_with_seeds(negative,
                                                   seeds=seeds,
                                                   mask=mask)
    whole_leaf_segmentation = post_process_segmentation(
        eaten_leaf_segmentation.copy())

    ann = annotate(image, whole_leaf_segmentation, eaten_leaf_segmentation)
    ann_fpath = os.path.join(output_directory, "annotated.png")
    with open(ann_fpath, "wb") as fh:
        fh.write(ann.png())
コード例 #13
0
    def test_watershed_with_seeds(self):
        from jicbioimage.segment import watershed_with_seeds
        from jicbioimage.segment import SegmentedImage

        ar = np.array([[0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 9, 0, 0],
                       [9, 9, 9, 9, 9, 9],
                       [0, 0, 0, 9, 0, 0],
                       [0, 0, 0, 9, 0, 0]], dtype=np.uint8)

        sd = np.array([[1, 0, 0, 0, 0, 2],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [3, 0, 0, 0, 0, 4]], dtype=np.uint8)

        segmentation = watershed_with_seeds(ar, seeds=sd)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2, 3, 4]))
コード例 #14
0
    def test_watershed_with_seeds_acts_like_a_transform(self):
        from jicbioimage.segment import watershed_with_seeds
        from jicbioimage.core.image import Image

        ar = np.array([[0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 9, 0, 0],
                       [9, 9, 9, 9, 9, 9],
                       [0, 0, 0, 9, 0, 0],
                       [0, 0, 0, 9, 0, 0]], dtype=np.uint8)
        im = Image.from_array(ar)
        self.assertEqual(len(im.history), 1)

        sd = np.array([[1, 0, 0, 0, 0, 2],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0],
                       [3, 0, 0, 0, 0, 4]], dtype=np.uint8)

        segmentation = watershed_with_seeds(im, seeds=sd)
        self.assertEqual(len(segmentation.history), 2)
        self.assertEqual(segmentation.history[-1],
                         "Applied watershed_with_seeds transform")
コード例 #15
0
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