Why does writing Images/Video to memory need so much RAM?

6 次查看(过去 30 天)
I wanted to save a videostream from a webcam which has 30fps to a file. The main goal is to save it with those 30fps and not less. Unfortunatelly it is only possible to write about 21 fps to a file with "set(videoObj, 'LoggingMode', 'disk')" because of Matlab/computation speed etc. That is why I wrote a code to gather frames first to memory in realtime, and later write the content of the memory to a file. This all works very well. However I can only write about 1900 frames to memory because of a RAM problem. After writing about 1900 frames to memory Matlab needs about 11GB of RAM, and then I get the error message 'Unable to allocate memory for an incoming image frame due to insufficient free physical memory'. Of course I get this message because all free physical memory was used and there is no more free memory (My PC has 16 GB in total). But why does Matlab need so much memory (11GB) for a .avi video with 1900 frames or 63 seconds which has only the size of about 291MB? My goal was to write a video with about 15000-25000 frames @30fps to memory in realtime and then write it to a file, to get a real 30fps video saved. It seems that with this solution it is not possible. How can I avoid that Matlab has to use so much memory, or is there a solution to avoid this problem? Thanks
% ------------------------------------------------------- Write Video to memory
videoObj = videoinput('winvideo',3,'RGB24_1920x1080');
videoObj.FramesPerTrigger = 5000;
set(videoObj, 'LoggingMode', 'memory');
start(videoObj);
wait(videoObj,((videoObj.FramesPerTrigger/30)+10))
frames_Available = videoObj.framesAvailable;
disp(frames_Available);
videoWriter = VideoWriter('D:\MyProject\TestVideo.avi');
open(videoWriter);
imageData = getdata(videoObj,frames_Available);
for frameLoop = 1:1:frames_Available
myFrames =(imageData(:,:,:,frameLoop));
s1 = ['Frames: ' (num2str(uint16(frameLoop),'%4d\n')) '/' (num2str(uint16(frames_Available),'%4d\n'))];
myFrames = insertText(myFrames,[18 255-120],s1, 'FontSize',20,'BoxColor','green','BoxOpacity',0.5,'TextColor','black');
writeVideo(videoWriter,myFrames)
end
close(videoWriter);
clear all;
%Error event occurred at 11:53:03 for video input object: RGB24_1920x1080-winvideo-3.
%Unable to allocate memory for an incoming image frame due to insufficient free physical memory.
%Unable to allocate memory for an incoming image frame due to insufficient free physical memory.
%Error in imaqdevice/wait (line 115)
% wait(udd, waittime);
%Error in Video2Memory (line 7)
%wait(videoObj,((videoObj.FramesPerTrigger/30)+10))
  3 个评论
enzo kleedorfer
enzo kleedorfer 2017-2-5
Thanks. Is there a way to write 15000-25000 frames @30fps to memory instead of only about 1900 frames?

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by