# import the necessary packages
import warnings
warnings.filterwarnings("ignore")
from sklearn.cluster import KMeans
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import numpy as np
import matplotlib.pyplot as plt
import cv2
# load the image and grab its width and height
image = cv2.imread("python_one.jpg")
(h, w) = image.shape[:2]
plt.imshow(image)
plt.show()
print((h, w))
(900, 1200)
# load the image and convert it to a floating point data type
image = img_as_float(image)
# loop over the number of segments
for numSegments in (200, 300, 500, 800):
# 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")
# show the plots
plt.show()
# load the image and grab its width and height
image = cv2.imread("python.jpg")
(h, w) = image.shape[:2]
plt.imshow(image)
plt.show()
print((h, w))
# load the image and convert it to a floating point data type
image = img_as_float(image)
# loop over the number of segments
for numSegments in (200, 300, 500, 800):
# 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")
# show the plots
plt.show()
(410, 612)
# load the image and grab its width and height
image = cv2.imread("datascience.png")
(h, w) = image.shape[:2]
plt.imshow(image)
plt.show()
print((h, w))
# load the image and convert it to a floating point data type
image = img_as_float(image)
# loop over the number of segments
for numSegments in (200, 300, 500, 800):
# 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")
# show the plots
plt.show()
(840, 1600)
In this coding recipe, we discussed how to do a simple image segmentation with superpixels using Python and OpenCV.
Specifically, we have learned the followings: