Plot sequence of images on top of a trajectory
3 views (last 30 days)
Show older comments
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?
0 Comments
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])
colormap(gray(256))
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;
drawnow();
pause(0.1) % wait
end
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.
2 Comments
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;
end
% 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')
f2 = fit(xyt(:,2),xy0t(:,2),'poly1')
% these are the ranges used for hi.XData and hi.YData
xdrange = [1 size(G,2)]*f1.p1 + f1.p2
ydrange = [1 size(G,1)]*f2.p1 + f2.p2
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)
ydrange = [1 size(G,1)]*f2(1) + f2(2)
More Answers (0)
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!