label2rgb and rgb2label

7 ビュー (過去 30 日間)
RuiQi
RuiQi 2017 年 7 月 15 日
コメント済み: Walter Roberson 2018 年 3 月 28 日
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 ?

採用された回答

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

その他の回答 (1 件)

dsds dsds
dsds dsds 2018 年 3 月 28 日
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 件のコメント
Walter Roberson
Walter Roberson 2018 年 3 月 28 日

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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by