make a movie with axes from images matlab

303 views Asked by At

i have a folder with jpg images and i want to convert them into a movie but i also want to have axes on the movie (images dont have axes).my images are grayscale.i ve done something but it is incorrect. the command movie(F,1); shows the movie but the axes are not fix on the movie .also the avi that i save shows only the movie (motion of the images) without the axes. this is my code:

vidObj = VideoWriter('sample.avi');
open(vidObj);
for num_frame=1:40
     %find filename of the image
     filename = sprintf('%s_%d.jpg','new_image/sample_image',num_frame);
     cmap = colormap(gray);
     res = grs2rgb(filename,cmap);%convert image from grayscale to rgb
     F(num_frame)=im2frame(res);
 end
 imshow('new_image/sample_image_1.jpg');%show the first image
 set(gca,'FontSize',14);
 xlabel('Lateral distance [mm]');
 ylabel('Axial distance [mm]');
 xlim([-50 50]);
 ylim([20 105]);
 axis([-50 50 20 105]);
 axis ('on');
 movie(F,1);
 writeVideo(vidObj,F);
 close(vidObj);

what is the mistake??

1

There are 1 answers

0
hbaderts On

The problem is that the axis setup you are doing is only used in MATLAB and is not saved with the video. To save the axis setup, you can use getframe to create a new frame of the current plot. I suggest the following:

vidObj = VideoWriter('sample.avi');
open(vidObj);

for num_frame=40:-1:1
    % load image
    filename = sprintf('%s_%d.jpg','new_image/sample_image',num_frame);
    cmap = colormap(gray);
    res = grs2rgb(filename,cmap);

    % create plot
    imshow(res);
    set(gca,'FontSize',14);
    xlabel('Lateral distance [mm]');
    ylabel('Axial distance [mm]');
    xlim([-50 50]);
    ylim([20 105]);
    axis([-50 50 20 105]);
    axis ('on');

    % save current plot as movie frame
    F(num_frame) = getframe(gcf);
end

writeVideo(vidObj,F);
close(vidObj);

Note the for num_frame=40:-1:1: This is to preallocate F at the first iteration, which is better for speed (MATLAB doesn't need to extend the variable at each iteration)