% my requirements is to track a "red color circular object like ball" but it is tracking "red color" "objects" moreover it is not doing the kalman tracking please help me!!!!

vid=videoinput('winvideo',2,'I420_320x240');% initialising video
set(vid,'FramesPerTrigger',Inf);
set(vid,'ReturnedColorspace','rgb');
vid.FrameGrabInterval=2;
framesneeded=300;
t=0.01; %time to take readings
x=[0;0]; % nitialising
a=0; % initialising
A = [1 t; 0 1] ; %kalman parameters
D = [t^2/2; t]; %kalman parameters
C = [1 0];% kalman para meters
xnoise = 0.05;
znoise = 10;
Ez = znoise^2;
Ex = xnoise^2 * [t^4/4 t^3/2; t^3/2 t^2];
P = Ex;
cent1=0; %initialising
cent2=0;%initialising
cent=[0;0];%initialising
velx1=0;%initialising
vely1=0;%initialising
start(vid)%initialising
while(vid.FramesAcquired<=framesneeded)
RGB=getsnapshot(vid); %getting snapshot
R=RGB(:,:,1); %taking R
R=fliplr®;
G=RGB(:,:,2);
G=fliplr(G);
B=RGB(:,:,3);
B=fliplr(B);
RGB=cat(3,R,G,B);
R=single®-(single(B)/2+single(G)/2); %Extracting R
R=uint8®;
bw=R>40; %taking objects grater than the value
bw=medfilt2(bw,[3 3]); % filtering
bw=bwareaopen(bw,20); %removing objects less than 2o pixels area
bw=bwconncomp(bw,8);
stats=regionprops(bw,'CENTROID','Area','Perimeter'); %stats of the certain region properties
imshow(RGB)
hold on
if length(stats)>1
if ((([stats.Perimeter].^2) / (4 * pi * [stats.Area]))>0.8) cent=stats(1).Centroid; % logic here is to identify a circular object
end velx=(cent1-cent(1))^2; %calculating velocity
vely=(cent2-cent(2))^2;%calculating velocity
cent1=cent(1);
cent2=cent(2);
vel=(((velx)+(vely))^0.5)/20; %calculating velocity (20 is the default fps)
ax=abs((velx-velx1)/20);%calculating acceleration
ay=abs((vely-vely1)/20);%calculating acceleration=
velx1=velx;
vely1=vely;
a=((ax^2)+(ay^2))^0.5;%calculating acceleration
x=(A*(cent)')+(D*a);%kalman equations
z=(C*(x))+Ez;%kalman equations
P=(A*P*A')+Ex;%kalman equations
K=(P*C'*(inv((C*P*C')+Ez)));%kalman equations
x=x+(K*((z)-(C*x)));%kalman equations
P=(eye(2)-K*C)*P;%kalman equations
x=x';
plot(x(1),x(2),'r+','MarkerfaceColor','r','markerSize',20);% plotting
plot(cent(1),cent(2),'g+','MarkerfaceColor','g','markerSize',20);
t=text(cent(1)+15,cent(2), strcat('X:', num2str(round(cent(1))), 'Y:', num2str(round(cent(2)))));
hold off
flushdata(vid);
end
end

Answers (1)

Tracking red objects must be a staple in the image processing courses these days since we see this asked so often.

1 Comment

That is right .....I want to know about the kalman filer as to where my logic is wrong

This question is closed.

Asked:

Sri
on 4 Sep 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!