def test_threshold_otsu_multiplier(self):
     from jicbioimage.transform import threshold_otsu
     array = np.array([[1, 2, 3], [7, 8, 9]], dtype=np.uint8)
     # Threshold used: 3 * 0.6 = 1.79
     expected = np.array([[0, 1, 1], [1, 1, 1]], dtype=np.bool)
     thresholded = threshold_otsu(array, multiplier=0.6)
     self.assertTrue(np.array_equal(expected, thresholded))
    def test_threshold_otsu(self):
        from jicbioimage.transform import threshold_otsu
        from jicbioimage.core.image import Image

        # Test with uint8.
        array = np.array([[1, 2, 3], [7, 8, 9]], dtype=np.uint8)
        expected = np.array([[0, 0, 0], [1, 1, 1]], dtype=np.bool)
        thresholded = threshold_otsu(array)
        self.assertTrue(np.array_equal(expected, thresholded))
        self.assertTrue(isinstance(thresholded, Image))

        # Test with float.
        array = np.array([[1, 2, 3], [7, 8, 9]], dtype=np.float)
        expected = np.array([[0, 0, 0], [1, 1, 1]], dtype=np.bool)
        thresholded = threshold_otsu(array)
        self.assertTrue(np.array_equal(expected, thresholded))
        self.assertTrue(isinstance(thresholded, Image))
Ejemplo n.º 3
0
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
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
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(stack):
    """Segment the stack into 3D regions representing cells."""
    mask = threshold_otsu(stack)
    mask = remove_small_objects_in_plane(mask, min_size=1000)
    mask = pseudo_convex_hull(mask)
    stack = identity(stack)
    stack = filter_median(stack)
    stack = gradient_magnitude(stack)
    stack = discrete_gaussian_filter(stack, 2.0)
    stack = morphological_watershed(stack, 0.664)
    identity(stack.view(PrettyColorImage3D))
    stack = filter_cells_outside_mask(stack, mask)
    stack = remove_border_segmentations(stack)
    identity(stack.view(PrettyColorImage3D))
    return stack
def fit_central_circle(image, radius_lower_bound=170, radius_upper_bound=190):
    """Find the centre and radius of the central circle in image. Analysis is
    restrictied the given bounds for the radius of the circle."""

    smoothed = smooth_gaussian(image.astype(np.float), sigma=5)
    edges = find_edges_sobel(smoothed)
    thresh = threshold_otsu(edges)

    hmm = 170, 190
    hough_radii = np.arange(140, 170, 2)
    hough_res = hough_circle(thresh, hough_radii)

    circles = find_n_best_hough_circles(hough_radii, hough_res, 1)
    circle = circles[0]

    return circle
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)
Ejemplo n.º 11
0
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
Ejemplo n.º 12
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