How to use the mahotas.stretch function in mahotas

To help you get started, we’ve selected a few mahotas 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_watershed.py View on Github external
binary = data > threshold

    image_set.add(
        "binary",
        cellprofiler.image.Image(
            image=binary, convert=False, dimensions=image.dimensions
        ),
    )

    module.run(workspace)

    original_shape = binary.shape

    distance = scipy.ndimage.distance_transform_edt(binary)

    distance = mahotas.stretch(distance)

    surface = distance.max() - distance

    if image.volumetric:
        footprint = numpy.ones((3, 3, 3))
    else:
        footprint = numpy.ones((3, 3))

    peaks = mahotas.regmax(distance, footprint)

    if image.volumetric:
        markers, _ = mahotas.label(peaks, numpy.ones((16, 16, 16)))
    else:
        markers, _ = mahotas.label(peaks, numpy.ones((16, 16)))

    expected = mahotas.cwatershed(surface, markers)
github luispedro / luispedro_org / files / talks / 2014 / 09-tcmm2014 / jug-segmentation-tutorial / saveimages.py View on Github external
import mahotas as mh
from jugfile import method1
from matplotlib import cm
import numpy as np

im = mh.imread('images/dna-21.jpg')
mh.imsave('image_stretched.jpeg', mh.stretch(im.astype(float)**.01))
m1 = method1.f('images/dna-21.jpg', 2)
m1 = m1.astype(np.uint8)
color = ((cm.rainbow(m1.astype(float)/m1.max())[:,:,:3]).reshape(m1.shape+(3,)))
color[m1 == 0] = (0,0,0)
mh.imsave('image_method1.jpeg', mh.stretch(color))

ref = mh.imread('references/dna-21.png')
color = ((cm.rainbow(ref.astype(float)/ref.max())[:,:,:3]).reshape(m1.shape+(3,)))
color[ref == 0] = (0,0,0)
mh.imsave('image_reference.jpeg', mh.stretch(color))
github luispedro / BuildingMachineLearningSystemsWithPython / ch10 / figure9.py View on Github external
#
# It is made available under the MIT License

import numpy as np
import mahotas as mh
image = mh.imread('../SimpleImageDataset/building05.jpg')
image = mh.colors.rgb2gray(image, dtype=np.uint8)

# We need to downsample ``image`` so that the details are visibly pixelated.
# This exaggerates the effect so that the result is clear
image = image[::4, ::4]
thresh = mh.sobel(image)
filtered = mh.sobel(image, just_filter=True)

thresh = mh.dilate(thresh, np.ones((7, 7)))
filtered = mh.dilate(mh.stretch(filtered), np.ones((7, 7)))

# Paste the thresholded and non-thresholded versions of the image side-by-side
h, w = thresh.shape
canvas = 255 * np.ones((h, w * 2 + 64), np.uint8)
canvas[:, :w] = thresh * 255
canvas[:, -w:] = filtered

mh.imsave('../1400OS_10_09+.jpg', canvas)
github luispedro / BuildingMachineLearningSystemsWithPython / ch10 / figure5_6.py View on Github external
# We now build a composite image with three panels:
#
# [ IM8 | | IM16 | | IM32 ]

h, w = im8.shape
canvas = np.ones((h, 3 * w + 256), np.uint8)
canvas *= 255
canvas[:, :w] = im8
canvas[:, w + 128:2 * w + 128] = im16
canvas[:, -w:] = im32
mh.imsave('../1400OS_10_05+.jpg', canvas[:, ::2])

# Threshold the image
# We need to first stretch it to convert to an integer image
im32 = mh.stretch(im32)
ot32 = mh.otsu(im32)

# Convert to 255 np.uint8 to match the other images
im255 = 255 * (im32 > ot32).astype(np.uint8)
mh.imsave('../1400OS_10_06+.jpg', im255)
github luispedro / BuildingMachineLearningSystemsWithPython / ch10 / figure13.py View on Github external
import numpy as np

# Adds a little salt-n-pepper noise to an image

im = mh.demos.load('lena')
im = rgb2grey(im)

# Salt & pepper arrays
salt = np.random.random(im.shape) > .975
pepper = np.random.random(im.shape) > .975

# salt is 170 & pepper is 30
# Some playing around showed that setting these to more extreme values looks
# very artificial. These look nicer

im = np.maximum(salt * 170, mh.stretch(im))
im = np.minimum(pepper * 30 + im * (~pepper), im)

mh.imsave('../1400OS_10_13+.jpg', im.astype(np.uint8))
github CellProfiler / CellProfiler / cellprofiler / modules / watershed.py View on Github external
if factor > 1:
                if x.volumetric:
                    factors = (1, factor, factor)
                else:
                    factors = (factor, factor)

                x_data = skimage.transform.downscale_local_mean(x_data, factors)

            threshold = skimage.filters.threshold_otsu(x_data)

            x_data = x_data > threshold

            distance = scipy.ndimage.distance_transform_edt(x_data)

            distance = mahotas.stretch(distance)

            surface = distance.max() - distance

            if x.volumetric:
                footprint = numpy.ones(
                    (self.footprint.value, self.footprint.value, self.footprint.value)
                )
            else:
                footprint = numpy.ones((self.footprint.value, self.footprint.value))

            peaks = mahotas.regmax(distance, footprint)

            if x.volumetric:
                markers, _ = mahotas.label(peaks, numpy.ones((16, 16, 16)))
            else:
                markers, _ = mahotas.label(peaks, numpy.ones((16, 16)))
github luispedro / mahotas / mahotas / demos / nuclear_distance_watershed.py View on Github external
import mahotas as mh
from os import path
import numpy as np
from matplotlib import pyplot as plt

nuclear = mh.demos.load('nuclear')
nuclear = nuclear[:,:,0]
nuclear = mh.gaussian_filter(nuclear, 1.)
threshed  = (nuclear > nuclear.mean())
distances = mh.stretch(mh.distance(threshed))
Bc = np.ones((9,9))

maxima = mh.morph.regmax(distances, Bc=Bc)
spots,n_spots = mh.label(maxima, Bc=Bc)
surface = (distances.max() - distances)
areas = mh.cwatershed(surface, spots)
areas *= threshed



import random
from matplotlib import colors
from matplotlib import cm
cols = [cm.jet(c) for c in range(0, 256, 4)]
random.shuffle(cols)
cols[0] = (0.,0.,0.,1.)