MATLAB: Drawing a bounding box around foreground objects

drawing a bounding box around foreground objectsImage Processing Toolbox

Hello Everyone.
I have applied a backgrund subtraction algorithm called frame differencing it is working very well I can easily detect the moving objects from the video now I want to draw the rectanlge or bounding boc around the detected objects.I have used the connected component labelling here like that below provided by Image Analyst.But I could not draw the bounding box..Any help is appreciated.
figure(3);imshow(fg1);
drawnow;
hold on;
labeledImage = bwconncomp(fg1,8);
measurements = regionprops(labeledImage,'BoundingBox','Centroid');
if length(measurements) == 0
% No blobs found.
fprintf('Done!\n');
continue; % Skip to the next frame.
end
totalNumberOfBlobs = length(measurements);
for blobNumber = 1:totalNumberOfBlobs
bb = measurements(blobNumber).BoundingBox;
bco = measurements(blobNumber).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bco(1),bco(2),'-m+')
myCaption = sprintf('(x=%.1f, y=%.1f)', bco(1), bco(2));
text(bco(1)+15,bco(2), myCaption,…
'FontName','Arial','FontWeight','normal',…
'FontSize',12,'Color','blue');
set(gca,'xdir','normal')
set(gca,'ydir','reverse')
axis on;
end
drawnow; % Force display to update.
fprintf('Done!\n');
end
end
hold off
toc
uiwait(msgbox('Execution Ended'));

Best Answer

You probably need a "hold on" between these two lines:
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bco(1),bco(2),'-m+')
Like this:
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
hold on;
plot(bco(1),bco(2),'-m+')