How can I track and measure the velocity of a moving object in a video?

M
Marshall · Oct 1, 2020 · 2.5K views
Question
The video itself is quite simple. It's a high contrast capture of a droplet moving across the screen. How can I calculate the velocity of the droplet given that I already have the scale (pixels to mm)? Any input/direction would be appreciated.
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 30, 2026

You can easily calculate the velocity multiplying the distance of centroids between previous frame and current frame, the frame rate of the video and the scale of the unit is meter/pixel.

scale = 1/320; % meter/pixel
frameRate = 30; % frame/second
velocity = velociy_pix * frameRate * scale; % pixel/frame * frame/second * meter/pixel

Here is an example code that calculates the velocity of a moving ball (requires Computer Vision System Toolbox):

%%Create a video for detection
writerObj = VideoWriter('movingBall.avi');
open(writerObj);
bgI = zeros(240,320,'uint8');
pos_org = [160 120];
r = 10;
for k = 1: 300
    f = 0.2*(1-exp(-k/200));
    pos = pos_org + 50*[cos(2*pi*f/30*k) sin(2*pi*f/30*k)];
    I = insertShape(bgI, 'FilledCircle', [pos r], 'Color', 'white');
    I = I + uint8(randn(size(I)))*10;
    writeVideo(writerObj,im2frame(I));
end
close(writerObj);

%%Calculate velocity of the ball
videoFileReader = vision.VideoFileReader('movingBall.avi');
S = info(videoFileReader);
frameRate = S.VideoFrameRate; % frame/second
scale = 1/320; % m/pixel
videoPlayer = vision.VideoPlayer('Position',[100 100 600 400]);
oldPoints = [];
while ~isDone(videoFileReader)
    videoFrame = step(videoFileReader);
    G = rgb2gray(videoFrame);
    BW = G > 0.5;
    BW2 = bwareaopen(BW, 30);
    BW3 = imfill(BW2, 'holes');
    stats = regionprops('table',BW3,'Centroid');
    points = table2array(stats);
    if ~isempty(oldPoints)
        % Calculate velocity (pixels/frame)
        vel_pix = sqrt(sum((points-oldPoints).^2,2));
        vel = vel_pix * frameRate * scale; % pixels/frame * frame/seconds * meter/pixels
    else
        vel_pix = 0;
        vel = 0;
    end

      % Visualize the velocity
      videoFrameOut = insertObjectAnnotation(videoFrame, 'circle', ...
          [points 10*ones(size(points,1),1)], ...
          cellstr(num2str(vel,'%2.2f')));
      step(videoPlayer, videoFrameOut);

      oldPoints = points;
  end
  release(videoFileReader);
  release(videoPlayer);

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!