calculating the distance
2 views (last 30 days)
Show older comments
I have a code for tracking a red ball taken from internet real time process,now if i move the redball is it possible to calculate the distance and speed,i have connected a eye robot so i want to calculate the distance of redball moved in order to make the robot move for tha particular distance and with speed
clc;
vid=videoinput('winvideo',1, 'YUY2_640X480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval =20;
start(vid)
first=getsnapshot(vid);
num=2;
centroid(1,1)=0;
centroid(1,2)=0;
thetaa=0;
diffx=0;
diffy=0;
dx=0;
dy=0;
are1(1)=0;
dist(1)=0;
while(vid.FramesAcquired<=50)
data = getsnapshot(vid);
proframe=abs(first-data);
first=data;
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.15);
diff_im = bwareaopen(diff_im,5000);
bw = bwlabel(diff_im, 8);
stats = regionprops(bw, 'BoundingBox', 'Centroid','Area');
figure,imshow(data)
hold on
bc=[0 0];
ar=0;
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
ar = stats(object).Area;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
centroid(num,1)=bc(1);
centroid(num,2)=bc(2);
are1(num)=ar;
if (num-1)~=1
diffx(num) = (centroid(num,1)-centroid((num-1),1));
dx(num)=diffx(num);
diffy(num) = centroid(num,2)-centroid((num-1),2);
dy(num)=diffy(num);
dist(num)=sqrt(((dx(num))^2)+((dy(num))^2));
areaf(num)= (are1(num)-are1(num-1));
% serport=RoombaInit(4);
%
% if areaf(num)>0
% travelDist(serport, 0.3,0.4);
% elseif areaf(num)<0
% travelDist(serport, 0.3,-0.5);
% end
travelDist(serport, 0.3,0.4);
i have specifies a constant for movement ,now i want variables of speed and distance,,plz help
0 Comments
Accepted Answer
Pedro
on 11 Feb 2014
distance=sum(sqrt(diffx(i)^2+diffy(i)^2)); % i=1:num
velocity=sqrt((diffx(i)/time)^2+(diffy(i)/time)^2); % time betwen num and num-1
I hope this helps, Sheers!
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!