How to Segment a object's based on color pixel value ? and how to labelling it ?
    4 views (last 30 days)
  
       Show older comments
    
How to Segment a object's based on color ?and how to labelling it ? Image dynamic ?

0 Comments
Answers (1)
  Image Analyst
      
      
 on 17 Feb 2021
        You can use the Color Thresholder on the Apps tab of the tool ribbon.  Tell it to export the code.  Then you can pass the mask into bwlabel() or bwconncomp().
For the image you've shown, it looks like it was already labeled and passed in to label2rgb() function to produce a pseudocolored image.  If not, then tell me how you obtained that image and why you don't have the labeled image for it.
3 Comments
  Image Analyst
      
      
 on 17 Feb 2021
				See my File Exchange where I have several demos where I use color segmentation without the color thresholder.
And will you respond to my last paragraph?
  Image Analyst
      
      
 on 26 Jun 2021
				You can create a histogram (256x256x256) of colors, then use unique() to get the unique colors.  Then use a for loop to find all pixels of each color in an RGB image and assign a labeled image the color index.  Something like (untested)
[rows, columns, numColorChannels] = size(rgbImage);
hist3d = zeros(256,256,256);
for col = 1 : columns
    for row = 1 : row
        r = rgbImage(row, column, 1);
        g = rgbImage(row, column, 2);
        b = rgbImage(row, column, 3);
        hist3d(r, g, b) = hist3d(r, g, b) + 1;
    end
end
row = 1;
uniqueColors = zeros(nnz(hist3d), 3);
for r = 0 : 255
    for g = 0 : 255
        for b = 0 : 255
            uniqueColors(row, :) = [r, g, b];
        end
    end
end
% Create labeled image.
labeledImage = zeros(rows, columns);
for row = 1 : size(uniqueColors, 1)
    maskr = rgbImage(:, :, 1) == uniqueColors(row, 1);
    maskg = rgbImage(:, :, 2) == uniqueColors(row, 2);
    maskb = rgbImage(:, :, 3) == uniqueColors(row, 3);
    mask = maskr & maskg & maskb; % Mask of where this RGB color occurs in the image.
    labeledImage(mask) = row;
end
imshow(labeledImage, []);
title('Labeled Image')
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
