This code processes a video in real time through a webcam in Matlab. For each frame, I perform some operations and the final result is 'degrees'.
I need to store the result of degrees for, let's say, the last 15 frames, and check that they are all in the same range (for example between 50 and 80 degrees), all this while the video is still running. But I would like to be deleting the previous ones in order to save memory (since it is video acquisition it can run forever), or if not possible how can I always compare with the last 15 frames?
function DetectTarget2()
clc;imaqreset;close all;
try
% For linux
Vid = videoinput('linuxvideo', 1);
catch
try
% For mac
Vid = videoinput('macvideo', 1);
catch
errordlg('No webcam available');
end
end
set(Vid,'FramesPerTrigger',1); %capture 1 frame every time Vid is triggered
set(Vid,'TriggerRepeat',Inf); %infinite amount of triggers
set(Vid,'ReturnedColorSpace','RGB');
triggerconfig(Vid, 'Manual'); %trigger Vid manually within program
t = timer('TimerFcn',@dispim, 'Period', 0.04,...
'executionMode','fixedRate');
function dispim(~,~)
trigger(Vid)%trigger Vid to capture image
im=getdata(Vid,1);
detector = vision.CascadeObjectDetector('Cascade1Matlab.xml');
bbox = step(detector, im);
% CALCULATIONS
degrees=result;
end
end
No problem at all, use the modulo
function to access cells in a cell array.
In somewhat pseudocode:
result_buffer = cell(1, 15);
index = 1;
while ~finished
... % some calculation
result_buffer{mod(index, 15) + 1} = result;
% access some previous result
result_buffer{mod(index - 5, 15) + 1}; % the image of 5 iterations before
index = index + 1;
end
This is a simple circular buffer.