Out of memory. Type "help memory" for your options

조회 수: 8 (최근 30일)
Veronika Mazulina
Veronika Mazulina 2020년 3월 31일
댓글: Walter Roberson 2022년 9월 15일
Hello everyone!
I try to make visualisation of the robots motion and record it in video file. The code looks as follows:
%% Visualisation
for ii=1:length(VisualisationArray)
center_x = VisualisationArray(ii,1:4:RobotsNum*4);
center_y = VisualisationArray(ii,2:4:RobotsNum*4);
orientation = VisualisationArray(ii,3:4:RobotsNum*4);
[x,y] = triangle(center_x, center_y, orientation);
% h.FaceColor = Agent(1).Structure.Position.state(ii);
set(h,'Xdata',x,'Ydata',y);
F(ii) = getframe(gcf);
pause(0.001)
end
%% Write a video
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 10;
% open the video writer
open(writerObj);
% write the frames to the video
for i=1:length(F)
% convert the image to a frame
frame = F(i) ;
writeVideo(writerObj, frame);
end
% close the writer object
close(writerObj);
However, if VisualisationArray is too long I recieve the next error message:
Out of memory. Type "help memory" for your options.
Error in matlab.graphics.internal.getframeWithDecorations (line 26)
u = getFrameImage(c, withDecorations);
Error in alternateGetframe
Error in getframe (line 136)
x = alternateGetframe(parentFig, offsetRect, scaledOffsetRect, includeDecorations);
Error in Animation (line 17)
F(ii) = getframe(gcf);
How to deal with it? Maybe there are another options to record frames video?
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 3월 31일
Which MATLAB version are you using, and which operating system?
Veronika Mazulina
Veronika Mazulina 2020년 3월 31일
R2018b

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 31일
I guess for large visualization, and somehow the matrix F becomes very large. Why not just write each frame at the time it is created. It will completely avoid the creation of the array F.
% % Visualisation
%% Write a video
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 10;
% open the video writer
open(writerObj);
for ii=1:length(VisualisationArray)
center_x = VisualisationArray(ii,1:4:RobotsNum*4);
center_y = VisualisationArray(ii,2:4:RobotsNum*4);
orientation = VisualisationArray(ii,3:4:RobotsNum*4);
[x,y] = triangle(center_x, center_y, orientation);
% h.FaceColor = Agent(1).Structure.Position.state(ii);
set(h,'Xdata',x,'Ydata',y);
% convert the image to a frame
frame = getframe(gcf);
% write the frames to the video
writeVideo(writerObj, frame);
pause(0.001)
end
% close the writer object
close(writerObj);

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 3월 31일
Rather than keeping all the data for all the frames in memory and writing the video all at once why not open the VideoWriter object before the for loop then visualize, capture, and write each frame inside the loop body? Then after the for loop is complete close the VideoWriter object.
See the "Create AVI File from Animation" example on the writeVideo documentation page as a model.
  댓글 수: 2
David Russell
David Russell 2022년 9월 15일
Is there any difference in the final result between calling writeVideo after the loop vs. in the loop? E.g. does each call to writeVideo actually compress the individual frame, or just save it uncompressed to disk so that the whole video can be compressed when the object is closed?
Walter Roberson
Walter Roberson 2022년 9월 15일
That is a good question. Mathworks does not publicly define the situations under which compression is done.
The code inside videoWriter() does not explicitly do any compression. But the code calls internal routines to write one frame at a time, and has an comment about the data pipe buffering 10 frames, and has an internal crosscheck about whether that buffer became full (I think.. the logic could be more clear.)
I would deduce from the code that compression is probably not happening at the time that close is done, that instead the internal workings buffer up to 10 frames at a time and compress as they go.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by