how can i get a bounding box for the detected blob in a video

1 view (last 30 days)
hi, am intrested in knowing how to get a bounding box for the detected blob in a video my code involves background subraction & i use simple functions
clc
clear all
close all
m=0; n=0;
readerobj = mmreader('dt2.wmv');% dt2 is my sample fixed cam video
vidframes = read(readerobj);
thresh = 15;
bg = read(readerobj,1);
bg_bw = double(rgb2gray(bg));
fr_size = size(vidframes);
width = fr_size(2);
height = fr_size(1);
fg = zeros(height, width);
numFrames=get(readerobj,'NumberofFrames');
for k = 1 : numFrames
mov(k).cdata = vidframes(:,:,:,k);
mov(k).colormap = [];
end
movie(mov, 1, readerobj.FrameRate)
a=1; x=[0 0]; p=0; c=0; for i = 2:2:numFrames
fr = read(readerobj,i);
fr_bw = rgb2gray(fr);
fr_diff = abs(double(fr_bw) - double(bg_bw));
for j=1:width
for k=1:height
if ((fr_diff(k,j) > thresh))
fg(k,j) = 255; %fr_bw(k,j)
else
fg(k,j) = 0;
end
if (fr_bw(k,j) > bg_bw(k,j))
bg_bw(k,j) = bg_bw(k,j) + 1;
elseif (fr_bw(k,j) < bg_bw(k,j))
bg_bw(k,j) = bg_bw(k,j) - 1;
end
end
end
%median filter to remove noise
L=medfilt2(fg,[5,5]);
%removing small parts less than threshold area
final=bwareaopen(L,4000);
%filling the holes
ifill=imfill(final,'holes');
%to know the number of connected objects
[Ilabel num]=bwlabel(ifill);
if (num>=1)
%region properties of the image
Iprops=regionprops(Ilabel);
%extracting the bounding box properties
Ibox=[Iprops.BoundingBox];
????
after this i want a code which would continuously draw a bounding box arounf the blob?

Answers (1)

Image Analyst
Image Analyst on 6 May 2014
Try starting with something like this:
delete hRect; % Delete prior boxes.
for blobIndex = 1 : num
thisBB = Iprops(blobIndex).BoundingBox;
hRect(blobIndex) = rectangle('Position', thisBB);
end
You will have to initialize hRect the first time (to anything at all) so the delete doesn't bomb.
  3 Comments
Image Analyst
Image Analyst on 6 May 2014
You're welcome Cedric. I saw that, even though I didn't reply. I'm trying to remember to vote for your posts to get your privileges up.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!