在matlab中使用insertshape函数插入直线后添加图例
我的代码如下所示:在matlab中使用insertshape函数插入直线后添加图例,matlab,legend,imshow,Matlab,Legend,Imshow,我的代码如下所示: p = imread('C.png'); p1 = im2double(p); RG = insertShape(p1, 'Filledcircle', pos1, 'LineWidth', 10,'Color','blue','Opacity',1); RG = insertShape(RG, 'Line', {line1,line2},'Color',{0 1 0;0 1 1}); hc = imshow(RG); legend(hc,'line1','line2');
p = imread('C.png');
p1 = im2double(p);
RG = insertShape(p1, 'Filledcircle', pos1, 'LineWidth', 10,'Color','blue','Opacity',1);
RG = insertShape(RG, 'Line', {line1,line2},'Color',{0 1 0;0 1 1});
hc = imshow(RG);
legend(hc,'line1','line2');
legend('show');
我从使用图像查看器应用程序插入的圆中获取x和y坐标,这不是将坐标连接在一起的正确方法。
insertShape
直接修改图像的RGB数据,不会为插入的形状生成图形对象。因此,图例
没有要显示的图形句柄(另请参见:
作为一种解决方法,您可以为每个形状生成一个虚拟线系列。因为NaN
值不是由MATLAB可视化渲染的,所以可以使用它们创建一个不显示在轴上的线对象
例如:
% Read sample image
RGB = imread('peppers.png');
imshow(RGB);
% Add some annotations
dim1 = [0.3 0.7 0.2 0.2];
annotation('rectangle', dim1, 'Color', 'red')
dim2 = [0.6 0.5 0.1 0.1];
annotation('rectangle', dim2, 'Color', 'blue')
x1 = [0.5 0.6];
y1 = [0.7 0.5];
annotation('line', x1, y1, 'Color', 'green', 'LineWidth', 2)
x2 = [0.5 0.7];
y2 = [0.9 0.6];
annotation('line', x2, y2, 'Color', 'magenta', 'LineWidth', 2)
% ============== EVERYTHING ABOVE THIS LINE IS SPECIFIC TO MY EXAMPLE, IGNORE IT
% Plot dummy lineseries for legend
% Specify 'DisplayName' for legend, can also be done manually in legend call
hold on
tmp(1) = plot(NaN, 'Color', 'green', 'LineWidth', 2, 'DisplayName', 'Green Line');
tmp(2) = plot(NaN, 'Color', 'magenta', 'LineWidth', 2, 'DisplayName', 'Magenta Line');
hold off
% Display legend
legend(tmp)
这将产生以下结果:
我不确定,但我认为
图例
仅适用于绘图
,分散
等。尝试在所需位置写入字符串,用作图例
的替代品!换句话说,图例
可能需要一个轴
对象来关联。感谢您的评论,我需要连接2个圆乘2条线,我有x和y坐标,如何使用注释:line1=[494286561235];Line2=[580228672197]我试过了,但似乎对我不起作用annotation@Kira你不需要使用注释
。像你已经在做的那样使用插入形状
。我只使用了注释
,因为我没有计算机视觉工具箱。我也尝试过,如果我在图例之前显示,仍然不会显示图例,但是如果我最后调用了图例功能,然后只显示图例,没有image@Kira然后用不起作用的代码更新你的问题。在我的示例中,我在imshow
之后调用legend
,没有任何问题。它现在似乎起作用,但我使用plot函数而不是注释,尽管我的x和y坐标是完全不匹配,我无法连接圆圈,将立即更新代码,谢谢