Convert RGB image to label

4 views (last 30 days)
RuiQi
RuiQi on 14 Jul 2017
Commented: Image Analyst on 15 Jul 2017
This function converts my labeled image into an RGB https://www.mathworks.com/help/images/ref/label2rgb.html Is it possible to convert an RGB image into a labeled one ? Because the algorithm I am running returns me a color per label but I need the label only.
I tried the solution posted below but it didnt give me the right results. The first image is the output of the algorithm, each cluster in the image has its own color. The second image is the result of running the code below.
BW = im2bw(I, graythresh(I));
CC = bwconncomp(BW);
L = labelmatrix(CC);
imagesc(L);

Accepted Answer

Image Analyst
Image Analyst on 14 Jul 2017
rgb2ind() will come up with some kind of color classification and give you the classified image back. Then each class (index) can be shown as a color. This is a completely different concept than what label2rgb does. That takes a labeled image, which is a binary image of segmented blobs that's been run though bwconncomp() or bwlabel(), and then colors each class (label) with a unique color. They're TOTALLY different things. And I'm not sure what thing you want.
Do you want to take a full color image and quantize it down into a small number of representative colors? If so, use rgb2ind().
Do you want to take a grayscale image, segment it to find certain interesting blobs/regions in the image (like person, wall, and floor), and then give each one of those regions a unique color? If so, use normal segmentation and classification techniques to produce a segmented/classified image and then label it so that each region gets a unique ID number, like person = 1, wall = 2, and floor = 3. Then use label2rgb() to assign colors to those regions.
Again, different concepts. What do you want to do?
  2 Comments
RuiQi
RuiQi on 15 Jul 2017
Sorry, I have an algorithm that assigns a label to each region. The problem is, the executable outputs a colored image (3 channel). I need to convert it back to a labeled image for processing.
1. Image
2. Segment (each pixel has a label)
3. Convert to rgb
4. Convert back to label
I need step 4. I cant modify step 2 and 3.
Image Analyst
Image Analyst on 15 Jul 2017
If you're starting with your top image:
Then each region, large or small, has a label. Then the algorithm does not give you the labeled image but evidently gives you the pseudocolored image as its only output and there is no way you can get the original labeled image because the algorithm is like an impenetrable black box (like a DLL or something). And you're evidently unable to get the developer to change the black box to also output the labeled image. So do I have that correct?
The problem is the colormap used to peseudocolor the image has only a certain number of colors, not a unique color for each label. Is that correct (some colors are reused)? Like the same magenta shows up on several blob regions? And I'm assuming you don't have the colormap used to make this, correct? So what you'll have to do is to first find out how many unique colors you have. You can do that by making a 3-D histogram. Now you just assign every count in the 3-D histogram a number, for example it could simply be the linear index of the count like you'd get with the find(rgbHistogram(:)) function.
The simplest solution is for you to just change your executable to also output the labeled image.

Sign in to comment.

More Answers (1)

Jan
Jan on 14 Jul 2017
Did you see the examples in the doc of label2rgb?
BW = im2bw(I, graythresh(I));
CC = bwconncomp(BW);
L = labelmatrix(CC);
bwlabeln might be useful also, but you need a BW image at first.
  1 Comment
RuiQi
RuiQi on 14 Jul 2017
Hi Simon, I tried your solution but its not giving me the right results. I have been looking at https://www.mathworks.com/help/matlab/ref/rgb2ind.html but Im worried it might not do a proper conversion from rgb back into labels.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!