www.gusucode.com > images 案例代码 matlab源码程序 > images/ComputeSuperpixelsOfInputRGBImageExample.m

    %% Compute Superpixels of Input RGB Image
% 
%%
% Read image into the workspace.
A = imread('kobi.png');
%%
% Calculate superpixels of the image.
[L,N] = superpixels(A,500);
%%
% Display the superpixel boundaries overlaid on the original image.
figure
BW = boundarymask(L);
imshow(imoverlay(A,BW,'cyan'),'InitialMagnification',67)
%%
% Set the color of each pixel in the output image to the mean RGB color of
% the superpixel region.
outputImage = zeros(size(A),'like',A);
idx = label2idx(L);
numRows = size(A,1);
numCols = size(A,2);
for labelVal = 1:N
    redIdx = idx{labelVal};
    greenIdx = idx{labelVal}+numRows*numCols;
    blueIdx = idx{labelVal}+2*numRows*numCols;
    outputImage(redIdx) = mean(A(redIdx));
    outputImage(greenIdx) = mean(A(greenIdx));
    outputImage(blueIdx) = mean(A(blueIdx));
end    

figure
imshow(outputImage,'InitialMagnification',67)