Ejemplo n.º 1
0
def _define_kernel(shape, size, dtype):
    """Build a kernel to apply a filter on images.

    Parameters
    ----------
    shape : str
        Shape of the kernel used to compute the filter ('diamond', 'disk',
        'rectangle' or 'square').
    size : int, Tuple(int) or List(int)
        The size of the kernel:
            - For the rectangle we expect two values (width, height).
            - For the square one value (width).
            - For the disk and the diamond one value (radius).
    dtype : type
        Dtype used for the kernel (the same as the image).

    Returns
    -------
    kernel : skimage.morphology.selem object
        Kernel to use with a skimage filter.

    """
    # build the kernel
    if shape == "diamond":
        kernel = diamond(size, dtype=dtype)
    elif shape == "disk":
        kernel = disk(size, dtype=dtype)
    elif shape == "rectangle" and isinstance(size, tuple):
        kernel = rectangle(size[0], size[1], dtype=dtype)
    elif shape == "square":
        kernel = square(size, dtype=dtype)
    else:
        raise ValueError("Kernel definition is wrong.")

    return kernel
Ejemplo n.º 2
0
def segleaf(I):
    """
    Leaf segmentation using random walker
    :param I: Input image as a float

    :return: A binary image that shows the segmentation of the leaf from the background
    """
    #Separate the image into it's three colour components
    red = 255 * I[:, :, 0]
    green = 255 * I[:, :, 1]
    blue = 255 * I[:, :, 2]

    #Create a new image with the amount of greeness exaggerrated
    greenness = 1.97 * green - red - blue
    #Find a threshold value
    T = filt.threshold_otsu(greenness)
    #Create an array of identical size to the image, with seed pixels labelled as fg and bg
    seeds = np.zeros_like(I, dtype=np.uint8)
    seeds[greenness >= T] = 1  # fg label
    try:
        seeds[greenness < 5] = 2  # bg label
        labels = seg.random_walker(I, seeds, beta=10, multichannel=True)
    except:
        #For when a IndexError occurs
        try:
            seeds[greenness < 10] = 2  # bg label
            labels = seg.random_walker(I, seeds, beta=10, multichannel=True)
        except:
            seeds[greenness < 22] = 2  # bg label
            labels = seg.random_walker(I, seeds, beta=10, multichannel=True)

    #Label the bg labelled pixels as False
    labels[labels == 2] = 0
    labels = labels[:, :,
                    0]  #All three colour components share the same value now, so only one is needed.

    #Perform some region processing to improve the result
    labels = morph.opening(labels, selem.diamond(1))
    labels = morph.binary_closing(labels, selem.diamond(2.5))
    labels = morph.remove_small_objects(labels, 30)
    labels = morph.binary_closing(labels, selem.diamond(3))
    labels = morph.remove_small_objects(labels, 340)
    labels = morph.binary_closing(labels, selem.diamond(6))
    return labels
Ejemplo n.º 3
0
def test_default_selem(function):
    strel = selem.diamond(radius=1)
    image = np.array(
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
         [0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
    im_expected = function(image, strel)
    im_test = function(image)
    testing.assert_array_equal(im_expected, im_test)
Ejemplo n.º 4
0
def test_default_selem():
    functions = [
        binary.binary_erosion, binary.binary_dilation, binary.binary_opening,
        binary.binary_closing
    ]
    strel = selem.diamond(radius=1)
    image = np.array(
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
         [0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
    for function in functions:
        im_expected = function(image, strel)
        im_test = function(image)
        yield testing.assert_array_equal, im_expected, im_test
Ejemplo n.º 5
0
def test_default_selem(function):
    strel = selem.diamond(radius=1)
    image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
    im_expected = function(image, strel)
    im_test = function(image)
    testing.assert_array_equal(im_expected, im_test)
Ejemplo n.º 6
0
def test_default_selem():
    functions = [
        grey.erosion, grey.dilation, grey.opening, grey.closing,
        grey.white_tophat, grey.black_tophat
    ]
    strel = selem.diamond(radius=1)
    image = np.array(
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
         [0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
         [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
    for function in functions:
        im_expected = function(image, strel)
        im_test = function(image)
        yield testing.assert_array_equal, im_expected, im_test
Ejemplo n.º 7
0
def test_default_selem():
    functions = [binary.binary_erosion, binary.binary_dilation,
                 binary.binary_opening, binary.binary_closing]
    strel = selem.diamond(radius=1)
    image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
    for function in functions:
        im_expected = function(image, strel)
        im_test = function(image)
        yield testing.assert_array_equal, im_expected, im_test
Ejemplo n.º 8
0
def test_default_selem():
    functions = [grey.erosion, grey.dilation,
                 grey.opening, grey.closing,
                 grey.white_tophat, grey.black_tophat]
    strel = selem.diamond(radius=1)
    image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
    for function in functions:
        im_expected = function(image, strel)
        im_test = function(image)
        yield testing.assert_array_equal, im_expected, im_test
Ejemplo n.º 9
0
def _define_kernel(shape, size, dtype):
    """Build a kernel to apply a filter on images.

    Parameters
    ----------
    shape : str
        Shape of the kernel used to compute the filter (`diamond`, `disk`,
        `rectangle` or `square`).
    size : int, Tuple(int) or List(int)
        The size of the kernel:
            - For the rectangle we expect two values (`height`, `width`).
            - For the square one value (`width`).
            - For the disk and the diamond one value (`radius`).
    dtype : type
        Dtype used for the kernel (the same as the image).

    Returns
    -------
    kernel : skimage.morphology.selem object
        Kernel to use with a skimage filter.

    """
    # build the kernel
    if shape == "diamond":
        kernel = diamond(size, dtype=dtype)
    elif shape == "disk":
        kernel = disk(size, dtype=dtype)
    elif shape == "rectangle" and isinstance(size, (tuple, list)):
        kernel = rectangle(size[0], size[1], dtype=dtype)
    elif shape == "square":
        kernel = square(size, dtype=dtype)
    else:
        raise ValueError("Kernel definition is wrong. Shape of the kernel "
                         "should be 'diamond', 'disk', 'rectangle' or "
                         "'square'. Not {0}.".format(shape))

    return kernel
Ejemplo n.º 10
0
def _segmentation(img_row, img_Red_row, result_denoise, tp=5):

    # segmentation of img_row:
    #----------------------------------------------------------------------

    img_denoise = result_denoise[tp]
    #img_denoise = result_denoise
    #Random walker
    segmentation_rw = _rand_walk(img_denoise)

    # Watershed
    segmentation_ws = _water(img_denoise)

    labeled_image = _join_seg(segmentation_rw, segmentation_ws)

    # Constructing the h-dome for analysis under the pic:
    # calculate hdome to have stronger difference in S/N
    #----------------------------------------------------------------------

    #seed = np.copy(img_denoise)
    #seed[1:-1, 1:-1] = img_denoise.min()
    #mask = img_denoise
    #dilated = reconstruction(seed, mask, method='dilation')
    #hdome = img_denoise - dilated

    props = regionprops(labeled_image, intensity_image=img_denoise)
    #props_hdome = regionprops(labeled_image, intensity_image = hdome)

    # Denoising / processing / segmentation of img_row red:
    #----------------------------------------------------------------------

    Red_binary = threshold_adaptive(img_Red_row[tp, :, :], block_size=37)
    d = selem.diamond(radius=4)
    Red_binary_open = opening(Red_binary, d)
    Red_binary_open = binary_erosion(Red_binary_open, selem=disk(2))
    Red_binary_open = binary_dilation(Red_binary_open, selem=disk(2))

    Red_binary_open_labeled = label(Red_binary_open)
    Red_binary_open_labeled_overlay = label2rgb(Red_binary_open_labeled,
                                                image=img_Red_row[tp, :, :])

    props_Red = regionprops(Red_binary_open_labeled,
                            intensity_image=img_denoise)

    # Getting property out
    #----------------------------------------------------------------------

    #Cells property
    #------------------
    cell_coord = []
    mean_intensity = []
    numb_para = []
    prop_green = []

    #Red property
    #------------------
    prop_red = []

    for cell, prop in enumerate(props):
        prop_green.append(props[cell])

    for cell, prop in enumerate(props):

        # Cell part:
        #------------------

        ycent, xcent = props[cell]['centroid']
        mean_intensity.append(props[cell]['mean_intensity'])
        cell_coord.append((ycent, xcent))

        # Parasite part:
        #------------------
        Para_masked = np.copy(Red_binary_open_labeled)
        Mask = (labeled_image == cell + 1)
        Para_masked[~Mask] = 0
        para_ID = list(np.unique(Para_masked[Para_masked != 0]))
        numb_para.append(len(para_ID))

        ### Get the all parasite not just the piece that overlap i
        lst_prop_para_ID = []

        for para, prop in enumerate(props_Red):
            ID = props_Red[para]['label']
            if ID in para_ID:
                lst_prop_para_ID.append(prop)
        prop_red.append((lst_prop_para_ID))

    result1 = np.array(cell_coord)
    result2 = np.array(mean_intensity)
    result3 = np.array(prop_red)
    result4 = np.array(prop_green)

    result = np.concatenate((result1, result2[:, np.newaxis],
                             result3[:, np.newaxis], result4[:, np.newaxis]),
                            axis=1)

    return result, labeled_image, prop_red, Red_binary_open