In [4]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
from imutils.convenience import resize
In [6]:
# load the image and convert it to a floating point data type
image = img_as_float(io.imread("./data/IMG_3835_1024.jpg"))
image = resize(image, height = 500)
# loop over the number of segments
for numSegments in (100, 200, 300):
# apply SLIC and extract (approximately) the supplied number
# of segments
segments = slic(image, n_segments = numSegments, sigma = 5)
# show the output of SLIC
fig = plt.figure("Superpixels -- %d segments" % (numSegments))
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(image, segments))
plt.axis("off")
In [ ]:
# The first is the is the n_segments argument which defines how many superpixel
# segments we want to generate. This value defaults to 100 segments.
# We then supply sigma , which is the smoothing Gaussian kernel applied prior to segmentation.
# Other optional parameters can be utilized in the function, such as max_iter ,
# which the maximum number of iterations for k-means, compactness ,
# which balances the color-space proximity with image space-proximity, and
# convert2lab which determines whether the input image should be converted
# to the L*a*b* color space prior to forming superpixels (in nearly all cases,
# having convert2lab set to True is a good idea).