示例#1
0
def ImageAnnotationToMask(iterable, imagecol, annocol):
    """
    samples >> ImageAnnotationToMask(imagecol, annocol)

    Create mask for image annotation. Annotation are of the following formats.
    See imageutil.annotation2coords for details.
    ('point', ((x, y), ... ))
    ('circle', ((x, y, r), ...))
    ('rect', ((x, y, w, h), ...))
    ('polyline', (((x, y), (x, y), ...), ...))

    >>> import numpy as np
    >>> from nutsflow import Collect

    >>> img = np.zeros((3, 3), dtype='uint8')
    >>> anno = ('point', ((0, 1), (2, 0)))
    >>> samples = [(img, anno)]
    >>> masks = samples >> ImageAnnotationToMask(0, 1) >> Collect()
    >>> print(masks[0][1])
    [[  0   0 255]
     [255   0   0]
     [  0   0   0]]

    :param iterable iterable: Samples with images and annotations
    :param int imagecol: Index of sample column that contain image
    :param int annocol: Index of sample column that contain annotation
    :return: Iterator over samples where annotations are replaced by masks
    :rtype: generator
    """
    for sample in iterable:
        sample = list(sample)[:]  # going to mutate sample
        mask = ni.annotation2mask(sample[imagecol], sample[annocol])
        sample[annocol] = mask
        yield tuple(sample)
示例#2
0
def ImagePatchesByAnnotation(iterable,
                             imagecol,
                             annocol,
                             pshape,
                             npos,
                             nneg=lambda npos: npos,
                             pos=255,
                             neg=0,
                             retlabel=True):
    """
    samples >> ImagePatchesByAnnotation(imagecol, annocol, pshape, npos,
                                 nneg=lambda npos: npos,
                                 pos=255, neg=0, retlabel=True)

    Randomly sample positive/negative patches from image based on annotation.
    See imageutil.annotation2coords for annotation format.
    A patch is positive if its center point is within the annotated region
    and is negative otherwise.

    >>> import numpy as np
    >>> np.random.seed(0)    # just to ensure stable doctest
    >>> img = np.reshape(np.arange(25), (5, 5))
    >>> anno = ('point', ((3, 2), (2, 3),))
    >>> samples = [(img, anno)]

    >>> getpatches = ImagePatchesByAnnotation(0, 1, (3, 3), 1, 1)
    >>> for (p, l) in samples >> getpatches:
    ...     print(p.tolist(), l)
    [[12, 13, 14], [17, 18, 19], [22, 23, 24]] 0
    [[11, 12, 13], [16, 17, 18], [21, 22, 23]] 1
    [[7, 8, 9], [12, 13, 14], [17, 18, 19]] 1

    :param iterable iterable: Samples with images
    :param int imagecol: Index of sample column that contain image
    :param int annocol: Index of sample column that contain annotation
    :param tuple pshape: Shape of patch
    :param int npos: Number of positive patches to sample
    :param int|function nneg: Number of negative patches to sample or
        a function hat returns the number of negatives
        based on number of positives.
    :param int pos: Mask value indicating positives
    :param int neg: Mask value indicating negatives
    :param bool retlabel: True return label, False return mask patch
    :return: Iterator over samples where images are replaced by image patches
        and masks are replaced by labels [0,1] or mask patches
    :rtype: generator
    """
    for sample in iterable:
        image, annotations = sample[imagecol], sample[annocol]
        mask = ni.annotation2mask(image, annotations)
        n = len(annotations[1]) if annotations else 0
        it = ni.sample_pn_patches(image, mask, pshape, npos * n, nneg, pos,
                                  neg)
        for img_patch, mask_patch, label in it:
            outsample = list(sample)[:]
            outsample[imagecol] = img_patch
            outsample[annocol] = label if retlabel else mask_patch
            yield tuple(outsample)
def test_annotation2mask():
    img = np.zeros((3, 3), dtype='uint8')
    anno = ('point', ((0, 1), (2, 0)))
    mask = ni.annotation2mask(img, anno)
    expected = np.array([[0, 0, 255], [255, 0, 0], [0, 0, 0]], dtype='uint8')
    nt.assert_allclose(mask, expected)