MATLAB: Drawing bounding box onto an image without imshow and hold on …

rectangleregionprops

Hi there. I was wondering if its possible to draw a rectangle, whose coordinates is obtained from regoingprops. I however, am not interested to show the image first. What I intend to do is to get the bounding box coordinates form regionprops, and then just superimpose it onto an image without displaying it. I would then like to save the image (saving is ok… just drawing the rectangle got me a bit tangled up 😛 ). Would appreciate any advice!
The code I'm currently using is as shown above. (It's in a for loop) where 'l' is the counter.
THANKS in advance!!!
f = figure,imshow(frame, 'Border', 'tight');
hold on;
rectangle('Position',s(l).BoundingBox,'EdgeColor','y','LineWidth',2)
print(f, '-r80', '-djpeg', [dirName 'results' '\' jpegFiles(i).name]);
close all;

Best Answer

If you have the Computer Vision Toolbox then you can use shapeInserter
If you do not have that, then you can just assign values to the appropriate rows and columns in the array. Watch out for the possibility that the values for the coordinates are not integral
bb = s(l).BoundingBox;
from_row = ceil(bb(2));
to_row = from_row + bb(4) - 1;
from_col = ceil(bb(1));
to_col = from_col + bb(3) - 1;
yellow = [255, 255, 0];
%left and right side
frame(from_row:to_row, [from_col, to_col], 1) = yellow(1);
frame(from_row:to_row, [from_col, to_col], 2) = yellow(2);
frame(from_row:to_row, [from_col, to_col], 3) = yellow(3);
%top and bottom
frame([from_row, to_row], from_col+1:to_col-1, 1) = yellow(1);
frame([from_row, to_row], from_col+1:to_col-1, 1) = yellow(2);
frame([from_row, to_row], from_col+1:to_col-1, 1) = yellow(3);