Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skimage slic: getting neighbouring segments

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?

like image 962
Luca Avatar asked Dec 01 '25 21:12

Luca


1 Answers

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.

You can find some related functions here, thought they are not very well documented (yet).

like image 60
Andreas Mueller Avatar answered Dec 04 '25 12:12

Andreas Mueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!