video processing in real life

3 views (last 30 days)
noy
noy on 22 Feb 2015
Commented: noy on 23 Feb 2015
Hi guys, Can you please help me to find the problem in this code? this code meant to identify in real time blinking events, captured by the laptop's camera.
It suppose to use the laptop's camera, capture reference image - and manually define a blob(polygone). than it should calc for the recorded video (in real time) the pixel density in the blob area, present and save it.
The problem is in the saving of the 'data' vector -- since the callback function need to by force closed it doesn't return properly.
Thank you so much! Cheers, Noy
function realVideo()
%initiate
data = [];
% Define frame rate
NumberFrameDisplayPerSecond=30;
% Set-up webcam video input
vid = videoinput('winvideo', 1);
% 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');
s=getsnapshot(vid);
image(s)
[r c] = ginput(4);
[BW, xi, yi]= roipoly (s,r,c);
mask = poly2mask(xi, yi, size(BW,1), size(BW,2));
% set up timer object
TimerData=timer('TimerFcn', {@FrameRateDisplay,vid,mask,data },'Period',1/NumberFrameDisplayPerSecond,'ExecutionMode','fixedRate','BusyMode','drop');
% Open figure
hFigure=figure(1);
% Start video and timer object
start(vid);
c = clock; time=[c(4) , c(5), c(6)]
start(TimerData);
uiwait(hFigure);
stop(TimerData);
%clean
delete(TimerData);
stop(vid);
delete(vid);
% clear persistent variables
clear functions;
%function [data] = FrameRateDisplay(obj, event,vid,mask )
function [data] = FrameRateDisplay(obj, event,vid,mask,data )
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 (find(mask==1)));
data(end+1) = Values;
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 (find(mask==1)));
data(end+1) = Value;
OldValues=get(handlesPlot,'YData');
set(handlesPlot,'YData',[OldValues Value]);
% update any changes
set(obj,'UserData',data);
end

Answers (1)

Image Analyst
Image Analyst on 22 Feb 2015
Subtract the current frame from an average of a few prior frames. Then threshold and look for enough pixels to indicate that the light has just turned on. If the difference image has a bunch of positive values, then the light just turned on. If the difference image has all real low values, or negative values, then the light has been on (at least as long as the prior frames you averaged), or is currently off.
See attached demo where I do a running difference on frames. The difference would be that you will use getsnapshot() to get the frame from the webcam rather than read() to get the frame from a video disk file.
  1 Comment
noy
noy on 23 Feb 2015
Unfortunately I don't understand your answer,
My problem, as I see it, is that the calculated data don't save in real time (as soon as i close the window [line 40: uiwait(hFigure);] , all the data is gone).
can you kindly explain me what I should search in your demo??
thank you so much! Noy

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!