Placing a Title on Bounding Box

7 ビュー (過去 30 日間)
Amanda Johnson
Amanda Johnson 2014 年 8 月 6 日
回答済み: thang anh 2018 年 5 月 2 日
I would like to be able to add the title 'Defect' to my bounding box below. I have seen this done in many other applications such as object tracking. But I cannot seem to get it to work. Also, could this be done for multiple bounding boxes. Here is the code I have sof the bounding boxes:
%% Display Bounding Box
figure, imshow(DifferenceOf2),
title('Subtraction of Dialated Edge Template'),
[L ,Num] = bwlabel(DifferenceOf2);
stats = regionprops(DifferenceOf2, 'basic');
stats(1).Area;, ..., stats(end).Area;
stats.Area
allArea = [stats.Area];
s = regionprops(DifferenceOf2, 'all');
s(1)
centroids = cat(2, s.Centroid);
hold on
plot(centroids(:,1), centroids(:,2), 'r*')
for n=1: length(s)
rectangle('Position', s(n).BoundingBox, 'EdgeColor', 'g', 'LineWidth',2), title('Detected Defect')
end
hold off
.
. .
I would like have something like the figure below. I would rather it display the area and/or centroid of the defect as opposed to just 'Defect'.
.
.
. .
Please and Thank you for your help.

採用された回答

Chad Greene
Chad Greene 2014 年 8 月 6 日
Instead of the title command, consider using text. You can pin the text to some x,y location, then align the text around the pinning point by 'verticalalignment' and 'horizontalalginment' options.
  2 件のコメント
Amanda Johnson
Amanda Johnson 2014 年 8 月 6 日
Thank you for your help! I was able to get the text to appear in my figure, and changed it to red. I just used the values from the centroid for the X and Y values.
.
hold on
plot(centroids(:,1), centroids(:,2), 'r*')
for n=1: length(s)
rectangle('Position', s(n).BoundingBox, 'EdgeColor', 'g', 'LineWidth',2), title('Defected Detect')
H= text( centroids(:,1) +5, centroids(:,2),'Thank you Chad Greene! ');
set(gcf,'DefaultTextColor','red')
end
hold off
.
Chad Greene
Chad Greene 2014 年 8 月 6 日
Great, glad it works. Just a note, you don't need to change the text color of the whole figure if you don't want to. You could change the color of just that text bit by
H= text(centroids(:,1) +5, centroids(:,2),'You''re welcome Amanda Johnson','color','red','fontsize',44);
or even
H= text(centroids(:,1) +5, centroids(:,2),'You''re welcome Amanda Johnson');
set(H,'color','red','fontsize',44)

サインインしてコメントする。

その他の回答 (2 件)

Christopher Berry
Christopher Berry 2014 年 8 月 6 日
What you are looking for is either annotation. You can also you sprintf to get the string formatted with the information you want. For example,
str = sprintf('Defect at (%0.2f,%0.2f)',32.32443,1.1233)
annotation('textbox',[0.2,0.4,0.1,0.1],'String',str)
For your case, you can the the [x y width height] (given in my example as [0.2,0.4,0.1,0.1]) information for the annotation from your bounding box data.
Take a look at the documentation for annotation here
  5 件のコメント
Amanda Johnson
Amanda Johnson 2014 年 8 月 6 日
hey hey hey!! making progress... Thank you!
hold on
plot(centroids(:,1), centroids(:,2), 'r*')
for n=1: length(s)
rectangle('Position', s (n).BoundingBox, 'EdgeColor', 'g', 'LineWidth',2), title('Defected Detect')
H= text( centroids(:,1) +5, centroids(:,2), 'Thanks Christopher Berry... This is the actual Centroid! ');
set(gcf,'DefaultTextColor','red');
str = sprintf('Defect at (%0.2f,%0.2f)',centroids(:,1), centroids(:,2))
annotation('textbox',[0.5,0.4,0.1,0.1],'String',str)
end
hold off
.
.
.
Christopher Berry
Christopher Berry 2014 年 8 月 6 日
編集済み: Christopher Berry 2014 年 8 月 6 日
Haha, I like your text :)

サインインしてコメントする。


thang anh
thang anh 2018 年 5 月 2 日
thank

カテゴリ

Help Center および File ExchangeGeometric Geodesy についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by