Hello everyone, can anyone provide me the matlab code of FD SLIC algorithm or FM SLIC algorithm

1 view (last 30 days)

I am working on a project which requires the matlab code of FD SLIC algorithm ...

  1 Comment
国楠
国楠 on 28 Jan 2025
function [labels, num_labels] = fd_slic(img, k, m, max_iter)
% FD SLIC (Fast and Simple Linear Iterative Clustering) Algorithm
% Input:
% img: Input image (RGB or grayscale)
% k: Number of desired superpixels
% m: Compactness factor (higher values give more weight to spatial proximity)
% max_iter: Maximum number of iterations
% Output:
% labels: Label matrix (each pixel is assigned a superpixel label)
% num_labels: Number of superpixels generated
% Convert image to LAB color space (better for segmentation)
if size(img, 3) == 3
img_lab = rgb2lab(img);
else
img_lab = img; % If grayscale, use as is
end
[rows, cols, ~] = size(img_lab);
N = rows * cols; % Total number of pixels
S = floor(sqrt(N / k)); % Grid interval for initial cluster centers
% Initialize cluster centers
[x, y] = meshgrid(S/2:S:cols, S/2:S:rows);
centers = [x(:), y(:)];
num_centers = size(centers, 1);
% Initialize cluster centers' color and position
centers_lab = zeros(num_centers, 3);
for i = 1:num_centers
centers_lab(i, :) = img_lab(centers(i, 2), centers(i, 1), :);
end
% Initialize labels and distances
labels = -ones(rows, cols);
distances = inf(rows, cols);
% Iterate
for iter = 1:max_iter
% Assign pixels to the nearest cluster center
for i = 1:num_centers
% Define a 2S x 2S region around the cluster center
x_min = max(1, centers(i, 1) - S);
x_max = min(cols, centers(i, 1) + S);
y_min = max(1, centers(i, 2) - S);
y_max = min(rows, centers(i, 2) + S);
% Extract the region
region = img_lab(y_min:y_max, x_min:x_max, :);
[region_rows, region_cols, ~] = size(region);
% Compute distances
d_color = sum((region - centers_lab(i, :)).^2, 3);
d_space = (meshgrid(1:region_cols, 1:region_rows) - S).^2 + ...
(meshgrid(1:region_rows, 1:region_cols)' - S).^2;
d = d_color + (m / S)^2 * d_space;
% Update labels and distances
region_labels = labels(y_min:y_max, x_min:x_max);
region_distances = distances(y_min:y_max, x_min:x_max);
update_mask = d < region_distances;
region_labels(update_mask) = i;
region_distances(update_mask) = d(update_mask);
labels(y_min:y_max, x_min:x_max) = region_labels;
distances(y_min:y_max, x_min:x_max) = region_distances;
end
% Update cluster centers
for i = 1:num_centers
mask = (labels == i);
if any(mask(:))
centers(i, 1) = mean(find(sum(mask, 1)));
centers(i, 2) = mean(find(sum(mask, 2)));
centers_lab(i, :) = mean(reshape(img_lab(repmat(mask, [1, 1, 3])), [], 3));
end
end
end
% Post-processing to enforce connectivity
labels = enforce_connectivity(labels, k);
% Number of labels
num_labels = num_centers;
end
function labels = enforce_connectivity(labels, k)
% Enforce connectivity of superpixels
[rows, cols] = size(labels);
new_labels = zeros(rows, cols);
next_label = 1;
for i = 1:rows
for j = 1:cols
if new_labels(i, j) == 0
% Start a new segment
stack = [i, j];
new_labels(i, j) = next_label;
while ~isempty(stack)
% Pop from stack
current = stack(1, :);
stack(1, :) = [];
% Check neighbors
for x = max(1, current(1)-1):min(rows, current(1)+1)
for y = max(1, current(2)-1):min(cols, current(2)+1)
if labels(x, y) == labels(current(1), current(2)) && new_labels(x, y) == 0
new_labels(x, y) = next_label;
stack = [stack; x, y];
end
end
end
end
next_label = next_label + 1;
end
end
end
labels = new_labels;
end
Explanation:
  1. Input Parameters:
  • img: Input image (RGB or grayscale).
  • k: Desired number of superpixels.
  • m: Compactness factor (controls the balance between color similarity and spatial proximity).
  • max_iter: Maximum number of iterations.
  1. Steps:
  • Convert the image to LAB color space for better segmentation.
  • Initialize cluster centers on a grid.
  • Iteratively assign pixels to the nearest cluster center based on color and spatial distance.
  • Update cluster centers after each iteration.
  • Enforce connectivity of superpixels in the post-processing step.
  1. Output:
  • labels: A matrix where each pixel is assigned a superpixel label.
  • num_labels: The number of superpixels generated.

Sign in to comment.

Answers (1)

Torsten
Torsten on 28 Jan 2025
Edited: Torsten on 28 Jan 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!