2017-04-04 81 views
0

目前,我嘗試在帶有標籤的白色對象周圍繪製矩形框,以指示其大小。 我想創建4個類別的大小:Matlab:創建帶名稱的矩形框

  1. 不要有

Example of small objects

我不知道如何創建這個。

+3

歡迎來到本網站!您可能想要參觀:http://stackoverflow.com/tour,瞭解此網站的工作原理。另請檢查如何創建一個最簡單的示例:http://stackoverflow.com/help/mcve現在的問題無法解答。您可以編輯問題以包含一個獨立的代碼示例,一個簡短的錯誤解釋以及您真正想看到的內容。 –

回答

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

+0

謝謝。但現在我適用於我有的圖像。仍然不能 –

+0

我嘗試應用於我想要的圖像,但仍然不能。 –

+0

你是什麼意思「不能」?我將它應用到你的圖像上,它工作得很好。究竟是什麼問題? – user2999345