我目前无法访问 MatLab - 所以没有机会实际尝试它 - 但请查看以下功能。根据 VLFeat 的源代码,该函数vl_slic
将标签作为矩阵返回:
% VL_SLIC SLIC superpixels
% SEGMENTS = VL_SLIC(IM, REGIONSIZE, REGULARIZER) extracts the SLIC
% superpixes [1] from image IM. REGIONSIZE is the starting size of
% the superpixels and REGULARIZER is the trades-off appearance for
% spatial regularity when clustering (a larger value results in more
% spatial regularization). SEGMENTS is a UINT32 array containing the
% superpixel identifier for each image pixel.
函数draw_contours
定义如下:
function [contourImg] = draw_contours(labels, img)
% function [contourImg] = draw_contours(labels, img)
%
% David Stutz <david.stutz@rwth-aachen.de>
rows = size(img, 1);
cols = size(img, 2);
contourImg = img;
for i = 1: rows
for j = 1: cols
label = labels(i, j);
labelTop = 0;
labelBottom = 0;
labelLeft = 0;
labelRight = 0;
if i > 1
labelTop = labels(i - 1, j);
end;
if j > 1
labelLeft = labels(i, j - 1);
end;
if i < rows
labelBottom = labels(i + 1, j);
end;
if j < cols
labelRight = labels(i, j + 1);
end;
if labelTop ~= 0 && labelTop ~= label
contourImg(i, j, 1) = 0;
contourImg(i, j, 2) = 0;
contourImg(i, j, 3) = 0;
end;
if labelLeft ~= 0 && labelLeft ~= label
contourImg(i, j, 1) = 0;
contourImg(i, j, 2) = 0;
contourImg(i, j, 3) = 0;
end;
if labelBottom ~= 0 && labelBottom ~= label
contourImg(i, j, 1) = 0;
contourImg(i, j, 2) = 0;
contourImg(i, j, 3) = 0;
end;
if labelRight ~= 0 && labelRight ~= label
contourImg(i, j, 1) = 0;
contourImg(i, j, 2) = 0;
contourImg(i, j, 3) = 0;
end;
end;
end;
end
给定图像img = imread('path/to/image');
,只需提取超像素,然后绘制轮廓图像:
img = imread('path/to/image');
labels = vl_slic(img, regionSize, regularizer);
contourImg = draw_contours(labels, img);
imshow(contourImg);
该功能draw_contours
已使用 Turbopixels 超像素 [2] 进行了测试。下图显示了 VLFeat 的 SLIC [3] 实现的结果,它使用 C++ 实现的用于绘制轮廓的类似函数,以及使用 VLFeat 的 C 接口提取的超像素。C++ 源代码可以在 GitHub 上找到:https ://github.com/davidstutz/vlfeat-slic-example

- [2] A. Levinshtein、A. Stere、KN Kutulakos、DJ Fleet、SJ Dickinson、K. Siddiqi。Turbopixels:使用几何流的快速超像素。IEEE Transactions on Pattern Analysis and Machine Intelligence,第 2290 – 2297 页,2009。
- [3] R. Achanta、A. Shaji、K. Smith、A. Lucchi、P. Fua、S. Susstrunk。SLIC 超像素。技术报告,EPFL,洛桑,2010 年。