Clear Filters
Clear Filters

label2rgb and rgb2label

8 views (last 30 days)
RuiQi on 15 Jul 2017
Commented: Walter Roberson on 28 Mar 2018
RGB = label2rgb(L) converts my label array to a RGB array. Is there a function that converts the RGB array back to the original label array ?

Accepted Answer

Walter Roberson
Walter Roberson on 15 Jul 2017
rgb2ind() . If you did not pass a colormap into label2rgb then "the default value is 'jet'." so you should pass that into rgb2ind()

More Answers (1)

dsds dsds
dsds dsds on 28 Mar 2018
you should know your label color in rgb format and find pixels which corresponding to the label color in the rgb groundtruth image.then , retrieve them by rgb values .
labelcolor = [r0,g0,b0];
mask = (rgblabel(:,:,1)==r0)&(rgblabel(:,:,1)==g0)&(rgblabel(:,:,3)==b0);
bwlabel = zeros(size(labelcolor,1),size(labelcolor,2));
  1 Comment
Walter Roberson
Walter Roberson on 28 Mar 2018

Caution: if your values are in floating point format then you risk having problems with floating point round-off. Values in floating point format should seldom be compared for bit-for-bit equality.

Also, the above assumes that there is only one labelcolor, which is generally not the case. You would generally need to use

unique_rgb = uniquetol(reshape(rgblabel,[], 3));

and then work through all of the rows. Now, what you should consider to make it easier is

[label_colormap, label_idx] = uniquetol(reshape(rgblabel, [], 3));
misordered_label_matrix = reshape(label_idx, size(labelcolor,1), size(labelcolor,2));

this gives a color index for each pixel in the original image.

If you just want a pseudo-color image together with a colormap then label_colormap and misordered_label_matrix together will do the job. However, when the task is stated as reversing the effects of label2rgb then generally the person wants the colormap used to be the same as in the original image, and wants the index value to be the same as the original -- which the above does not do. You would have to match each row in label_colormap to the rows of the colormap that were used for label2rgb() to find the closest match (in some sense), and then you would have to map the index that was returned in misordered_label_matrix to the index in the original colormap.

All of this is done for you if you just use rgb2label() passing in the colormap that was used for the label2rgb() call.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!