使用深度学习或机器学习算法对图像进行分割后,得到的图像矩阵往往是多类别的整数值,在一般情况下,我们希望将每个整数(即类别)对应一个RGB颜色输出,我们可以参考下述代码,使用skimage库中的label2rgb方法来实现。
效果如下:
代码如下:
from skimage import io
from skimage import color
from skimage import segmentation
import matplotlib.pyplot as plt
# URL for tiger image from Berkeley Segmentation Data Set BSDS
url=('http://www.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/BSDS300/html/images/plain/normal/color/108073.jpg')
# Load tiger image from URL
tiger = io.imread(url)
# Segment image with SLIC - Simple Linear Iterative Clustering
seg = segmentation.slic(tiger, n_segments=30, compactness=40.0, enforce_connectivity=True, sigma=3)
# Generate automatic colouring from classification labels
io.imshow(color.label2rgb(seg,tiger))
plt.show()
参考链接: