Using VideoWriter for imagesc
显示 更早的评论
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 个评论
Varshini Guddanti
2016-7-7
编辑:Walter Roberson
2016-7-7
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)
回答(1 个)
Walter Roberson
2016-7-7
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 个评论
Daniele Bernardo Panaro
2019-9-19
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
2019-9-19
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?
类别
在 帮助中心 和 File Exchange 中查找有关 Audio and Video Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!