我正在尝试为使用label2rgb的标签图像和自定义颜色贴图制作的彩色贴图图像制作图例.基本上我想要一个图例,显示颜色图颜色的小矩形样本以及自定义标签.
我希望能够设置标签和背景的颜色.使用imshow显示图像,显然图例对这些图形没有影响.
我已经尝试过使用colorbar来解决这个问题,如下所示,但我对它非常不满意,因为如果我调整绘图窗口的大小,一切都会被抛弃.
任何人都可以告诉我应该如何制作这种类型的图例,最好以某种方式使它浮动并在调整图形窗口大小时根据需要移动.如果这是不可能的,那么有人可以告诉我如何为彩色条的标签文本着色或给色条的边界框(包含其标签)一个背景颜色?如您所见,如果我没有手动添加背景白框,则标签将不可见.
谢谢.
label_image = zeros(768, 1024);
label_image(100:400, 500:600)=1;
label_image(500:600, 100:600)=2;
label_image(25:300, 100:400)=3;
custom_colormap = [ 0 1 0; 1 0 0; 0 0 1;];
label_image = label2rgb(label_image, custom_colormap, [0 0 0]);
% Make a white backround for the colorbar.
% Don't want to have to do this line.
label_image(25:140, 775:1010,:)=255;
% Show the image
imshow(label_image, 'InitialMagnification', 50);
% Here, I would like to be able to set colorbar label text color and/ or
% colorbar bounding box background color, or better yet, use some variant
% on the legend function to do this automatically
colormap(custom_colormap);
labels={'Both', 'Always Moving', 'Moved'};
hcb=colorbar('peer',gca,...
[0.7 0.8 0.03 0.1],...
'YTickLabel',labels,...
'XAxisLocation','bottom',...
'TickLength',[0 0],...
'Color',[1 0 1]);