Matlab is not traditionally used to do real-time analysis. Usually one goes to Labview when it comes to this. But still Matlab and Labview are both extending their capabilities toward each other market. Advanced analysis can be done in Labview nowadays and more and more data acquisition can be done using Matlab.
I am going to present some ideas to do a form of realtime processing in Matlab. Before going into details, keep in mind that pure real-time analysis is not really possible without appropriate hardware. In short, you need to have a buffer somewhere. But if you are not too demanding, you can do a lot of things.
I am going to play with the one thing most people have : a webcam.
Most webcams don’t guarantee a fixed frame rate. As long as you get a sense of motion, you are happy. But I feel it is a good example as you should be able to run it on your laptop.
But let’s start. Here is the code :
function realVideo() % Define frame rate NumberFrameDisplayPerSecond=10; % Open figure hFigure=figure(1); % Set-up webcam video input try % For windows vid = videoinput('winvideo', 1); catch try % For macs. vid = videoinput('macvideo', 1); catch errordlg('No webcam available'); end end % Set parameters for video % Acquire only one frame each time set(vid,'FramesPerTrigger',1); % Go on forever until stopped set(vid,'TriggerRepeat',Inf); % Get a grayscale image set(vid,'ReturnedColorSpace','grayscale'); triggerconfig(vid, 'Manual'); % set up timer object TimerData=timer('TimerFcn', [email protected],vid},'Period',1/NumberFrameDisplayPerSecond,'ExecutionMode','fixedRate','BusyMode','drop'); % Start video and timer object start(vid); start(TimerData); % We go on until the figure is closed uiwait(hFigure); % Clean up everything stop(TimerData); delete(TimerData); stop(vid); delete(vid); % clear persistent variables clear functions; % This function is called by the timer to display one frame of the figure function FrameRateDisplay(obj, event,vid) persistent IM; persistent handlesRaw; persistent handlesPlot; trigger(vid); IM=getdata(vid,1,'uint8'); if isempty(handlesRaw) % if first execution, we create the figure objects subplot(2,1,1); handlesRaw=imagesc(IM); title('CurrentImage'); % Plot first value Values=mean(IM(:)); subplot(2,1,2); handlesPlot=plot(Values); title('Average of Frame'); xlabel('Frame number'); ylabel('Average value (au)'); else % We only update what is needed set(handlesRaw,'CData',IM); Value=mean(IM(:)); OldValues=get(handlesPlot,'YData'); set(handlesPlot,'YData',[OldValues Value]); end
There are two functions. realVideo is the function you should run and FrameRateDisplay is a function used by a timer object to generate a sense of fixed frame rate. You will get to understand why “a sense”.
To run this example you will need to have the Image Acquisition Toolbox.
This code is, in some ways, in two parts. The first part is interacting with your webcam. It acquires a picture every time ‘trigger(vid);’ is sent.
The second part is a timer object that launch ‘FrameRateDisplay’ at a fixed interval (in this example every 0.1 seconds). I use timer as most computers don’t have a real and regular hardware trigger available. Timer rely on CPU cycles availability to launch a function at regular interval so it is not guarantee but should be quite good. Within FrameRateDisplay, the average of the picture is calculated and added to a plot.
When trying to develop real-time analysis, you should make sure that your analysis (here FrameRateDisplay) is being calculated in less time than the period of your data acquisition. On my machine, this is easily the case.
To achieve that, you can follow my optimization guidelines.
In this example, please note how I used persistent variables to limit memory allocation. I also minimize all my calls to graphics by only updating the data, not the figure or the axes. xlabel or title are very slow functions for instance, so you should call them only once. Another good idea is to limit the size of all variables (please note the usage of uint8).
In any case, this is how it should look like :