How do i clear memory used by getsnapshot without deleted video object

3 views (last 30 days)
raj6 on 8 Nov 2018
Answered: Arun Jaganathan on 13 Nov 2018
I am currently learning to use the image acquisition toolbox. I am using videoinput to create a video object (vidobj) that I am using to aquire video (at 30fps) and writing to disk. At the same time I am also trying to analyze frames from this vidobj in real time ( at a slower frame rate). See code below :
xy = zeros(2,3000);
count = 0;
% Defining features of the video object that will be recorded
vidobj = videoinput('pointgrey',1);
src = getselectedsource(vidobj);
src.FrameRatePercentage = 50;
vidobj.FramesPerTrigger = Inf;
% vidobj.TriggerRepeat = 50;
vidobj.LoggingMode = 'disk';
logfile = VideoWriter('logfile2.avi');
vidobj.DiskLogger = logfile;
(vidobj); % start recording
while vidobj.FramesAcquired < 6000
snapshot = zeros(1024,1280);
snapshot = getdata(vidobj,1);
count= count+1;
bw = 1 - im2bw(snapshot,0.36);
cc = bwconncomp(bw);
rp = regionprops(cc,'Area','Centroid');
large = find([rp.Area]>70);
jr = find([rp(large).Area]<300);
xy(:,count) = rp(large(jr)).Centroid; % x and y coordinates of the fly for each snapshot
clear snapshot
% pause(180);
s = toc;
When I comment out the while loop and no longer take snapshots, writing vidobj to disk happens almost instantaneously and my RAM usage is low. However when I start taking snapshots my RAM usage shoots up and logging of video object to disk takes twice as long ( a 3 minute recording session takes 6 minutes to complete - as an additional 3 minutes is needed after acquisition to write the video to disk).
How do I fix this issue with getsnapshot? I have acertained that it is not the analysis steps within the while loop that slow down the process and rather it is the getsnapshot command itself.
Thanks in advance for your help.

Answers (1)

Arun Jaganathan
Arun Jaganathan on 13 Nov 2018
Try logging the video in memory and disk simultaneously, so that the videoinput is available for analysis:
videobj.LoggingMode = 'disk&memory';
The reason it takes more time if logged in disk directly could be because, the getsnapshot() function looks for the frame from memory, and not from the disk.

Community Treasure Hunt

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

Start Hunting!