
how can i separate each color into a single image?
    4 views (last 30 days)
  
       Show older comments
    
 I used imsegK-means and superpixel for color classification . How can I separate each cluster into a single image ?
%superpixels and imsegk-means function for color classificatsion
%show the original colored image
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', 10);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Give a name to the title bar.
set(gcf,'name','Retinal Image Classification with Color Features and Superpixels','numbertitle','off') 
%-----------------------------------------------------------------------------------------------------------------
%Convert the image to the L*a*b* color space.
Alab = rgb2lab(rgbFixed);
%Calculate superpixels of the image.
[L,N] = superpixels(Alab,10000,'isInputLab',true);
BW = boundarymask(L);
subplot(2,2,2)
imshow(imoverlay(InputImage,BW,'blue'));
title('Overlay of boundary mask over the original image');
%Create a cell array of the set of pixels in each region.
pixelIdxList = label2idx(L);
%Determine the median color of each superpixel region in the L*a*b* color space.
[m,n] = size(L);
meanColor = zeros(m,n,3,'single');
for i = 1:N
    meanColor(pixelIdxList{i}) = mean(Alab(pixelIdxList{i}));
    meanColor(pixelIdxList{i}+m*n) = mean(Alab(pixelIdxList{i}+m*n));
    meanColor(pixelIdxList{i}+2*m*n) = mean(Alab(pixelIdxList{i}+2*m*n));
end
%Cluster the color feature of each superpixel by using the imsegkmeans function.
numColors = 5;
[Lout,cmap] = imsegkmeans(meanColor,numColors,'numAttempts',10);
cmap = lab2rgb(cmap);
subplot(2,2,3);
imshow(label2rgb(Lout));
title('Clustering of colors using imsegkmeans function');
%Use cluster centers as the colormap for a thematic map. 
subplot(2,2,4)
imshow(double(Lout),cmap)
title('Thematic map')
0 Comments
Accepted Answer
  Image Analyst
      
      
 on 6 Nov 2022
        Perhaps you mean something more like this:
% Demo by Image Analyst
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
clear;  % Erase all existing variables. Or clearvars if you want.
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
markerSize = 40;
rgbImage = imread('peppers.png');
%superpixels and imsegk-means function for color classificatsion
%show the original colored image
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', 10);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Give a name to the title bar.
set(gcf,'name','Retinal Image Classification with Color Features and Superpixels','numbertitle','off') 
%-----------------------------------------------------------------------------------------------------------------
%Convert the image to the L*a*b* color space.
Alab = rgb2lab(rgbImage);
%Calculate superpixels of the image.
[L,N] = superpixels(Alab,10000,'isInputLab',true);
BW = boundarymask(L);
subplot(2,2,2)
imshow(imoverlay(rgbImage,BW,'blue'));
title('Overlay of boundary mask over the original image');
%Create a cell array of the set of pixels in each region.
pixelIdxList = label2idx(L);
%Determine the median color of each superpixel region in the L*a*b* color space.
[m,n] = size(L);
meanColor = zeros(m,n,3,'single');
for i = 1:N
    meanColor(pixelIdxList{i}) = mean(Alab(pixelIdxList{i}));
    meanColor(pixelIdxList{i}+m*n) = mean(Alab(pixelIdxList{i}+m*n));
    meanColor(pixelIdxList{i}+2*m*n) = mean(Alab(pixelIdxList{i}+2*m*n));
end
%Cluster the color feature of each superpixel by using the imsegkmeans function.
numColors = 5;
[Lout,cmap] = imsegkmeans(meanColor,numColors,'numAttempts',10);
cmap = lab2rgb(cmap);
subplot(2,2,3);
imshow(label2rgb(Lout));
title('Clustering of colors using imsegkmeans function');
%Use cluster centers as the colormap for a thematic map. 
subplot(2,2,4)
imshow(double(Lout),cmap)
title('Thematic map')

3 Comments
  Image Analyst
      
      
 on 6 Nov 2022
				Did you look at Lout?  Just add this code to show each class:
for k = 1 : max(Lout(:))
    figure
    imshow(Lout == k);
    caption = sprintf('Class #%d', k);
    title(caption)
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!