label2rgb and rgb2label

조회 수: 6 (최근 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