MATLAB: Placing a Title on Bounding Box

boundingboxrectangleregionprops

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.

Best Answer

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.