Effacer les filtres
Effacer les filtres

Using VideoWriter for imagesc

82 vues (au cours des 30 derniers jours)
Frederik Faye
Frederik Faye le 2 Fév 2016
Commenté : Walter Roberson le 19 Sep 2019
I would like to make a movie based on imagesc(M), where M is a matrix that changes with t in a loop. Of course, any other solution that outputs a video file with the frames of what is shown by imagesc would also be fine. Here's what I have:
%looping over t
h = imagesc(M);
axis off
refreshdata(h,'caller')
drawnow
F(t) = getframe(gcf);
%outside loop
v = VideoWriter('newfile.avi','Archival');
v.FrameRate = 60;
open(v)
writeVideo(v,F)
close(v)
However, I get the warning:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter/close (line 267)
In VideoWriter/delete (line 202)
In Untitled (line 44)
Error using VideoWriter/writeVideo (line 369)
The 'cdata' field of FRAME must not be empty
Error in Untitled (line 47)
writeVideo(v,F)
Thanks!
  1 commentaire
Varshini Guddanti
Varshini Guddanti le 7 Juil 2016
Modifié(e) : Walter Roberson le 7 Juil 2016
I tried to change the order of your code. Please try this:
% outside loop
v = VideoWriter('newfile.avi','Archival');
v.FrameRate = 60;
open(v)
.
% inside loop
.
%looping over t
h = imagesc(M);
axis off
refreshdata(h,'caller')
drawnow
F(t) = getframe(gcf);
writeVideo(v,F)
% close loop
.
% outside loop
close(v)

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 7 Juil 2016
Is it possible that you store one final frame after the end of the t loop, and that final frame is empty? Or alternately, that you store an empty initial frame?
For debugging, add this before the VideoWriter:
all_valid = true;
flen = length(F);
for K = 1 : flen
if isempty(F(K).cdata)
all_valid = false;
fprint('Empty frame occurred at frame #%d of %d\n', K, flen);
end
end
if ~all_valid
error('Did not write movie because of empty frames')
end
  2 commentaires
Daniele Bernardo Panaro
Daniele Bernardo Panaro le 19 Sep 2019
Hello I have a similar problem. Using your debugging code (adapting it to my code) it gives me the message
'Empty frame occurred at frame #1 of 1139
Error using UP_MGL_1_2 (line 239)
Did not write movie because of empty frames'
How do I fix the empty frame?
Thank you
Walter Roberson
Walter Roberson le 19 Sep 2019
I notice that only the first frame is empty, not the others. That hints that you might have an off-by-one error in the logic of how you store the frames. Could you show us the code you use to initialize your frame array, and how you update it?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by