label2rgb and rgb2label

14 Ansichten (letzte 30 Tage)
RuiQi
RuiQi am 15 Jul. 2017
Kommentiert: Walter Roberson am 28 Mär. 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 ?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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()

Weitere Antworten (1)

dsds dsds
dsds dsds am 28 Mär. 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));
bwlabel(mask)=1;
  1 Kommentar
Walter Roberson
Walter Roberson am 28 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by