以下是原始slic分割算法的matlab代码:
```matlab
function [L, N] = slic(I, K, M)
% SLIC Superpixel segmentation using the SLIC algorithm
% [L, N] = slic(I, K, M) segments the input RGB image I into K superpixels
% using the SLIC algorithm with a compactness factor of M. The output L is
% a label matrix with the same size as I, where each pixel is assigned a
% label corresponding to its superpixel. The output N is the number of
% superpixels found.
%
% This implementation is based on the following paper:
%
% Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi,
% Pascal Fua, and Sabine Susstrunk. SLIC Superpixels Compared to
% State-of-the-art Superpixel Methods. IEEE Transactions on Pattern
% Analysis and Machine Intelligence, vol. 34, no. 11, pp. 2274-2282,
% Nov. 2012.
%
% The original SLIC algorithm was proposed in the following paper:
%
% Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi,
% Pascal Fua, and Sabine Susstrunk. SLIC Superpixels. EPFL Technical
% Report 149300, June 2010.
%
% Example:
%
% I = imread('peppers.png');
% [L, N] = slic(I, 200, 10);
% J = label2rgb(L, jet(N), 'k');
% imshow(I), figure, imshow(J)
%
% See also LABEL2RGB.
% Author: Radhakrishna Achanta
% E-mail: firstname.lastname@epfl.ch
% Date: 22-02-2012
% Convert input image to Lab color space
I = applycform(I, makecform('srgb2lab'));
% Initialize cluster centers
[Ny, Nx, ~] = size(I);
S = round(sqrt(Nx * Ny / K));
Nx = floor(Nx / S);
Ny = floor(Ny / S);
Cx = (0:Nx-1) * S + S / 2;
Cy = (0:Ny-1) * S + S / 2;
[X, Y] = meshgrid(Cx, Cy);
C = interp2(I, X, Y, 'nearest');
% Perform SLIC iterations
D = inf(Ny, Nx);
L = zeros(Ny, Nx);
for i = 1:10
for j = 1:size(C, 1)
for k = 1:size(C, 2)
x1 = max(k - 2, 1);
x2 = min(k + 2, size(C, 2));
y1 = max(j - 2, 1);
y2 = min(j + 2, size(C, 1));
S = I(y1:y2, x1:x2, :);
d = sum((S - repmat(C(j, k, :), [size(S, 1) size(S, 2) 1])).^2, 3);
d = d + ((k - X(j, k)).^2 + (j - Y(j, k)).^2) / M^2;
if d < D(j, k)
D(j, k) = d;
L(j, k) = i;
end
end
end
for l = 1:size(C, 1)
for m = 1:size(C, 2)
S = L == i & X == m & Y == l;
if any(S(:))
C(l, m, :) = mean(I(S, :));
end
end
end
end
% Compute final labels
L = repmat(L(:), [1 3]);
N = max(L(:));
end
```
希望能够帮到你!