How can I plot line iteratively on an existing movie frame?

1 view (last 30 days)
Hi,
I want to visualize the trajectory of a moving ball in a movie. I obtain the centroid of the ball in each frame. I want to show a line path on the movie while the movie continues playing. Please suggest me a way to perform this?
My Code:
AviFrames; % This creates the movie as a struct in the workspace.
numF = evalin('base','numberOfFrames'); % Getting the number of frames in the movie
film = evalin('base','mov'); % Assigning the movie to "film" variable
initial_xy = [57.7411 35.5395]; % intitial cooridinates of the ball
p1b = [initial_xy(1,1),initial_xy(1,2)];
xs = zeros(numF, 2); % x coorinates of ball in the consecutive frames
ys = zeros(numF, 2); % y coorinates of ball in the consecutive frames
for i=1:numF
image = Struct2Image(i,film); % Converting struct to image (ith frame)
imshow(image);
[cx, cy, r] = CemberPoz(image); % Getting regionprops of the ball in the current image
cpos = [cx cy];
p2b = [cpos(1,1),cpos(1,2)];
pb = [p1b p2b];
bgx(i,:) = pb(1:2:3);
bgy(i,:) = pb(2:2:4);
p1b = p2b;
line([bgx(i,1) bgx(i,2)],[bgy(i,1) bgy(i,2)],'color','b','LineWidth',2);
hold on
axis([0 320 0 240])
hold off
end
Here is one screen capture:
Thanks in advance and kind regards, Tugrul Tasci

Answers (2)

Image Analyst
Image Analyst on 26 Aug 2012
Your screenshot didn't come through. And you need to not take hold off or else your first line will blow away all others.
  1 Comment
Walter Roberson
Walter Roberson on 26 Aug 2012
"hold" has no effect on line().
The poster is continually drawing new images and drawing new lines, so it is fine (other than from a performance point of view) to have the old contents continually blown away.
It is, however, not at all clear to me why the axis() command is there: in that context, it is going to create a new axis rather than resize the existing axis.

Sign in to comment.


Tugrul TASCI
Tugrul TASCI on 26 Aug 2012
Thanks for your answers. I changed my code as below and now I succeed what I want.
AviFrames;
numF = evalin('base','numberOfFrames');
film = evalin('base','mov');
initial_xy = [57.7411 35.5395];
p1b = [initial_xy(1,1),initial_xy(1,2)];
fig = figure('Position',[600 400 640 480]);
set(fig,'DoubleBuffer','on');
set(gca,'xdir','normal');
set(gca,'ydir','reverse');
axis([0 320 0 240]);
axis on
bgx = [];
bgy = [];
for i=1:numF
imge = Struct2Image(i,film);
imshow(imge);
hold on
[cx, cy, r] = CemberPoz(imge);
cpos = [cx cy];
p2b = [cpos(1,1),cpos(1,2)];
pb = [p1b p2b];
bgx = [bgx pb(1:2:3)];
bgy = [bgy pb(2:2:4)];
p1b = p2b;
plot(bgx,bgy);
drawnow;
hold off
end
------------------------------------------------------------------
(I am not sure if this URL works) You can preview the first 10 seconds of the output movie.

Categories

Find more on Animation 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!