Exemple #1
0
def test_grain():
    L = np.array([
        [0,0,1,1,1,0],
        [0,1,0,0,0,0],
        [0,0,2,2,2,0],
        [0,2,2,2,2,2],
        ])
    D = np.array([
        [0, 0,12,12,12,0],
        [0,12, 0, 0, 0,0],
        [0,0,21,22,23,0],
        [0,24,25,26,27,28],
        ])

    assert np.all( pymorph.grain(D, L, 'max', 'data') == (12, 28))
    assert np.all( pymorph.grain(D, L, 'min', 'data') == (12, 21))
    assert np.all( pymorph.grain(D, L, 'mean', 'data') == (12, 24.5))
Exemple #2
0
def test_image():
    f = np.array([
        [0,1,2,3,0],
        [0,1,3,4,1],
        ])
    labels = np.array([
        [1,1,2,2,0],
        [1,1,2,2,0],
        ])

    for measure in ('max', 'mean', 'min', 'std'):
        image = pymorph.grain(f, labels, measure, 'image')
        data = pymorph.grain(f, labels, measure, 'data')
        assert len(data) == labels.max()

        for v,lab in zip(image.ravel(), labels.ravel()):
            if lab > 0:
                assert v == data[lab-1]
Exemple #3
0
def regionProps(mask, image=None):
    """Calculates some basic properties of ROIs in a mask.

    Properties calculated:  centroids, boxes, areas.  If the original image is passed in,
    this also calcluate the mean value in each region.

    Keys of returned dictionary:  'centroid', 'boundingBox', 'area', and optionally, 'meanIntensity'

    :param mask: a 2d labeled image
    :param image: an optional 2p numpy array, original image, for calculation of mean intensity values
    :returns: a dictionary of lists, each containing property values
    """

    if image is not None:
        if len(image.shape) >= 3:
            meanImage = np.mean(image, axis=2)
        else:
            meanImage = image

        min = meanImage.min()
        if min < 0:
            meanImage -= meanImage.min()

        means = pymorph.grain(meanImage,
                              labels=mask,
                              measurement='mean',
                              option='data')
    else:
        means = None

    numLabels = mask.max()
    centroids = pymorph.blob(mask, measurement='centroid', output='data')
    boxes = pymorph.blob(mask, measurement='boundingbox', output='data')
    area = pymorph.blob(mask, measurement='area', output='data')

    coms = []
    for i in range(1, numLabels + 1):
        coms.append(nd.measurements.center_of_mass(meanImage,
                                                   labels=mask == i))

    if means is not None:
        props = [{
            'meanIntensity': means[i],
            'centroid': centroids[i],
            'com': coms[i],
            'boundingBox': boxes[i],
            'area': area[i]
        } for i in range(numLabels)]
    else:
        props = [{
            'centroid': centroids[i],
            'com': coms[i],
            'boundingBox': boxes[i],
            'area': area[i]
        } for i in range(numLabels)]
    return props
Exemple #4
0
def test_off_by_one():
    # Contributed by Timothy Hirzel
    L = np.array([
            [0,0,1,1,1,0],
            [0,1,0,0,0,0],
            [0,0,2,2,2,0],
            [0,2,2,2,2,2],
            ])
    D = np.array([
            [0, 0,12,12,12, 0],
            [0,12, 0, 0, 0, 0],
            [0, 0,21,22,23, 0],
            [0,24,25,26,27,28],
            ])
    maxdata = np.array([12,28])
    maximage = np.array([
            [0, 0,12,12,12, 0],
            [0,12, 0, 0, 0, 0],
            [0, 0,28,28,28, 0],
            [0,28,28,28,28,28],
            ])
    assert((maxdata == pymorph.grain(D,L,'max', 'data')).all())
    assert((maximage == pymorph.grain(D,L,'max', 'image')).all())
def regionProps(mask, image=None):
    """Calculates some basic properties of ROIs in a mask.

    Properties calculated:  centroids, boxes, areas.  If the original image is passed in,
    this also calcluate the mean value in each region.

    Keys of returned dictionary:  'centroid', 'boundingBox', 'area', and optionally, 'meanIntensity'

    :param mask: a 2d labeled image
    :param image: an optional 2p numpy array, original image, for calculation of mean intensity values
    :returns: a dictionary of lists, each containing property values
    """

    if image is not None:
        if len(image.shape) >= 3:
            meanImage = np.mean(image, axis=2)
        else:
            meanImage = image

        min = meanImage.min()
        if min < 0:
            meanImage -= meanImage.min()

        means = pymorph.grain(meanImage, labels=mask, measurement='mean', option='data')
    else:
        means = None
        
    numLabels = mask.max()
    centroids = pymorph.blob(mask, measurement='centroid', output='data')
    boxes = pymorph.blob(mask, measurement='boundingbox', output='data')
    area = pymorph.blob(mask, measurement='area', output='data')

    coms = []
    for i in range(1, numLabels+1):
        coms.append(nd.measurements.center_of_mass(meanImage, labels=mask==i))

    if means is not None:
        props = [{'meanIntensity':means[i], 'centroid':centroids[i], 'com':coms[i], 'boundingBox':boxes[i], 'area':area[i]} for i in range(numLabels)]
    else:
        props = [{'centroid':centroids[i], 'com':coms[i], 'boundingBox':boxes[i], 'area':area[i]} for i in range(numLabels)]
    return props