How to use pdist for distance travelled and tracking, calculated on 2 pairs of centroids for 2 images ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have to track circular balls falling and I have video of them. I took out 2 images from the video and I am trying to track the balls and their distance travelled. I did centroid tracking for both of the images and saved their centroids.
clear
clf
clc
%% video reading
V = VideoReader('V.mp4');
V.NumFrames
allFrames = read(V);
whos allFrames;
%% first frame
V1 = read(V,[6143]);
%imsave(V1);
%imshow(V1);
%% second frame
V2 = read(V,[6144]);
%imsave(V2);
%imshow(V2);
%% for first frame V1
%%%%% PARAMETERS %%%%%%%
th=0.2;
r1=20;
r2=10;
p1=2800;
%%%%%%%%%%%%%%%%%%%%%%%%%
%% convert to Binary
tha=0.2;
BWa=im2bw(V1,tha);
BWa=~BWa;
%imshow(BWa);
%% morphology operations (Erode)
r1a=5;
sea1=strel('disk',r1a,0);
im2a=imerode(BWa,sea1);
%imshow(im2a);
%% morphology operations (Dilate)
r2a=5;
sea2=strel('disk',r2a,0);
im3a=imdilate(im2a,sea2);
%imshow(im3a);
%% BWareaopen removing less than p1 pixels
p1a=100;
im4a=bwareaopen(im3a,p1a);
%imshow(im4a);
%% subtracting images
%im5a=im2bw(im3a-im4a);
%imshow(im5a);
%% clear border
im6a=imclearborder(im4a);
%imshow(im6a);
%% centroid tracking
c1=regionprops(im6a,'Centroid');
centroids1 = cat(1,c1.Centroid);
%imshow(im6a);
hold on
plot(centroids1(:,1),centroids1(:,2),'yx')
hold off
%% for second frame V2 convert to Binary
thb=0.2;
BWb=im2bw(V2,thb);
BWb=~BWb;
%imshow(BWb);
%% morbphology operations (Erode)
r1b=5;
se1b=strel('disk',r1b,0);
im2b=imerode(BWb,se1b);
%imshow(im2b);
%% morphology operations (Dilate)
r2b=5;
se2b=strel('disk',r2b,0);
im3b=imdilate(im2b,se2b);
%imshow(im3b);
%% BWareaopen removing less than p1 pixels
p1b=100;
im4b=bwareaopen(im3b,p1b);
%imshow(im4b);
%% subtracting images
%im5b=im2bw(im3b-im4b);
%imshow(im5b);
%% clear border
im6b=imclearborder(im4b);
%imshow(im6b);
%% centroid tracking
c2=regionprops(im6b,'Centroid');
centroids2 = cat(1,c2.Centroid);
%imshow(im6b);
hold on
plot(centroids2(:,1),centroids2(:,2),'yx')
hold off
%% pdist for tracking and distance measurement
How can I use pdist to track and measure distance travelled on these balls centroids and images?
Also, how can I apply the whole code from 2 frames to the full length of the video?
I am new to MATLAB. Any help is appreciated!
0 comentarios
Respuestas (1)
Harsh Sanghai
el 23 de Mzo. de 2023
Hi,
To use "pdist" to track the balls and measure their distance traveled, you can calculate the pairwise Euclidean distance between the centroids in both frames using "pdist" and then match the closest centroids between the frames. To match the centroids, you can use the "matchpairs" function, which finds the indices of the closest pairs of points.
% Read the video
v = VideoReader('V.mp4');
% Initialize variables
centroids = cell(1, v.NumFrames);
distances = zeros(1, v.NumFrames-1);
% Loop over all frames
for i = 1:v.NumFrames
% Read the current frame
frame = read(v, i);
% Convert to binary
bw = im2bw(frame, 0.2);
bw = ~bw;
% Erode
se1 = strel('disk', 5, 0);
bw = imerode(bw, se1);
% Dilate
se2 = strel('disk', 5, 0);
bw = imdilate(bw, se2);
% Remove small objects
bw = bwareaopen(bw, 100);
% Clear border
bw = imclearborder(bw);
% Find centroids
c = regionprops(bw, 'Centroid');
centroids{i} = cat(1, c.Centroid);
end
% Loop over all frames except the last one
for i = 1:v.NumFrames-1
% Match centroids between frames
pairs = matchpairs(centroids{i}, centroids{i+1});
matched1 = centroids{i}(pairs(:,1),:);
matched2 = centroids{i+1}(pairs(:,2),:);
% Calculate pairwise distances
d = pdist2(matched1, matched2);
% Calculate total distance traveled
distances(i) = sum(d(:));
end
To apply this code to the full length of the video, you can simply remove the frame indices from the "read" function and let it read all frames:
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!