skimage作为图像处理库,包括多种图像分割算法。其中超像素slic目前表现较好,该部分代码如下。
from skimage.segmentation import slic,mark_boundaries
from skimage import io
import matplotlib.pyplot as plt
# import numpy as np
#
# np.set_printoptions(threshold=np.inf)
img = io.imread("Lenna.png")
segments = slic(img, n_segments=60, compactness=10)
out=mark_boundaries(img,segments)
# print(segments)
plt.subplot(121)
plt.title("n_segments=60")
plt.imshow(out)
segments2 = slic(img, n_segments=300, compactness=10)
out2=mark_boundaries(img,segments2)
plt.subplot(122)
plt.title("n_segments=300")
plt.imshow(out2)
plt.show()