Finding displacement/distance and velocity afterwards
2 views (last 30 days)
Show older comments
I am doing a fall detection program for my project. Currently, I have tracked a moving object in my video and along with the tracking rectangle, I am able to display the centroid of that box as the object moves.
How do I go about finding the displacement of the centroid of the current frame and the previous frame? After that, how do I find velocity and display a graph showing the velocity of the object from the moment it appears in the video until the end of video? I understand to find velocity is Displacement/Frame Rate, but I am having trouble how to write the codes.
This are my codes so far:
videoObject = VideoReader(movieFullFileName); numberOfFrames = videoObject.NumberOfFrames; vidHeight = videoObject.Height; vidWidth = videoObject.Width; numberOfFramesWritten = 0; figure; set(gcf, 'units','normalized','outerposition',[0 0 1 1]); centry=[];
for frame = 1 : 100 : numberOfFrames
intFrame = read(videoObject, 5);
thisFrame = read(videoObject, frame);
%Display it
subplot(221);
imshow(thisFrame);
caption = sprintf('Original video\nFrame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
%background substraction
i_sub=intFrame-thisFrame;
subplot(222);
imshow(i_sub);
title('Template Background Subtracted', 'FontSize', fontSize);
igray=rgb2gray(i_sub);
level=graythresh(i_sub);
binarylevel=im2bw(i_sub,level);
subplot(223);
imshow(binarylevel);
title('After binary', 'FontSize', fontSize);
BinaryImage = bwareaopen(BinaryImage,10000);
subplot(224);
imshow(BinaryImage)
title('Centroid position of object', 'FontSize', fontSize);
hold on
% Label the image.
labeledImage = bwconncomp(BinaryImage,8);
% Call bounding box and centroid function
measurements = regionprops(labeledImage,'BoundingBox','Centroid');
totalNumberOfBlobs = length(measurements);
for blobNumber = 1:totalNumberOfBlobs
bb = measurements(blobNumber).BoundingBox;
bco = measurements(blobNumber).Centroid;
x=bco(1);
y=bco(2);
rectangle('Position',bb,'EdgeColor','y','LineWidth',1)
plot(x,y,'-m+');
centry=[centry bco(2)];
myCaption = sprintf('(x=%.1f, y=%.1f)', x, y);
text(x+30,y, myCaption,'FontName','Arial','FontWeight','normal','FontSize',8,'Color','red');
set(gca,'xdir','normal')
set(gca,'ydir','reverse')
axis on;
end
drawnow;
hold off
end
figure;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
subplot(121);
plot(centry,'m', 'LineWidth', 2);
title('Motion Tracking', 'FontSize',fontSize);
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!