How to use the mahotas.center_of_mass 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 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_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)
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_cmp_ndimage3():
    R = (255*np.random.rand(32,128,8,16)).astype(np.uint16)
    R += np.arange(16)
    m = mahotas.center_of_mass(R)
    n = ndimage.center_of_mass(R)
    p = slow_center_of_mass(R)
    assert np.abs(n - m).max() < 1.
    assert np.abs(p - m).max() < 1.
github luispedro / mahotas / tests / test_center_of_mass.py View on Github external
def test_labels():
    R = (255*np.random.rand(128,256)).astype(np.uint16)
    labels = np.zeros(R.shape, np.intc)
    labels[100:,:] += 1
    labels[100:,100:] += 1
    centres =  mahotas.center_of_mass(R, labels)
    for label,cm in enumerate(centres):
        assert np.all(cm == mahotas.center_of_mass(R * (labels == label)))
github luispedro / mahotas / mahotas / features / shape.py View on Github external
semimajor : float
    semiminor : float

    References
    ----------
    Prokop, RJ, and Reeves, AP.  1992. CVGIP: Graphical Models and Image
    Processing 54(5):438-460

    '''
    from .moments import moments
    bwimage = _make_binary(bwimage)

    if not np.any(bwimage):
        return 0.,0.

    cof = mh.center_of_mass(bwimage)
    hull_mu00 = moments(bwimage, 0, 0, cof)
    hull_mu11 = moments(bwimage, 1, 1, cof)
    hull_mu02 = moments(bwimage, 0, 2, cof)
    hull_mu20 = moments(bwimage, 2, 0, cof)

    semimajor = np.sqrt((2 * (hull_mu20 + hull_mu02 + \
                    np.sqrt((hull_mu20 - hull_mu02)**2 + \
                    4 * hull_mu11**2)))/hull_mu00)

    semiminor = np.sqrt((2 * (hull_mu20 + hull_mu02 - \
                    np.sqrt((hull_mu20 - hull_mu02)**2 + \
                    4 * hull_mu11**2)))/hull_mu00)
    return semimajor, semiminor