% SLIC Simple Linear Iterative Clustering SuperPixels
%
% Implementation of Achanta, Shaji, Smith, Lucchi, Fua and Susstrunk's
% SLIC Superpixels
%
% Usage: [l, Am, Sp, d] = slic(im, k, m, seRadius, colopt, mw)
%
% Arguments: im - Image to be segmented.
% k - Number of desired superpixels. Note that this is nominal
% the actual number of superpixels generated will generally
% be a bit larger, espiecially if parameter m is small.
% 所需 superpixels 的数量。请注意, 这是名义上生成的数量,实际数量一般会更大一点, 尤其是当参数 m 小的时候。
% m - Weighting factor between colour and spatial
% differences. Values from about 5 to 40 are useful. Use a
% large value to enforce superpixels with more regular and
% smoother shapes. Try a value of 10 to start with.
% 颜色和空间差异之间的加权因子。在CIELAB空间,范围在[1,40].从大约5到40是有用的。 使用大值来强制 superpixels 的形状更加整齐和平滑。请尝试使用值10开始。
% seRadius - Regions morphologically smaller than this are merged with
% adjacent regions. Try a value of 1 or 1.5. Use 0 to
% disable.在形态上小于此的区域与相邻区域合并。尝试值1或1.5。使用0禁用。
% colopt - String 'mean' or 'median' indicating how the cluster
% colour centre should be computed. Defaults to 'mean'。字符串 "平均值" 或 "中间值", 指示如何计算簇颜色中心。默认为 "平均值"
% mw - Optional median filtering window size. Image compression可选中值过滤窗口大小。 图像压缩会导致图像中的 a*b* 组件中出现明显的工件。
% 中值滤波可以减少这种情况。mw可以是一个单一的值, 在这种情况下, 同样的中值滤波适用于每一个 ** 和 b * 组件。
% 另外, 它可以是一个2向量, 其中兆瓦 (1) 指定要应用于 L * 和兆瓦 (2) 的中值过滤窗口是要应用于 * 和 b * 的中值过滤窗口。
% can result in noticeable artifacts in the a*b* components
% of the image. Median filtering can reduce this. mw can be
% a single value in which case the same median filtering is
% applied to each L* a* and b* components. Alternatively it
% can be a 2-vector where mw(1) specifies the median
% filtering window to be applied to L* and mw(2) is the
% median filtering window to be applied to a* and b*.
%
% Returns: l - Labeled image of superpixels. Labels range from 1 to k.
% Am - Adjacency matrix of segments. Am(i, j) indicates whether
% segments labeled i and j are connected/adjacent
% Sp - Superpixel attribute structure array with fields:
% L - Mean L* value
% a - Mean a* value
% b - Mean b* value
% r - Mean row value
% c - Mean column value
% stdL - Standard deviation of L* 标准偏差 L *
% stda - Standard deviation of a* 标准偏差 a *
% stdb - Standard deviation of b* 标准偏差 b *
% N - Number of pixels
% edges - List of edge numbers that bound each
% superpixel. This field is allocated分配, but not set,
% by SLIC. Use SPEDGES(spedges) for this.
% d - Distance image giving the distance each pixel is from its
% associated superpixel centre.
%
% It is suggested that use of this function is followed by SPDBSCAN to perform a
% DBSCAN clustering of superpixels. This results in a simple and fast
% segmentation of an image.
%
% Minor variations from the original algorithm as defined in Achanta et al's
% paper:
%
% - SuperPixel centres are initialised on a hexagonal grid rather than a square
% one. This results in a segmentation that will be nominally 6-connected
% which hopefully facilitates any subsequent post-processing that seeks to
% merge superpixels.
% - Initial cluster positions are not shifted to point of lowest gradient
% within a 3x3 neighbourhood because this will be rendered irrelevant the
% first time cluster centres are updated.
%
% Reference: R. Achanta, A. Shaji, K. Smith, A. Lucchi, P. Fua and
% S. Susstrunk. "SLIC Superpixels Compared to State-of-the-Art Superpixel
% Methods" PAMI. Vol 34 No 11. November 2012. pp 2274-2281.
%
% See also: SPDBSCAN, MCLEANUPREGIONS, REGIONADJACENCY, DRAWREGIONBOUNDARIES, RGB2LAB
% Copyright (c) 2013 Peter Kovesi
% Centre for Exploration Targeting
% School of Earth and Environment
% The University of Western Australia
% peter.kovesi at uwa edu au
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.
% Feb 2013
% July 2013 Super pixel attributes returned as a structure array
% Note that most of the computation time is not in the clustering, but rather
% in the region cleanup process.
function [l, Am, Sp, d] = slic(im, k, m, seRadius, colopt, mw, nItr, eim, We)
%%
if ~exist('colopt','var') || isempty(colopt), colopt = 'mean'; end %设置默认值,~exist('colopt','var')是检测colopt变量是否存在
if ~exist('mw','var') || isempty(mw), mw = 0; end
if ~exist('nItr','var') || isempty(nItr), nItr = 10; end
if exist('eim', 'var'), USEDIST = 0; else, USEDIST = 1; end
MEANCENTRE = 1;
MEDIANCENTRE = 2;
if strcmp(colopt, 'mean')%比较函数
centre = MEANCENTRE;
elseif strcmp(colopt, 'median')
centre = MEDIANCENTRE;
else
error('Invalid colour centre computation option');
end
[rows, cols, chan] = size(im);
if chan ~= 3
error('Image must be colour');
end
%%
% Convert image to L*a*b* colourspace. This gives us a colourspace that is
% nominally perceptually uniform. This allows us to use the euclidean
% distance between colour coordinates to measure differences between
% colours. Note the image becomes double after conversion. We may want to
% go to signed shorts to save memory.
im = rgb2lab(im); %执行完这句,图像变成双精度的了
% Apply median filtering to colour components if mw has been supplied
% and/or non-zero
if mw
if length(mw) == 1
mw(2) = mw(1); % Use same filtering for L and chrominance 对L和色度使用相同的滤波
end
for n = 1:3
im(:,:,n) = medfilt2(im(:,:,n), [mw(1) mw(1)]);%medfilt2消除噪声, 中值滤波器
end
end
% Nominal spacing between grid elements assuming hexagonal grid
% 六角网格间的标称间距
S = sqrt(rows*cols / (k * sqrt(3)/2));%???
% Get nodes per row allowing a half column margin at one end that alternates
% from row to row 获取每列的节点, 允许在一端从行到行交替的半列边距
nodeCols = round(cols/S - 0.5);
% Given an integer number of nodes per row recompute S 给定整数的每列节点数重新计算 S
S = cols/(nodeCols + 0.5);
% Get number of rows of nodes allowing 0.5 row margin top and bottom 获取允许0.5 行边距顶部和底端的节点行数
nodeRows = round(rows/(sqrt(3)/2*S));%行节点数
vSpacing = rows/nodeRows;
% Recompute k 重新计算k
k = nodeRows * nodeCols;
% Allocate memory and initialise clusters, labels and distances.分配内存和初始化中心、标签和距离。
C = zeros(6,k); % Cluster centre data 1:3 is mean Lab value,初始化聚类中心
SLIC超像素 MATLAB经典程序 直接可用
5星 · 超过95%的资源 需积分: 34 34 浏览量
2018-03-30
08:06:30
上传
评论 21
收藏 822KB RAR 举报
西兰小花花❀
- 粉丝: 55
- 资源: 6
最新资源
- HCIP-Datacom笔记 (1).pdf
- yolov5,SSD 可能使用到的一些代码
- bbbbbbbbbbbbbbbbbb
- 安卓逆向学习笔记之Frida Stalker 还原OLLVM AES.docx
- 安卓逆向学习笔记之unicorn来trace还原OLLVM Base64.docx
- 最新版本私钥助记词碰撞器大富豪使用python进行制作通过接口的方式进行验证支持多币种多链多网络一分钟万次验证高出货率
- 介绍离散性制造行业的MES系统流程
- Arduino IDE压缩包版本,2024年4月26日,最新版本
- 基于IDEA-CCNL/Randeng-Pegasus-238M-Summary-Chines微调的中文文本摘要任务源码+数据集
- 自动驾驶-状态估计和定位之直方图滤波(Histogram+Filter)定位应用和源码.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈