Drawing a bounding box around foreground objects

7 views (last 30 days)
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'));
  6 Comments
Mohammedashraf Shaikh
Mohammedashraf Shaikh on 25 Mar 2017
how to use hold on,plot or rectangle please help me out
Image Analyst
Image Analyst on 25 Mar 2017
hold on;
rectangle(.......whatever, or
plot(............ whatever.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Jun 2013
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+')
  7 Comments
Algorithms Analyst
Algorithms Analyst on 10 Jun 2013
Ok thank you I have computed it.I want to control the looping in your frames wrting code as you described here (<http://www.mathworks.co.kr/matlabcentral/answers/57299-adaptive-background-subtraction-algorithm>)
So when I start to write frames into disk the message appear everytime "wrote 1 frames out of 100" "wrote 2 frame out of 100"
I want restrict this message just one time.I mean is this possible to control just poping up one time.e.g
"Wrote 1 frame out of 100". just one time so when done it should say "Wrote 100 frame out of 100".
Algorithms Analyst
Algorithms Analyst on 10 Jun 2013
Here you have write it in loop
for f=1:numberofframes thisframe=read(vid,f); imshow(thisframe);
ProgressIndication=sprintf("wrote frames %d out of ",frame); I wan to control this.
end

Sign in to comment.

More Answers (1)

Mohammedashraf Shaikh
Mohammedashraf Shaikh on 23 Mar 2017
%%Extracting & Saving of frames from a Video file through Matlab Code%% clc; close all; clear all;
% assigning the name of sample avi file to a variable filename = 'C:\Users\Hp\Desktop\xyz.mp4';
%reading a video file mov = VideoReader(filename);
% Defining Output folder as 'snaps' %opFolder = fullfile(cd, 'SEM-8'); %if not existing %if ~exist(opFolder, 'dir') %make directory & execute as indicated in opfolder variable %mkdir('C:\Users\Hp\Desktop\opFolder'); %end
%getting no of frames numFrames = mov.NumberOfFrames;
%setting current status of number of frames written to zero numFramesWritten = 0;
%for loop to traverse & process from frame '1' to 'last' frames for t = 1 : numFrames currFrame = read(mov, t); %reading individual frames opBaseFileName = sprintf('%3.3d.png', t); opFullFileName = fullfile('C:\Users\Hp\Desktop\col1', opBaseFileName); imwrite(currFrame, opFullFileName, 'png'); %saving as 'png' file %indicating the current progress of the file/frame written progIndication = sprintf('Wrote frame %4d of %d.', t, numFrames); disp(progIndication); numFramesWritten = numFramesWritten + 1; end %end of 'for' loop progIndication = sprintf('Wrote %d frames to folder "%s"',numFramesWritten, 'C:\Users\Hp\Desktop\col1'); disp(progIndication); %End of the code I=imread('C:\Users\Hp\Desktop\col\001.png'); I1=rgb2gray(I); imshow(I1); J=imread('C:\Users\Hp\Desktop\col1\001.png');
I1=rgb2gray(I); J1=rgb2gray(J);
imshow(I1); imshow(J1); K=I1-J1; figure; imshow(K); title('SUBTRACTED IMAGE ');
*TILL Now i had this which subtracting two image AND getting foreground By using my code how could i draw Boundary

Community Treasure Hunt

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

Start Hunting!