Drawing a bounding box around foreground objects

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 comentarios

muthu A
muthu A el 16 de Feb. de 2017
Hai , i am a beginner of matlab.I want matlab code for object detection using backgtround subtraction.i have extract frames from video.now i want to detect object from those frames.pls let u know those who have code for this..
Image Analyst
Image Analyst el 16 de Feb. de 2017
muthu, start a brand new question on this and attach two of the frames.
%%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
*by this you can divide video into frame and compare new frame generated to some previous image using subtraction method
but i am not able to draw boundary in video or raise alarm pldease help me if you get *
Image Analyst
Image Analyst el 23 de Mzo. de 2017
Simply put hold on, then draw using plot() or rectangle(), then call drawnow.
how to use hold on,plot or rectangle please help me out
hold on;
rectangle(.......whatever, or
plot(............ whatever.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 7 de Jun. de 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 comentarios

Still it is not drawing on detected objects.When I apply frame differencing backgrouns Subtraction it is grawing bounding Box ob detected objects.But when I apply this code on other background subtraction algorithms it is not drawing..can you help to sort out this problem..
How can I convert frame numner in to time format?
Image Analyst
Image Analyst el 7 de Jun. de 2013
Try multiplying by the time between frames.
Ahh..ok I need to try..secondly can you tell me how I compute true positive and true negative for this algorithm is there any function in matlab?I want to measure the F-score for this algorithm.
Thanks
Image Analyst
Image Analyst el 10 de Jun. de 2013
You would have to manually specify that, unless you have a second automated way that you trust that you can use.
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".
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

Iniciar sesión para comentar.

Más respuestas (1)

Mohammedashraf Shaikh
Mohammedashraf Shaikh el 23 de Mzo. de 2017

0 votos

%%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

Preguntada:

el 7 de Jun. de 2013

Comentada:

el 25 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by