plotting x & y with time

Im trying to analyse 3 black dots which are marked in a white background. this is given a slight push so that it will move freely and is recorded so that i can take out each frame and detect the motion of the 3 dots. I am stuck at getting the displacement. I have managed to get the x and y co-ordinates of the centroids of each dot. I need to plot this with time. the motion of these 3 dots with time. could someone help please
i have attached the code and one image
please let me know if there is an alternate suggestion for this as well. would be very helpful
clf
clear all
close all
clc
%video=VideoReader('final.mp4');
%numberOfFrames = video.NumberOfFrames;
tf=0.3; %normalizedThresholdValue
sr=1000; % sampling rate
dt=1/sr;
cal=26.5; %Calibration distance
Folder='E:\Capstone\image analysis\VideoReader\additional\frames';
% selecting images to be analysed
[fname, fpath]=uigetfile('*.jpg','SELECT MULTIPLE FILES USING ctrl OR shift KEYS','MultiSelect', 'on');
n=length(fname);
t=(0:n-1)*dt;
for c=1:n
filename=[fpath,fname{c}];
originalImage=imread(filename);
%grayscaling image
data = rgb2gray(originalImage);
%Display grayscale image
figure(1)
imshow(data);
title('Grayscaled Image');
grid on;
drawnow;
% Threshold the image to get a binary image (only 0's and 1's)
%thresholdValue = normalizedThresholdValue * max(max(originalImage)); % Gray Levels.
tr=tf*max(max(data)); % Brightness threshold
binaryImage = im2bw(data, tf);
% Display the binary image.
figure(2)
imshow(binaryImage)
%colormap(gray)
z=im2double(binaryImage);
title('SELECT ANALYSIS REGION BY CLICKING NORTH-WEST AND SOUTH-EAST CORNERS WITH MOUSE')
if c==1
[x y]=ginput(2);
%pause
end
z=floor(z);
%z=z(floor(y(1)):floor(y(2)),floor(x(1)):floor(x(2)));
z=1-z; % reverse image black dots on white BG
figure(3)
surf(z)
shading interp
colormap(jet)
view(90,0);
tr=tf*max(z(:));
tp=tr+zeros(size(z));
figure(4)
surf(tp)
Z=z-tr;
zs=sign(Z);
zs(zs<0)=0;
figure(5)
surf(zs)
hold on
shading interp
colormap(gray)
view(2)
% i= y co-ordinates %j= x co-ordinates
[i,j]=find(zs); %returns vector containing linear indices of each non zero element
figure(6)
plot(j,i,'o')
hold off
is=sort(j);% x coordinates of which the dot region is detected
js=sort(i);% y coordinates of which the dot region is detected
%%%%%Find centroid of points
%vi=sign(is-2)+1;
%vi=find(vi);
%vj=sign(js-2)+1;
%vj=find(vj);
%centroid of dot 1
p1x=mean(is(1:end));
p1y=mean(js(1:82));
centroid1=[p1x p1y];
%centroid of dot 2
p2x=mean(is(1:end));
p2y=mean(js(83:162));
centroid2=[p2x p2y];
%centroid of dot 3
p3x=mean(is(1:end));
p3y=mean(js(163:end));
centroid3=[p3x p3y];
% x direction
dpx(c)=((p3x-p2x)-(p2x-p1x))/2;
x_sens=cal/(p3x-p1x);
dx(c)=x_sens*dpx(c);
x_cs=num2str(c);
x_dps=num2str(dpx(c));
dxs=num2str(dx(c));
title(strcat(x_cs,' .... ', x_dps));
% y direction
dpy(c)=((p3y-p2y)-(p2y-p1y))/2;
y_sens=cal/(p3y-p1y);
dy(c)=y_sens*dpy(c);
y_cs=num2str(c);
y_dps=num2str(dpy(c));
dys=num2str(dy(c));
title(strcat(y_cs,' .... ', y_dps));
figure(7)
plot3(t(1:c),p1y(1:c),p1x(1:c),'o');
hold on
plot3(t(1:c),p2y(1:c),p2x(1:c),'b');
hold on
plot3(t(1:c),p3y(1:c),p3x(1:c),'r');
hold off
xlim([0 max(t)]);
ylim([-3 3])
grid on
xlabel('Elapsed time [s]')
ylabel('Displacement y [mm]')
zlabel('Displacement x [mm]')
title(strcat(y_cs,' .... ', dxs));
%pause(0.01)
%,dx(1:c)
%return
end
000002.jpg

2 Comments

KSSV
KSSV on 16 May 2019
YOu have a video and you want to get the position of those black dots?
@KSSV
it is a video of the dots moving. there is around 110 frames of those dots in different positions. i want to detect the centroid of those 3 dots intially, and track the motion for all 3 dots for all those frames (the duration of the video). and then i need that displacement in x and y direction plotted in a graph vs time
help me if possible

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!