How to use the skimage.morphology function in skimage

To help you get started, we’ve selected a few skimage examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CellProfiler / CellProfiler / tests / modules / test_removeholes.py View on Github external
module.y_name.value = "output"

    module.size.value = 3.0

    module.run(workspace)

    actual = workspace.image_set.get_image("output").pixel_data

    if image.dimensions == 2:
        factor = (3.0 / 2.0) ** 2
    else:
        factor = (4.0 / 3.0) * ((3.0 / 2.0) ** 3)

    data = skimage.img_as_bool(image.pixel_data)

    expected = skimage.morphology.remove_small_holes(data, numpy.pi * factor)

    numpy.testing.assert_array_equal(actual, expected)
github DeepinSC / PyTorch-Luna16 / data_processing / LUNA_code / LUNA_segment_lung_ROI.py View on Github external
# and background (radio transparent tissue ie lungs)
        # Doing this only on the center of the image to avoid 
        # the non-tissue parts of the image as much as possible
        #
        kmeans = KMeans(n_clusters=2).fit(np.reshape(middle,[np.prod(middle.shape),1]))
        centers = sorted(kmeans.cluster_centers_.flatten())
        threshold = np.mean(centers)
        thresh_img = np.where(img
github jni / skan / skan / draw.py View on Github external
Dilate the skeleton by this amount. This is useful when rendering
        large images where aliasing may cause some pixels of the skeleton
        not to be drawn.
    axes : matplotlib Axes
        The Axes on which to plot the image. If None, new ones are created.

    Returns
    -------
    axes : matplotlib Axes
        The Axis on which the image is drawn.
    """
    image = _normalise_image(image, image_cmap=image_cmap)
    skeleton = skeleton.astype(bool)
    if dilate > 0:
        selem = morphology.disk(dilate)
        skeleton = morphology.binary_dilation(skeleton, selem)
    if axes is None:
        fig, axes = plt.subplots()
    image[skeleton] = alpha * np.array(color) + (1 - alpha) * image[skeleton]
    axes.imshow(image)
    axes.axis('off')
    return axes
github nanophotonics / nplab / nplab / analysis / mjh250-image-analysis.py View on Github external
def get_largest_binary_convex_hull(image):
	#assume image is binary
	return skimage.morphology.convex_hull_object(image)
github 3Scan / 3scan-skeleton / skeleton / treeVess.py View on Github external
def getTreeArray(branching=3, height=1, dims=3):
    if dims == 3:
        selem = morphology.ball(1)
    else:
        selem = morphology.disk(1)
    treeGraph = nx.balanced_tree(branching, height)
    maxIndex = max(treeGraph.nodes())
    dim = getDim(maxIndex, dims)
    dims = tuple([dim] * dims)
    treeArray = np.zeros(dims, dtype=np.uint8)
    for edge in treeGraph.edges():
        nzi1 = np.unravel_index(edge[0], dims=dims)
        nzi2 = np.unravel_index(edge[1], dims=dims)
        treeArray[tuple(nzi1)] = 1; treeArray[tuple(nzi2)] = 1;
        direction = (np.array(nzi1) - np.array(nzi2))
        dist = sqrt(np.sum((np.array(nzi1) - np.array(nzi2)) ** 2))
        normalized = direction / dist
        if dist < 1:
            for i in range(0, dist):
                treeArray[tuple((normalized * i) + nzi1)] = 1
    synVessels = morphology.binary_dilation(treeArray, selem)
github insarlab / MintPy / mintpy / objects / conncomp.py View on Github external
# remove regions with small area
        if print_msg:
            print('remove regions with area < {}'.format(int(min_area)))
        min_area = min(min_area, label_img.size * 3e-3)
        #flag_slabel = np.bincount(label_img.flatten()) < min_area
        #flag_slabel[0] = False
        #label_small = np.where(flag_slabel)[0]
        #for i in label_small:
        #    label_img[label_img == i] = 0
        mask = morph.remove_small_objects(label_img, min_size=min_area, connectivity=1)
        label_img[mask == 0] = 0
        label_img, num_label = measure.label(label_img, connectivity=1, return_num=True) # re-label

        # remove regions that would disappear after erosion operation
        erosion_structure = np.ones((erosion_size, erosion_size))
        label_erosion_img = morph.erosion(label_img, erosion_structure).astype(np.uint8)
        erosion_regions = measure.regionprops(label_erosion_img)
        if len(erosion_regions) < num_label:
            if print_msg:
                print('Some regions are lost during morphological erosion operation')
            label_erosion = [reg.label for reg in erosion_regions]
            for orig_reg in measure.regionprops(label_img):
                if orig_reg.label not in label_erosion:
                    if print_msg:
                        print('label: {}, area: {}, bbox: {}'.format(orig_reg.label,
                                                                     orig_reg.area,
                                                                     orig_reg.bbox))
                    label_img[label_img == orig_reg.label] = 0
        label_img, num_label = measure.label(label_img, connectivity=1, return_num=True) # re-label

        # get label boundaries to facilitate bridge finding
        if get_boundary:
github stefsmeets / instamatic / instamatic / montage.py View on Github external
plot : bool
            Plot the segmentation results and coordinates using Matplotlib

        Returns
        -------
        stagecoords : np.array, imagecoords : np.array
            Return both he stage and imagecoords as numpy arrays
        """
        from skimage import filters
        from skimage import morphology
        from skimage.measure import regionprops

        stitched = self.stitched

        thresh = filters.threshold_otsu(stitched)
        selem = morphology.disk(10)
        seg = morphology.binary_closing(stitched > thresh, selem=selem)

        labeled, _ = ndimage.label(seg)
        props = regionprops(labeled)

        stitched_binning = self.stitched_binning

        if not pixelsize:
            pixelsize = self.pixelsize * stitched_binning
        else:
            pixelsize *= stitched_binning

        if diameter is None:
            diameter = np.median([(prop.area ** 0.5) * pixelsize for prop in props])
            print(f'Diameter: {diameter:.0f} nm')
github Cysu / Person-Reid / scripts / learnattr.py View on Github external
def decomp_body(img, bpmap):
            imgs = [0] * len(bodyconf.groups)

            for i, grp in enumerate(bodyconf.groups):
                # mask = dilate(region_0 | region_1 | ... | region_k)
                regions = [(bpmap == pixval) for pixval in grp]
                mask = reduce(lambda x, y: x|y, regions)
                mask = morph.binary_dilation(mask, selem)
                imgs[i] = img * numpy.expand_dims(mask, axis=2)

            return imgs
github MStarmans91 / WORC / WORC / processing / segmentix.py View on Github external
PARAMETERS
    ----------
    contour: numpy array
            Array containing the contour

    radius: int, default 5
            Radius of ring to be extracted.
    '''
    contour = contour.astype(bool)
    radius = int(radius)
    disk = morphology.disk(radius)

    # Dilation with radius in axial direction
    for ind in range(contour.shape[0]):
        contour[ind, :, :] = morphology.binary_dilation(contour[ind, :, :],
                                                        disk)
    return contour
github junyanz / pytorch-CycleGAN-and-pix2pix / scripts / eval_cityscapes / cityscapes.py View on Github external
def make_boundaries(label, thickness=None):
        """
        Input is an image label, output is a numpy array mask encoding the boundaries of the objects
        Extract pixels at the true boundary by dilation - erosion of label.
        Don't just pick the void label as it is not exclusive to the boundaries.
        """
        assert(thickness is not None)
        import skimage.morphology as skm
        void = 255
        mask = np.logical_and(label > 0, label != void)[0]
        selem = skm.disk(thickness)
        boundaries = np.logical_xor(skm.dilation(mask, selem),
                                    skm.erosion(mask, selem))
        return boundaries