Convert RGB image to label
7 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
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
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.
More Answers (1)
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!