To calculate SLIC superpixels in python, we have two options:
skimage.segmentation.slic
http://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slicI found that these provided completely different segmentation unless enforce_connectivity=True
in skimage.segmentation.slic
. Here I show a brief example describing how different they are, which we could not see at http://scikit-image.org/docs/dev/auto_examples/plot_segmentations.html
example.py
from skimage.segmentation import slic, mark_boundaries
from skimage.data import lena
import slic as slic_wrap # code available at https://github.com/amueller/slic-python
import matplotlib.pyplot as plt
img = lena()
plt.subplot(1,3,1)
plt.imshow(mark_boundaries(img, slic_wrap.slic_n(img, 40, 10)))
title('Original version')
plt.subplot(1,3,2)
plt.imshow(mark_boundaries(img, slic(img, 40, 10, sigma=1, enforce_connectivity=False)))
title('skimage version\n enforce_connectivity=False', size=9)
plt.subplot(1,3,3)
plt.imshow(mark_boundaries(img, slic(img, 40, 10, sigma=1, enforce_connectivity=True)))
title('skimage version\n enforce_connectivity=True', size=9)
Here I tried to tune the parameter sigma
in skimage version to obtain a similar output to the original version. Obviously, enforce_connectivity
option is essential to avoid color-sensitive segments.
Recommended Posts