1

我目前正在为 MATLAB 中的卫星图像添加注释。由于每个文本字段下方的颜色变化很大,因此我想在文本下方使用背景颜色以使其更易于查看和阅读。

但是,当我这样做时,很多地形都会变得模糊。我虽然试图使每个文本框的背景颜色半透明,但在想出一个解决方案时遇到了死胡同。

有任何想法吗?我希望有一些 UI 元素,我可以将“facealpha”设置为 0.5。我还需要支持旋转的文本(如下面的示例所示)。

下面是一些示例代码和生成的图像。带有卫星数据的工作空间也可以在链接中找到: 示例工作空间

figure(1);clf
imagesc(xx,yy,Map);

hold on
plot(xInspection,yInspection,'g.-')

% # Two ways of making a rotated text annotation. 
% # Cant make background semi-transparent
testAnno= annotation('textarrow',[0.5 0.5],[0.5 0.5], ...
                'string','textarrow annotation', ...
                'HeadStyle','none','LineStyle', 'none',...
                'TextRotation',asin(directionVec(1))*180/pi,...
                'TextBackgroundColor',[0.7 0.7 0.7]);

testText = text(mean(xInspection),mean(yInspection),'text annotation', ...
        'rotation',asin(directionVec(1))*180/pi, ...
        'HorizontalAlignment','right', ...
        'color',[0 0 0], ...
        'backgroundcolor',[0.7 0.7 0.7], ...
        'fontsize',8);

在此处输入图像描述

4

2 回答 2

4

It doesn't look like either annotation or text return HgObjects that have BackgroundAlpha properties (they might exist but I wasn't able to find them using getundoc or by trying various different hacks).

I was able to get something working by drawing the background myself. Here is a simple proof of concept:

f = figure;
tObj = text(.5, .5, 'text object', 'FontSize', 20);
set(gca,'XLimMode', 'manual', 'YLimMode', 'manual'); % prevent the axes from resizing automatically
p = get(tObj, 'Extent'); %Get the outer position of the text

% now create a  patch around the text object
pObj = patch([p(1) p(1) p(1)+p(3) p(1)+p(3)], [p(2) p(2)+p(4) p(2)+p(4) p(2)], 'r');
uistack(tObj, 'top'); % put the text object on top of the patch object

set(pObj , 'FaceAlpha', .2); % set the alpha of the patch face to .2

%Rotate the objects
set(tObj, 'Rotation', 20);
rotate(pObj, [0 0 1], 20);
于 2012-11-05T14:29:52.913 回答
2

I am afraid the only way you can do this is by not setting any color to your annotations, and then placing a patch in the background of each annotation. So something like this:

% Use completely transparent annotations
hA = annotation('textarrow', ..., 'TextBackgroundColor', 'none')

% Place a transparent patch exactly in the background of your annotation
hP = patch(X, Y, 'white', 'EdgeColor', 'none', 'FaceColor', 'white', ...
    'alpha', 0.3)

% Ensure that your annotation is on top
uistack(hA, 'top')

But of course the big problem is to determine the correct coordinates of the patch (X and Y). Rotating is easy by simply multiplying your coordinates by a rotation matrix. However, finding the length and height of the patch and its central location is not that easy. You might be able to find some useful functions for this at Matlab central...

于 2012-11-05T14:29:38.887 回答