skimage slic: getting neighbouring segments
Question: There is a nice implementation of super resolution segment generation (SLIC)
in skimage.segmentation package in the python sklearn package.

The slic() method returns the integer sets of labels. My question is how can I
get the segments that are spatial neighbors of each other? What I would like
to do is build a graph using these segments and the edges would connect the
immediate neighbors. However, I cannot figure out how to get the immediate
neighbors of a segment.

The python code to perform the SLIC is as follows:

    
    
    from skimage import io
    from skimage.segmentation import slic
    from skimage.segmentation import find_boundaries
    # An image of dimensions 300, 300
    image = img_as_float(io.imread("image.png"))
    
    # call slic. This returns an numpy array which assigns to every 
    # pixel in the image an integer label
    # So segments is a numpy array of shape (300, 300)
    segments = slic(image, 100, sigma = 5)
    
    # Now I want to know the neighbourhood segment for each super-pixel
    # There is a method called find_boundaries which returns a boolean
    # for every pixel to show if it is a boundary pixel or not.
    
    b = find_boundaries(segments)
    

Here, I am stuck. I would like to know how to parse this boundary indices and
find out for a given label index (say 0), which label indexes share a boundary
with label of index 0. Is there a way to do this efficiently without looping
through the boundary array for every label index?


Answer: The way I do it is to build a graph containing an edge from each pixel to its
left and bottom pixel (so a 4 neighborhood), label them with their superpixel
number and remove duplicates. You can find code and details in [my blog
post](http://peekaboo-vision.blogspot.de/2011/08/region-connectivity-graphs-
in-python.html).

You can find some related functions
[here](https://github.com/amueller/segmentation/blob/master/utils.py#L140),
thought they are not very well documented (yet).

