Matlab:使用名称创建矩形框

时间:2017-04-04 08:21:06

标签: image matlab box

目前,我尝试在白色物体周围画一个矩形框,并用标签表示其大小。 我想创建4种尺寸:

  1. 没有
  2. Example of small objects

    我不知道如何创建它。

1 个答案:

答案 0 :(得分:0)

使用insertObjectAnnotation

% generate image
bw = zeros(1000);
[xg,yg] = meshgrid(1:1000);
rads = 10:20:100;
for ii = 1:length(rads)
    r = rads(ii);
    c = randi([1+r,1000-r],[1,2]);
    bw = bw | ( ((xg - c(1)).^2 + (yg - c(2)).^2) < r^2);
end
% extract region properties
props = regionprops(bw,'Area','BoundingBox');
% add labels
sizes = [0,2000,6000,10000];
labels = {'tiny','small','medium','large'};
colors = autumn(numel(labels));
bw = double(bw);
for ii = 1:numel(props)
    labelidx = find(props(ii).Area > sizes,1,'last');
    bw = insertObjectAnnotation(bw,'rectangle',...
        props(ii).BoundingBox,labels{labelidx},...
        'Color',colors(labelidx,:),'FontSize',32);
end
imshow(bw);

enter image description here

编辑:将此应用于OP的图片:

enter image description here

相关问题