def largest_connected_component(img, structure = None):
    r"""
    Select the largest connected binary component in an image.
    
    Treats all zero values in the input image as background and all others as foreground.
    The return value is an binary array of equal dimensions as the input array with TRUE
    values where the largest connected component is situated.
    
    Parameters
    ----------
    img : array_like
        An array containing connected objects. Will be cast to type numpy.bool.
    structure : array_like
        A structuring element that defines the connectivity. Structure must be symmetric.
        If no structuring element is provided, one is automatically generated with a
        squared connectivity equal to one.
    
    Returns
    -------
    binary_image : ndarray
        The supplied binary image with only the largest connected component remaining.
    """   
    labeled_array, num_features = label(img, structure)
    component_sizes = [numpy.count_nonzero(labeled_array == label_idx) for label_idx in range(1, num_features + 1)]
    largest_component_idx = numpy.argmax(component_sizes) + 1

    out = numpy.zeros(img.shape, numpy.bool)  
    out[labeled_array == largest_component_idx] = True
    return out
r"""
    Select the largest connected binary component in an image.
    
    Treats all zero values in the input image as background and all others as foreground.
    The return value is an binary array of equal dimensions as the input array with TRUE
    values where the largest connected component is situated.
    
    Parameters
    ----------
    img : array_like
        An array containing connected objects. Will be cast to type numpy.bool.
    structure : array_like
        A structuring element that defines the connectivity. Structure must be symmetric.
        If no structuring element is provided, one is automatically generated with a
        squared connectivity equal to one.
    
    Returns
    -------
    binary_image : ndarray
        The supplied binary image with only the largest connected component remaining.