%ezcapvideo.m is a demo application written by Terry Cornall of Monash
%University to show how a video capture activex control can be used from
%within Matlab. It makes extensive use of ezcapvideo.ocx, which needs to
%have been installed and registered on the host PC.

%ezcapvideo is a third party activex control available from
%http://www.shrinkwrapvb.com/ezvidcap.htm Thanks to Ray Mercer.
%This is freeware, but read the licence agreement and stick to it.

dialogs_wanted=0; %change to 1 if want to change video settings like mirror etc.

%make an activex container called ezvid

%ezvid=actxserver('vbvidc60.ezvidcap'); %launches the video interface without a visible picture
ezvid=actxcontrol('vbvidc60.ezvidcap',[0 -100 500 500]); %launches

%xvideoocx and opens a figure linked to the camera. Shows image. Use this
%one. The actxserver doesn't let you get get images
f=gcf;
set(f,'menubar','none','position',[0   400   400   400]);
drawnow; %otherwise it may not expose
%
%ezvid.get %shows some public properties that you can read and modify
%e.g. ezvid.capturefile='C:\my_favorite_capture_file.avi' will change
%the destination for the capture file.
%                      Capturing: 0
%                           hWnd: 786876
%                    DriverIndex: 'VFW_DEFAULT'
%                       AutoSize: 1
%                    CenterVideo: 1
%                    BorderStyle: 'SingleLine'
%                    VideoBorder: 'SingleLine'
%                    CaptureFile: 'C:\CAPTURE.AVI'
%                      BackColor: -2147483643
%                        Preview: 1
%                    PreviewRate: 15
%                     HasOverlay: 0
%                        Overlay: 0
%                    CaptureRate: 15
%         MakeUserConfirmCapture: 1
%            PercentDropForError: 10
%     CaptureViaBackgroundThread: 0
%                      IndexSize: 27000
%                   CaptureAudio: 0
%                 AbortLeftMouse: 1
%                AbortRightMouse: 1
%               TimeLimitEnabled: 0
%                      TimeLimit: 30
%                   StreamMaster: 'CAP_AVSTREAMMASTER_AUDIO'
%                 StretchPreview: 0
%                      CancelKey: 'KEY_NONE'
%              YieldEventEnabled: 0
%              FrameEventEnabled: 0
%        VideoStreamEventEnabled: 0
%         WaveStreamEventEnabled: 0
%      UsePreciseCaptureControls: 0
%                       HasAudio: 1
%                  HasDlgDisplay: 0
%                   HasDlgFormat: 1
%                   HasDlgSource: 1

%ezvid.invoke %shows the available interfaces
%e.g. CaptureVideo = bool CaptureVideo(handle) means that
%doing ezvid.invoke('CaptureVideo') will return a 1 if ok or a 0 if not
%ignore the (handle,  in the args. Look in the 'ezVidCap Help.doc'
%file that comes with the download for info on how to use these interfaces
% 	About = void About(handle)
% 	AllocCapFile = bool AllocCapFile(handle, int32)
% 	CapSingleFrame = bool CapSingleFrame(handle)
% 	CaptureAbort = bool CaptureAbort(handle)
% 	CaptureEnd = bool CaptureEnd(handle)
% 	CaptureVideo = bool CaptureVideo(handle)
% 	DiskSpace = int32 DiskSpace(handle)
% 	EditCopy = bool EditCopy(handle)
% 	GetDriverName = string GetDriverName(handle, Variant(Optional))
% 	GetDriverVersion = string GetDriverVersion(handle, Variant(Optional))
% 	LoadPalette = bool LoadPalette(handle, string)
% 	NumCapDevs = int32 NumCapDevs(handle)
% 	PreciseCaptureCancel = void PreciseCaptureCancel(handle)
% 	PreciseCaptureEnd = void PreciseCaptureEnd(handle)
% 	PreciseCaptureStart = void PreciseCaptureStart(handle)
% 	SaveAs = bool SaveAs(handle, string)
% 	SaveDIB = bool SaveDIB(handle, string)
% 	SavePalette = bool SavePalette(handle, string)
% 	ShowDlgAudioFormat = bool ShowDlgAudioFormat(handle)
% 	ShowDlgCompressionOptions = bool ShowDlgCompressionOptions(handle)
% 	ShowDlgVideoDisplay = bool ShowDlgVideoDisplay(handle)
% 	ShowDlgVideoFormat = bool ShowDlgVideoFormat(handle)
% 	ShowDlgVideoSource = bool ShowDlgVideoSource(handle)
% 	StreamNoFile = bool StreamNoFile(handle)

%get the matlab version
mat_ver=str2num(version('-release'));

%now we should have a window with video in it. (Assuming you have
%a video source plugged in...) Let's select what video and how
if(dialogs_wanted)    %turned off most of the time. Need to turn them on if want to change settings like mirror, etc
	ezvid.invoke('ShowDlgVideoSource');
	ezvid.invoke('ShowDlgVideoFormat');
	ezvid.invoke('ShowDlgVideoDisplay');
end
%cameraname=ezvid.invoke('GetDriverName');
%lets try some stuff...this captures to an avifile that matlab can read
%(maybe) 
% ezvid.capturefile='c:\mycapturefile.avi'
% ezvid.MakeUserConfirmCapture=0; %don't want confirmation dialog...
% ezvid.invoke('CaptureVideo');
% pause(10);
% ezvid.invoke('CaptureEnd');
% 'OK, video capture is finished now...'
% %then go and look in c:\mycapturefile.avi to see if there's 10 secs
% %of video there or not. It works for me... but
%mov=aviread('c:\mycapturefile.avi') %doesn't work...


%and here's a way to get access to the image frames. Not elegant, but
%works. Uses a DIB image file as an intermediate, so it runs fairly slowly.
%About 7 frames per sec, I think. OK for some things.
ezvid.MakeUserConfirmCapture=0; %don't want confirmation dialog...
ezvid.centervideo=1;
ezvid.autosize=0;
%ffig=figure('position',[1,1,200,200],'doublebuffer','on'); %somewhere to draw captured frames
aux=figure('position',[0 0 400 300],'doublebuffer','on','menubar','none');
while(1) %do it forever 
                            %Get a frame
    ezvid.invoke('CapSingleFrame');
    ezvid.preview=1; %start the preview again otherwise only get one frame lots of times
    ezvid.invoke('savedib','c:\mysingleframe.dib');     %You could make this a write to a ramdisk to avoid writing to disk
    origframe=imread('c:\mysingleframe.dib'); %there are a variety of  freeware ramdisk tools available on the net

     thisframe= im2bw(origframe,0.5); %threshold
     
     figure(aux);
     if(mat_ver>13)
         imshow(thisframe,[],'InitialMagnification','fit');
     else
        imshow(thisframe,[],'notruesize'); %show me the image we are working with
     end;
     drawnow; %make sure it shows now
end;

