calculating velocity from centroids

8 views (last 30 days)
C.G.
C.G. on 17 Mar 2020
Commented: Image Analyst on 17 Mar 2020
I have a code which calculates the position of centroids of multiple particles through each of the video frames. I am now trying to calculate the velocity of all the particles through the video using centroid displacement. I know we can use the difference in position between frames and potentially the frame rate, but writing the code is new to me,
Does anybody know an easy way to calculate this?
%% Get video into MATLAB
%Input video using videoreader and name it 'obj'
obj = VideoReader('highamballvideo.mov')
%% Read the frames in the video
%Tell it to read the frames in the video where 20 is start and 34 finish
frames_no = [20:34];
vidFrames = read(obj,[20 34]);
%Tell it to get the number of individual frames in the video
numFrames = size(vidFrames,4);
%Get individual frames
%Colormap is a table of colors used for index color videos
%c.data is an image sequence matrix
%How many times you repeat a function in a loop is defined by 'k'. k = the
%range of values the for loop will run through before ending.
for k = 1: numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
end
%Watch the video
figure(1), movie(mov, 1, obj.FrameRate), title('Original movie');
%Show all frames in a montaged figure
figure(2), montage(vidFrames(:,:,:,1:15)),title('Montage of frames 20 to 34');
%% Track particles in video
%Define the frames between which particles are going to be tracked
start_frame = 10;
end_frame = 100;
%Define the radii of the circles to get MATLAB to search for
min_radius = 4;
max_radius = 20;
quality = .9;
for loop = start_frame:end_frame;
clf
frame = rgb2gray(read(obj,loop));
%find circles function
centres=imfindcircles(frame,[min_radius,max_radius],'Sensitivity',quality,'Method','TwoStage')
%display image with scaled colors
imagesc(frame);
hold on
%here we get two vectors of the centroids and plot them througgh each
%frame
xs = centres(:,1);
ys = centres(:,2);
scatter(xs,ys,'r')
%Use drawnow to display the changes on the screen after each iteration through the loop.
drawnow
end
  2 Comments
C.G.
C.G. on 17 Mar 2020
my video is too large of a file to upload to anywhere unfortunatley

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 17 Mar 2020
Writing a tracking program is very difficult and I'm not going to do it for you. It could take weeks. I suggest you look for one in the File Exchange or a commercial product. Writing one is not for someone who is new to programming, sorry.
One problem is if some particles leave the field of view and others enter the field of view. How do you handle that? You have to identify which are the same and which are different.
Also, it's easiest if the particles don't move much from frame to frame so that you can identify which are which by seeing, for every particle in the (n+1)st frame, is closest to it's location in the nth frame.
Another problem is what to do if two particles pass very close to one another (overlap or almost overlap) -- you might not know which is which. To determine that you'd perhaps assume they were traveling in a straight line and see which IDs would allow them to continue in a straight line instead of colliding and having tracks that look like a V.
But what if particles could collide, like billiard balls. Now you have to allow for particles to bounce off one another.
One major problem with your code is that your IDs or labels are not consistent. The particle that shows up as xs(1), ys(1) in one frame might not be the same particle that shows up in xs(1), ys(1) in the next frame(s). It may have moved so that that particle is now at xs(23), ys(23) in the next frame. That is why you need to ID/label them.
So you can see it can get very, very involved.
  6 Comments

Sign in to comment.


Adam Danz
Adam Danz on 17 Mar 2020
"I am now trying to calculate the velocity of all the particles through the video using centroid displacement. I know we can use the difference in position between frames and potentially the frame rate, but writing the code is new to me "
To compute the distance between particles, use pdist, pdist2, hypot, or compute them directly using the distance formula (if you need help, let us know).
Once you have the distance, velocity is merely Distance/Time. The time depends on your frame rate.
framerate = 60; % 60 Hz or 60 fps
velocity = distance / (NumberOfFrames/framerate);
So, if you're computing the velocity between two frames, velocity = distance/(1/framerate).
  3 Comments
C.G.
C.G. on 17 Mar 2020
That is understandable. Thank you for your response.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!