I want the video to trace trajectory as shown in the above image. There must be something I am missing here,
Plot trajectory along with tracer colors
조회 수: 34 (최근 30일)
이전 댓글 표시
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
채택된 답변
Aishwarya
2024년 3월 18일
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:
- “image”: https://www.mathworks.com/help/matlab/ref/image.html
- “set”: https://www.mathworks.com/help/matlab/ref/set.html
- “drawnow”: https://www.mathworks.com/help/matlab/ref/drawnow.html
- “hold”: https://www.mathworks.com/help/matlab/ref/hold.html
I hope this resolves your query!
댓글 수: 2
DGM
2024년 3월 18일
편집: DGM
2024년 3월 18일
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;
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!