Calculating speed of moving object
20 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mariana Veloso
le 29 Juin 2021
Réponse apportée : Harikrishnan Balachandran Nair
le 18 Août 2021
Hi,
I've successfully tracked an object, more specifically a fish, that's alone in an aquarium. The next step is to calculate it's speed, through the use of the centroid. My idea was to calculate the distance between the centroids frame by frame. How can I do it? I'm stuck and don't really know how to do it.
Appreciate any help.
Here's the code I used to track the fish:
clear
close all
dataDir = '/Users/marianaveloso/Desktop/MATLAB';
cd(dataDir)
video = VideoReader('P1070143_15.MOV');
get(video);
nFrames = video.NumFrames;
vidHeight = video.Height;
vidWidth = video.Width;
cgFrames=video.FrameRate;
n0= 3900
nf= 12610
Area = read (video, 1);
figure;
imshow(Area, []);
set(gcf, 'Position', get(0,'Screensize'));
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = drawfreehand();
binaryImage = hFH.createMask();
Background = read (video, 1);
h = fspecial('gaussian',7,3);
background_gray=rgb2gray(Background);
background_gray= imadjust (background_gray);
background_gray=imcomplement(background_gray);
background_bw=imbinarize(background_gray,0.36);
mat=zeros(size(background_bw));
n=1;
for i= n0:nf % i=m:s:n
test=read(video,i);
test_gray=rgb2gray(test);
test_gray= imadjust (test_gray);
test_gray=imcomplement(test_gray);
test_bw=imbinarize(test_gray,0.39);
test_dif=test_bw-background_bw;
test_dif(binaryImage<1)=0;
test_dif2=medfilt2(test_dif,[3 3]);
test_dif2(test_dif2<0)=0;
cc = bwconncomp(test_dif2);
stats = regionprops(cc, 'Area','centroid');
idx = find([stats.Area]==max([stats.Area]));
test_dif4= ismember(labelmatrix(cc), idx);
a = regionprops(test_dif4,'centroid','area');
co(n,:)=a(1,1).Centroid;
else
co(n,:)=[0 0];
end
areas=[];
hold on
for i=1:length(a)
plot(co(n,1),co(n,2),'ro');
end
hold off
mat=mat+test_dif4;
n=n+1
end
0 commentaires
Réponses (1)
Harikrishnan Balachandran Nair
le 18 Août 2021
Hi,
From my understanding, you want to find the distance between the centroids of the object ,that you have already tracked, in different frames. This can be done by storing the co-ordinates of centroid of the object that you have tracked, in the current frame ,in a variable, at the end of each iteration. The centroid of the same object in the next frame can be calculated in the next iteration, and using the co-ordinate that was stored in the previous iteration of the loop, you can compute the euclidean distance between both the points. As an example, at the kth iteration, the distance the object has moved can be calculated as :
distCovered(k) = sqrt(sum((centroids(k,:)-centroids_old(k,:)).^2));
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!