%CAPTIVATOR Script for Webcam capturing software (eventually turn into arbitrary number of cameras accessible)
clear all; close all; clc
%getselectedsource
%% Control Options
superres=0;                   %If Set to 1, will run superresolution algorithm (Beeps signal 4 pictures saved quickly); note: MEMORY INTENSIVE!
poses=10;                      %Number of poses
beeplength=1;                 %Seconds to wait to take pictures after beep
waitlength=2;                %Seconds to wait between poses

%% Get Device Info
imaqreset                       %This is very important!!! Reset the information so that Matlab can ID multiple cameras
imaqhwinfo                      %Gets basic information about your camera driver-should ID 'winvideo'
info=imaqhwinfo('winvideo')     %Should ID multiple cameras if working, then can access through information below
info.DeviceInfo
%device1=hwInfo.DeviceInfo(1)   %Not req, if want device information
%out=imaqfind                    %Tells what video objects are in memory, should be blank at start, then you add cameras manually below

%% Define and Preview Devices (consider automating)
vid(1) = videoinput('winvideo', 1, 'RGB24_640x480'); %May not need to specify  'RGB24_640x480', (may make break if incorrect)
vid(2) = videoinput('winvideo', 2, 'RGB24_640x480');
vid(3) = videoinput('winvideo', 3, 'RGB24_640x480');
%vid(4) = videoinput('winvideo', 4, 'RGB24_640x480');
for i=1:length(vid)
vid_info1=imaqhwinfo(vid(i))      %Camera information, note resolution (known above)
get(vid(i))                       %Get all info about camera
set(vid(i))                       %Shows all properties possible to change for cam
end

%% Set up display (try putting into subplots?)
%Simple Preview
%for i=1:length(vid)
%subplot(2,2,i); preview(vid(i));
%end

% Create a customized Preview.
figure('Name', 'Visualizer');
uicontrol('String', 'Close', 'Callback', 'close(gcf)');
for i=1:length(vid)
vidRes=get(vid(i),'VideoResolution');
nBands= get(vid(i), 'NumberOfBands');
subplot(1,length(vid),i), hImage= imagesc( zeros(vidRes(2), vidRes(1), nBands) );
%set(gcf,'position',[5,14,1275,675],'PaperPositionMode','auto');
preview(vid(i), hImage);
end

%% Capture Sequence (consider customizing)
%Organization: h=pose, i=camera, j=1 normal-j=1:4 superres
%Note, takes about 1/100sec b/t each capture
for h=1:poses;                  
beep                            %Beep indicates taking picture sequence
pause(beeplength)               %1 second pause before take picture
tic
if superres==0;
    for i=1:length(vid)
    frame{h}{i}{1}=getsnapshot(vid(i));       %Take a snapshot from the webcam
    toc
    end
else
    for i=1:length(vid)
        for j=1:4;
        frame{h}{i}{j}=getsnapshot(vid(i));       %Take 4 snapshots from each webcam
        toc
        end
    end
end
pause(waitlength)
end

%% Post Processing Images

%% Save Images
for h=1:length(frame);
for i=1:length(vid);
    figure; image(frame{h}{i}{1});
    print('-djpeg',strcat('cam',num2str(i),'frame',num2str(h)));
    close all;
end
end
save('captivated_pic_data','frame')
return

%% Disp Results
poser=input('Enter pose number to display');
figure
for i=1:length(vid)
    subplot(1,length(vid),i); image(frame{poser}{i}{1});                  %Graphs the pictures taken for you
end
title=strcat('WebcamPreview');
%print('-djpeg',char(title));




%% Scrap Code and Legacy Code (shows a few options)
%obj2 = videoinput('winvideo',1,'RGB24_640x480','Tag','Webcam');
%out3 = imaqfind({'Type', 'Tag'}, {'videoinput', 'Webcam'})

%Change Directory to the one for "observe"
%observe('vid?');
%Title of video may change, see observe for syntax
%observe('vid:1',2,'video####.avi',15,'IV50',100,true)
%observe('Philips SPC230NC Webcam #4', 2, 'video####.avi', 15, 'IV50', 100,
%true);


