How to use mahotas - 10 common examples

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 iamshang1 / Projects / Basic_ML / Image_Classification / traditional_svm.py View on Github external
def colors(image):
    image = image // 64
    r,g,b = image.transpose((2,0,1))
    pixels = 1 * r + 4 * b + 16 * g
    hist = np.bincount(pixels.ravel(), minlength=64)
    hist = hist.astype(float)
    hist = np.log1p(hist)
    return hist

for i in range(len(images)):
    print "processing image %i of %i" % (i+1, len(images)) 
    labels.append(images[i][:-len('00.jpg')])
    im = mh.imread(images[i])
    imgrey = mh.colors.rgb2gray(im, dtype=np.uint8)
    features.append(np.concatenate([mh.features.haralick(im).ravel(), mh.features.lbp(imgrey, 30, 10).ravel(), colors(im)]))
    surfim = mh.imread(images[i], as_grey=True)
    surfim = surfim.astype(np.uint8)
    alldescriptors.append(surf.dense(surfim, spacing=16))

concatenated = np.concatenate(alldescriptors)
print "fitting k mean clusters for surf descriptors"
km = KMeans(15)
km.fit(concatenated)
print "creating surf features"
sfeatures = []
for d in alldescriptors:
    c = km.predict(d)
    sfeatures.append(np.array([np.sum(c == ci) for ci in range(15)]))

features = np.array(features) 
sfeatures = np.array(sfeatures, dtype=float)
github hsSam / PracticalPythonAndOpenCV_CaseStudies / Chapter06 / utilities / dataset.py View on Github external
# When the width is greater than the height
	if image.shape[1] > image.shape[0]:
		image = imutils.resize(image, width=w)
	# When the height is greater than the width
	else:
		image = imutils.resize(image, height=h)

	# Save memory for the extent of the image and grab it
	extent = np.zeros((h, w), dtype="uint8")
	offset_x = (w - image.shape[1]) // 2
	offset_y = (h - image.shape[0]) // 2
	extent[offset_y:offset_y + image.shape[0], offset_x:offset_x + image.shape[1]] = image

	# Compute the center of mass of the image and then move the center of mass to the center of the image
	(c_y, c_x) = np.round(mahotas.center_of_mass(extent)).astype("int32")
	(d_x, d_y) = ((size[0] // 2) - c_x, (size[1] // 2) - c_y)
	matrix = np.float32([[1, 0, d_x], [0, 1, d_y]])
	extent = cv2.warpAffine(extent, matrix, size)

	# Return the extent of the image
	return extent
github apollos / opencv-practice / handwriting_recognition / pyimagesearch / utils / dataset.py View on Github external
if image.shape[1] > image.shape[0]:
		image = imutils.resize(image, width=eW)

	# otherwise, the height is greater than the width
	else:
		image = imutils.resize(image, height=eH)

	# allocate memory for the extent of the image and grab it
	extent = np.zeros((eH, eW), dtype="uint8")
	offsetX = (eW - image.shape[1]) // 2
	offsetY = (eH - image.shape[0]) // 2
	extent[offsetY:offsetY + image.shape[0], offsetX:offsetX + image.shape[1]] = image

	# compute the center of mass of the image and then move the center of mass to the center
	# of the image
	(cY, cX) = np.round(mahotas.center_of_mass(extent)).astype("int32")
	(dX, dY) = ((size[0] // 2) - cX, (size[1] // 2) - cY)
	M = np.float32([[1, 0, dX], [0, 1, dY]])
	extent = cv2.warpAffine(extent, M, size)

	# return the extent of the image
	return extent
github luispedro / mahotas / tests / test_watershed.py View on Github external
def cast_test(M,S,dtype):
        M = M.astype(dtype)
        S = S.astype(dtype)
        W = mahotas.cwatershed(2-S,M)
        assert sys.getrefcount(W) == 2
        assert np.all(W == np.array([[1, 1, 1, 1],
               [1, 1, 1, 1],
               [1, 1, 1, 1],
               [2, 2, 1, 1],
               [2, 2, 2, 2],
               [2, 2, 2, 2],
               [2, 2, 2, 2]]))
    for d in [np.uint8, np.int8, np.uint16, np.int16, np.int32, np.uint32,int]:
github luispedro / mahotas / tests / test_labeled.py View on Github external
def test_border():
    labeled = np.zeros((32,32), np.uint8)
    labeled[8:11] = 1
    labeled[11:14] = 2
    labeled[14:17] = 3
    labeled[10,8:] = 0
    b12 = mahotas.labeled.border(labeled, 1, 2)
    YX = np.where(b12)
    YX = np.array(YX).T
    b13 = mahotas.labeled.border(labeled, 1, 3)

    assert not np.any(b13)
    assert np.any(b12)
    assert (11,0) in YX
    assert (11,1) in YX
    assert (12,1) in YX
    assert (12,9) not in YX

    b13 = mahotas.labeled.border(labeled, 1, 3, always_return=0)
    assert b13 is None
github CellProfiler / CellProfiler / tests / modules / test_watershed.py View on Github external
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)

    expected = expected * binary

    expected = skimage.measure.label(expected)

    actual = workspace.get_objects("watershed")

    actual = actual.segmented

    numpy.testing.assert_array_equal(expected, actual)
github luispedro / mahotas / tests / test_watershed.py View on Github external
def test_mix_types():
    f = np.zeros((64,64), np.uint16)
    f += np.indices(f.shape)[1]**2
    f += (np.indices(f.shape)[0]-23)**2
    markers = np.zeros((64,64), np.int64)
    markers[32,32] = 1
# Below used to force a crash (at least in debug mode)
    a,b = mahotas.cwatershed(f, markers, return_lines=1)
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 / mahotas / tests / test_center_of_mass.py View on Github external
def test_labels_not_intc():
    img = np.arange(256).reshape((16,16))
    labels = img.copy()
    labels %= 3
    cm = mahotas.center_of_mass(img, labels)
    assert cm.shape == (3,2)

    labels = labels.T.copy()
    cm = mahotas.center_of_mass(img, labels.T)
    assert cm.shape == (3,2)

    labels = labels.T.copy()
    labels = labels.astype(np.uint16)
    cm = mahotas.center_of_mass(img, labels)
    assert cm.shape == (3,2)
github luispedro / mahotas / tests / test_center_of_mass.py View on Github external
def test_labels_not_intc():
    img = np.arange(256).reshape((16,16))
    labels = img.copy()
    labels %= 3
    cm = mahotas.center_of_mass(img, labels)
    assert cm.shape == (3,2)

    labels = labels.T.copy()
    cm = mahotas.center_of_mass(img, labels.T)
    assert cm.shape == (3,2)

    labels = labels.T.copy()
    labels = labels.astype(np.uint16)
    cm = mahotas.center_of_mass(img, labels)
    assert cm.shape == (3,2)