Plot trajectory along with tracer colors

34 vues (au cours des 30 derniers jours)
Alakesh Upadhyaya
Alakesh Upadhyaya le 8 Mar 2024
I am dealing with a particle moving in a 2D space, and I've successfully recorded its position coordinates. Now, I aim to visualize the particle's trajectory overlaid onto video frames. The color of the trajectory should evolve over time, with only the current positions reflecting the changing colors. However, I'm facing an issue where the tracer line disappears as time progresses, despite the changing colors of the current particle positions in the trajectory. Here is my code.
data = load('data.dat');
frames=200;
colormapJet = colormap(jet(size(data, 1)));
for i = 1:frames
img = imread(sprintf('%04d.jpg', i));
imshow(img, []);
hold on
x = data(i, 1);
y = data(i, 2);
if i > 1
x_prev = data(i - 1, 1);
y_prev = data(i - 1, 2);
plot([x_prev, x], [y_prev, y], '-o','color', colormapJet(i, :), 'LineWidth', 4.0);
end
drawnow;
hold off
end
data= my xy co-ordinates
img =frames
I want somethin like this as shown in image below. I want to make a video along with the frames and trajectory lines which leads to something like this.
As soon as I comment the below part, it traces trajectory the way i want, but I can't overlay the images alone with the trajectory.
img = imread(sprintf('%04d.jpg', i));
imshow(img, []);
Any help would be appreciated. Thanks
  1 commentaire
Alakesh Upadhyaya
Alakesh Upadhyaya le 8 Mar 2024
I want the video to trace trajectory as shown in the above image. There must be something I am missing here,

Connectez-vous pour commenter.

Réponse acceptée

Aishwarya
Aishwarya le 18 Mar 2024
Hi Alakesh,
Based on the information provided, it seems you're aiming to visualize a particle's trajectory overlaid on video frames but are encountering issues with the current implementation.
After reviewing the code, here are some insights that can help achieve the desired result:
  • The problem you're facing appears to be related to the use of the “hold off” command at the end of the for loop, which clears the current axes after plotting each frame, thus erasing the trajectory line. To resolve this, consider moving the “hold off” command outside the for loop.
  • When you comment out the “imshow” part, the trajectory remains because it's no longer being overwritten by the next frame's image. This explains why you could see the trajectory across frames.
  • To achieve your goal of overlaying the trajectory on video frames with evolving colours, ensure that the trajectory is retained while updating the background image.
Consider the following modified code to achieve the desired output:
data = load('data.dat');
frames = 200;
colormapJet = colormap(jet(size(data, 1)));
% Display the first frame to initialize the plot
hImage = image(images{1});
hold on;
% Loop to update the image and draw the trajectory
for i = 1:frames
% Update image data
set(hImage, 'CData', images{i});
% Plot current position with evolving color
plot(data(i, 1), data(i, 2), 'o', 'color', colormapJet(i, :), 'LineWidth', 4.0);
drawnow;
% No need for 'hold off' as we're updating the image and plotting over it
end
% Turn off hold
hold off;
In this code, the plot is initialized with the first frame and then updated the image’s “Cdata” within the loop to change the background without clearing the trajectory. Inside the loop, it also plots the current position with changing colour, making the trajectory visible throughout the video.
For more information about the functions used, refer to the below MathWorks documentation:
I hope this resolves your query!
  2 commentaires
DGM
DGM le 18 Mar 2024
Modifié(e) : DGM le 18 Mar 2024
Given the marker style, it would be preferable to use a single scatter() object than to create a large number of single-point line() objects.
% frames is both the number of images and the number of datapoints
frames = 200;
% some fake data
th = linspace(0,360,frames);
data = 100*[cosd(th(:)) sind(th(:))] + 128; % a simple circle
inpict = imread('cameraman.tif');
images = repmat({inpict},[frames 1]); % just a bunch of duplicates
% create a color table
CT = jet(frames); % don't need to use colormap() to get a map
% Display the first frame to initialize the plot
hImage = imshow(images{1},'border','tight'); hold on;
hScatter = scatter(data(1,1),data(1,2),20,CT(1,:),'filled');
% Loop to update the image and draw the trajectory
for k = 2:frames
% Update image data
hImage.CData = images{k};
% update the scatter object
hScatter.XData = data(1:k-1,1);
hScatter.YData = data(1:k-1,2);
hScatter.CData = CT(1:k-1,:);
% force redraw
drawnow;
end
% Turn off hold
hold off;
Alakesh Upadhyaya
Alakesh Upadhyaya le 16 Sep 2024
Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by