Drawing bounding box onto an image without imshow and hold on ...

19 views (last 30 days)
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 :P ). 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;

Accepted Answer

Walter Roberson
Walter Roberson on 9 Dec 2015
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);
  3 Comments
Walter Roberson
Walter Roberson on 9 Dec 2015
Opps, yes, I copied and pasted the lines and forgot to change the 1's.
RGB images are 3 dimensional, with (:,:,1) referring to the Red component, (:,:,2) referring to the Green component, and (:,:,3) referring to the Blue component.
Sandipan Dey
Sandipan Dey on 23 Aug 2022
Edited: Sandipan Dey on 23 Aug 2022
or slightly moifiying the abovr code to change color channels at once and draw a bounding box (e.g., one obtained using regionprops) with a given (odd) width (e.g., 3):
function frame = drawBB(bb, frame, width)
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];
for i = -fix(width/2):fix(width/2)
%left and right side
frame(from_row:to_row, [from_col+i, to_col+i], :) = ...
permute(repmat(yellow, to_row-from_row+1, 1, 2), [1,3,2]);
%top and bottom
frame([from_row+i, to_row+i], from_col+1:to_col-1, :) = ...
permute(repmat(yellow, 2, 1, to_col-from_col-1), [1,3,2]);
end
end
The output can be seen in the following montage (the input image is taken from a coursera course by mathworks).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!