Clear Filters
Clear Filters

Plot sequence of images on top of a trajectory

1 view (last 30 days)
I have a a 3D array G that has frames of a ball moving. I also have a variable named traj, that gives me the coordinates of the trajectory of the ball (traj.x and traj.y ). The arrays are attached.
I need to plot the frames on top of the trajectory. As if the trajectory is the background of the frames.
If you use sliceViewer(G) you will see the movement. I just want the trajectory to be the background of this movement.
I don't know even how to start.
Any ideas?

Accepted Answer

DGM on 3 Apr 2024
Edited: DGM on 3 Apr 2024
Here's a start. Note that the alignment is just guesswork on my part. Also, I don't know why there's a frame offset between the image and the xy data.
load G.mat
load traj.mat
alph = 0.5; % scalar FG opacity
nframes = 100;
frameoffset = 2; % the image appears to be 2 frames ahead of the plot data?
% set up the figure
hp = plot(traj.x,traj.y,'-o'); hold on
hi = image(uint8(G(:,:,1))); hold on % need to reassert hold
hi.XData = [-92 85.6]; % these are estimates
hi.YData = [81.1 -87];
xlim([-50 50])
ylim([-50 50])
set(gca,'ydir','normal','dataaspectratio',[1 1 1])
grid on
% run the loop
for f = 1+frameoffset:nframes
% update the plot data
hp.XData = traj.x(1:f);
hp.YData = traj.y(1:f);
% update the image data and reassert
hi.CData = uint8(G(:,:,f-frameoffset));
hi.AlphaData = (G(:,:,f-frameoffset)<128)*alph;
pause(0.1) % wait
If you don't have some sort of spatial calibration data, I can show you how I got the estimates that I used. It'd probably be best if you didn't have to resort to doing what I did.
L on 4 Apr 2024
Hi @DGM, thanks so much.
I don't have any clibration data. Can you walk me through the estimates?
DGM on 4 Apr 2024
Edited: DGM on 4 Apr 2024
This is how I did it:
load G.mat
load traj.mat
nframes = 100;
% get centroid positions for all frames
xy = zeros(nframes,2);
for f = 1:nframes
S = regionprops(G(:,:,f)<128,'centroid');
xy(f,:) = S.Centroid;
% select only the segments where the two sequences correspond
xy0 = [traj.x; traj.y].';
xy0t = xy0(3:end,:); % trimmed traj data
xyt = xy(1:end-2,:); % trimmed centroid data
% get the fit parameters
f1 = fit(xyt(:,1),xy0t(:,1),'poly1')
f1 =
Linear model Poly1: f1(x) = p1*x + p2 Coefficients (with 95% confidence bounds): p1 = 3.626 (3.612, 3.641) p2 = -95.73 (-96.13, -95.32)
f2 = fit(xyt(:,2),xy0t(:,2),'poly1')
f2 =
Linear model Poly1: f2(x) = p1*x + p2 Coefficients (with 95% confidence bounds): p1 = -3.431 (-3.444, -3.418) p2 = 84.54 (84.19, 84.88)
% these are the ranges used for hi.XData and hi.YData
xdrange = [1 size(G,2)]*f1.p1 + f1.p2
xdrange = 1x2
-92.0992 85.5811
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ydrange = [1 size(G,1)]*f2.p1 + f2.p2
ydrange = 1x2
81.1059 -87.0186
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If you don't have CFT, you can just use polyfit() instead of fit():
f1 = polyfit(xyt(:,1),xy0t(:,1),1);
f2 = polyfit(xyt(:,2),xy0t(:,2),1);
xdrange = [1 size(G,2)]*f1(1) + f1(2)
xdrange = 1x2
-92.0992 85.5811
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ydrange = [1 size(G,1)]*f2(1) + f2(2)
ydrange = 1x2
81.1059 -87.0186
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

More Answers (0)


Find more on 2-D and 3-D Plots in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!