%% Testing Camera Frame rates Creative Live WebCam
warning off  % secreen displays hurt FR
cd('C:\Documents and Settings\XP User\Desktop\IGVC') % path searching slow
maxNumCompThreads(2);  % go multithread
obj = videoinput('winvideo',1, 'RGB24_320x240'); %set resolution to smaller size
set(obj, 'SelectedSourceName', 'input1')
set(obj, 'ROIPosition',[0 120 320 120])  % optional windowing
triggerconfig(obj, 'manual');  % this is KEY, some people use preview window to get this effect but that is suboptimal

src_obj = getselectedsource(obj)  % hardware specific properties
% auto exposure destroys(!) framerate, Also Powerline correction (set to
% outdoors (no correction)), whiet balance mode seems to not make a diff
set(src_obj, 'ExposureMode', 'manual')
set(src_obj, 'Exposure', 4)
set(src_obj, 'WhiteBalanceMode', 'manual')
start(obj)
disp('camera init done')
 
%% Get first image
im = getsnapshot(obj);
figure(1)
imshow(im)
disp('starting test')
clear frameRate

for i = 1:3000
tic; 
im = getsnapshot(obj);
imBW = ( (im(:,:,1)>200) & (im(:,:,2)>200) & (im(:,:,3)>200) );  % thresh white
imBW = imopen(imBW, strel('rect', [3 3]));
[r,c] = find(imBW);
figure(1); subplot(1,2,1); imshow(im); hold on; plot(c,r,'r.'); hold off
frameRate(i) = 1/toc;
if (i>2)
figure(1); subplot(1,2,2); plot(frameRate(2:i))
end
end
disp('done')
fprintf('Mean frame rate %f, stdev %f \r', [mean(frameRate(2:end)) std(frameRate(2:end))] );
figure(2); hold on; plot(frameRate,'y'); 

%% cleanup
stop(obj)
delete(obj)
clear obj
