0% found this document useful (0 votes)
4 views5 pages

Simple Blob Tracking in Computer Vision

Uploaded by

Bruce Creations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Simple Blob Tracking in Computer Vision

Uploaded by

Bruce Creations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EEE3032 – Computer Vision and Pattern Recognition (handout)

Simple Blob Tracker


Lecture 13 – Tracking (I)

close all;
clear all;

F=aviread('c:\visiondemo\ball_uncomp.avi');
%F=aviread('c:\horse_ucomp.avi');
NUMFRAMES=length(F);

%% 1) Get a colour model from Frame 1


img=double(F(1).cdata)./255;
imshow(img);
[u v]=ginput(2);
topleft=round([u(1);v(1)]);
botright=round([u(2);v(2)]);

samps=[]; % will store RGB values of user-selected pixels


for x=topleft(1):botright(1)
for y=topleft(2):botright(2)
r=img(y,x,1);
g=img(y,x,2);
b=img(y,x,3);
samps=[ samps [ r ; g ;b ] ] % one column per rgb
end
end

% build eigenmodel
e=Eigen_Build(samps);

lastpos=round(topleft+(botright-topleft)./2);

%% 2) Track from frame 2 onwards


history=[];
for i=2:NUMFRAMES

% reshape the pixels in the image to be 1 column per pixel (r,g,b)


img=double(F(i).cdata)./255;
samps=[ reshape(img(:,:,1),1,[]) ; reshape(img(:,:,2),1,[]) ;
reshape(img(:,:,3),1,[]) ];

% get Mahalanobis distance of each pixel from eigenmodel


dst=Eigen_Mahalanobis(samps,e);
% reshape the Mahalanobis distances into a rectangular image
dst=reshape(dst,size(img,1),size(img,2));

% threshold the Mahalanobis distances


thresholded=dst<3;

% dilate the mask a bit


structuringelement = strel('square',3);
thresholded=imdilate(thresholded,structuringelement);

1 of 5
EEE3032 – Computer Vision and Pattern Recognition (handout)

% find all the centroids of the next frame's connected components


map=bwlabel(thresholded);
reglabels=setdiff(unique(map),0);
fprintf('There are %d connected components\n',max(reglabels));
for cc=1:max(reglabels)
mask=(map==cc);
[y x]=find(mask);
possiblecentroid{cc}=[mean(x);mean(y)];
end

bestdist=inf;
bestidx=-1;
for cc=1:max(reglabels)
thisdist=sqrt(sum((possiblecentroid{cc}-lastpos).^2));
if (thisdist<bestdist)
bestdist=thisdist;
bestidx=cc;
end
end

currentpos=possiblecentroid{bestidx};

history=[history currentpos];

% display
cla; hold on;
thresholded(:,:,2)=thresholded(:,:,1);
thresholded(:,:,3)=thresholded(:,:,1);
imgshow([img thresholded]);
plot(history(1,:),history(2,:),'y-');
plot(currentpos(1),currentpos(2),'m*');
drawnow;
title(i);

lastpos=currentpos;
end

2 of 5
EEE3032 – Computer Vision and Pattern Recognition (handout)

Descriptor based tracking (using RGB Histogram)


Lecture 13 – Tracking (I)

close all;
clear all;

%F=aviread('c:\visiondemo\ball_uncomp.avi');
F=aviread('c:\horse_ucomp.avi');
NUMFRAMES=length(F);

%% 1) Cut a template from Frame 1


img=double(F(1).cdata)./255;
imshow(img);
[u v]=ginput(2);
topleft=round([u(1);v(1)]);
botright=round([u(2);v(2)]);

targetwnd =img(topleft(2):botright(2),topleft(1):botright(1),:);
targetdesc=getTrackDescriptor(targetwnd);
[winh winw winc]=size(targetwnd);

lastpos=topleft+(botright-topleft)./2;

%% 2) Track from frame 2 onwards


history=[];
for i=2:NUMFRAMES

% reshape the pixels in the image to be 1 column per pixel (r,g,b)


img=double(F(i).cdata)./255;

STEP=2;
halfwinw=floor(winw/2);
halfwinh=floor(winh/2);
scores=ones(size(img,1),size(img,2)).*inf;
for x=(1+halfwinw):STEP:(size(img,2)-(halfwinw+1))
for y=(1+halfwinh):STEP:(size(img,1)-(halfwinh+1))
wnd=img((y-halfwinh):(y+halfwinh),(x-halfwinw):(x+halfwinw),:);
thisdesc=getTrackDescriptor(wnd);
eucdst=sqrt(sum((targetdesc-thisdesc).^2));
scores(y,x)=eucdst;
end
end

% threshold the descriptor distances


thresholded=scores<0.25;

% dilate the mask a bit


structuringelement = strel('square',3);
thresholded=imdilate(thresholded,structuringelement);

% find all the centroids of the next frame's connected components

3 of 5
EEE3032 – Computer Vision and Pattern Recognition (handout)

map=bwlabel(thresholded);
reglabels=setdiff(unique(map),0);
if isempty(reglabels)
fprintf('No connected components\n');
continue;
end
fprintf('There are %d connected components\n',max(reglabels));
for cc=1:max(reglabels)
mask=(map==cc);
[y x]=find(mask);
possiblecentroid{cc}=round([mean(x);mean(y)]);
end

bestdist=inf;
bestidx=-1;
for cc=1:max(reglabels)
thisdist=sqrt(sum((possiblecentroid{cc}-lastpos).^2));
if (thisdist<bestdist)
bestdist=thisdist;
bestidx=cc;
end
end

if (bestidx==-1)
fprintf('Not found! skipping frame\n');
continue;
end
currentpos=possiblecentroid{bestidx};

history=[history currentpos];

% display
cla; hold on;
thresholded(:,:,2)=thresholded(:,:,1);
thresholded(:,:,3)=thresholded(:,:,1);
imgshow([img thresholded]);
plot(history(1,:),history(2,:),'y-');
plot(currentpos(1),currentpos(2),'m*');
drawnow;
title(i);

lastpos=currentpos;
end

4 of 5
EEE3032 – Computer Vision and Pattern Recognition (handout)

Compute an RGB Histogram from an image


(used in Descriptor based tracking)
Lecture 13 – Tracking (I)

function F=getTrackDescriptor(img)

red=img(:,:,1);
green=img(:,:,2);
blue=img(:,:,3);

red=reshape(red,1,[]);
green=reshape(green,1,[]);
blue=reshape(blue,1,[]);

%F=[mean(red) ; mean(green); mean(blue)];


%return;

Q=4; % Split RGB space into 4x4x4 cubes

sf=1./Q;
redindex=floor(red./sf); % when Q=4 each pixel is in set {0,1,2,3}
greenindex=floor(green./sf);
blueindex=floor(blue./sf);

N=redindex*(Q^2) + greenindex*Q + blueindex;


bins=Q^3;

% Make histogram from N


F=hist(N,1:bins);

% Normalise the histogram


F=F./sum(F);

5 of 5

You might also like