The PREVIEW function has a second input argument which allows you to specify a handle graphics image object where the video will be displayed.
More information on how to set this up is available in the documentation:
Previewing Data :: Introduction (Image Acquisition Toolbox)
If you are using a previous version, read the following:
Here is an example of a GUI that offers the ability to use Image Acquisition Toolbox functions interactively.
For this example, three buttons will be used to toggle between turning the camera on and off, capture a snapshot image, and acquire video data. The GUI can be closed at any time by pressing the figure's Close button.
There are two steps in creating this GUI.
Step One. Create the Visual Implementation of the GUI
1. Start GUIDE by typing the following at the MATLAB command prompt:
2. In the GUIDE Quick Start dialog, under GUIDE templates, select Blank GUI (Default) and press OK. This will open a blank GUI figure.
3. Change the following properties of the figure. (You can double-click anywhere in the blank, gray figure area to open the Property Browser)
a. Name - Change to 'MyCameraGUI'
b. Tag - Change to 'MyCameraGUI'
c. Units - Change to 'pixels'
d. Position, Width - Change to 400
e. Position, Height - Change to 420
4. Click the button to the right of 'CloseRequestFcn' to auto-generate a callback function when the GUI is closed.
5. Insert an Axes into your GUI and modify its properties as follows:
a. Tag - Change to 'cameraAxes'
b. Units - Change to 'pixels'
c. Position, x - Change to 40
d. Position, y - Change to 40
e. Position, Width - Change to 320
f. Position, Height - Change to 240
g. Box - Change to 'on'
h. XTick - Change to '[]' by deleting all the entries.
(Note that this automatically changes XTickMode to 'manual'.)
i. XTickLabel - Change to '' by highlighting and delete all the entries.
(Note that this automatically changes XTickLabelMode to 'manual'.)
j. YTick - Change to '[]' by deleting all the entries.
(Note that this automatically changes YTickMode to 'manual'.)
k. YTickLabel - Change to '' by highlighting and delete all the entries.
(Note that this automatically changes YTickLabelMode to 'manual'.)
6. Insert a Push Button into your GUI and modify its properties as follows:
a. String - Change to 'Start Camera'
b. Tag - Change to 'startStopCamera'
c. Units - Change to 'pixels'
d. Position, x - Change to 20
e. Position, y - Change to 320
f. Position, Width - Change to 120
g. Position, Height - Change to 60
7. Repeat Step 6 with the following changes:
a. String - Change to 'Capture Image'
b. Tag - Change to 'captureImage'
c. Position, x - Change to 140
8. Repeat Step 6 with the following changes:
a. String - Change to 'Start Acquisition'
b. Tag - Change to 'startAcquisition'
c. Position, x - Change to 260
9. Save the GUI figure as "myCameraGUI.fig". This will generate "myCameraGUI.m", which we will edit in Step Two.
Step Two. Adapt the Generated Code of the GUI
1. Insert the following the code before the "Update Handles Structure" section of code in the "myCameraGUI_OpeningFcn" to create the video object for the camera.
handles.video = videoinput('winvideo', 1);
set(handles.video,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'...
'image(getsnapshot(handles.video));'...
'set(handles.cameraAxes,''ytick'',[],''xtick'',[]),'...
'else '...
'delete(imaqfind);'...
'end']);
triggerconfig(handles.video,'manual');
handles.video.FramesPerTrigger = Inf;
Please note that the 'adaptorname' argument passed to the VIDEOINPUT command in the code above will work for a Windows OS and appropriate changes will have to be made for other operating systems.
2. Modify the "UIWAIT makes myCameraGUI..." section of code so that it reads as follows:
uiwait(handles.myCameraGUI);
3. Modify the "--- Outputs from this function..." section of code so that it reads as follows:
function varargout = myCameraGUI_OutputFcn(hObject, eventdata, handles)
handles.output = hObject;
varargout{1} = handles.output;
3. Modify the "--- Executes on button press in startStopCamera." section of code so that it reads as follows:
function startStopCamera_Callback(hObject, eventdata, handles)
if strcmp(get(handles.startStopCamera,'String'),'Start Camera')
set(handles.startStopCamera,'String','Stop Camera')
start(handles.video)
set(handles.startAcquisition,'Enable','on');
set(handles.captureImage,'Enable','on');
else
set(handles.startStopCamera,'String','Start Camera')
stop(handles.video)
set(handles.startAcquisition,'Enable','off');
set(handles.captureImage,'Enable','off');
end
4. Modify the "--- Executes on button press in captureImage." section of code so that it reads as follows:
function captureImage_Callback(hObject, eventdata, handles)
frame = get(get(handles.cameraAxes,'children'),'cdata');
save('testframe.mat', 'frame');
disp('Frame saved to file ''testframe.mat''');
5. Modify the "--- Executes on button press in startAcquisition." section of code so that it reads as follows:
function startAcquisition_Callback(hObject, eventdata, handles)
if strcmp(get(handles.startAcquisition,'String'),'Start Acquisition')
set(handles.startAcquisition,'String','Stop Acquisition');
trigger(handles.video);
else
stop(handles.video);
disp('Saving captured video...');
videodata = getdata(handles.video);
save('testvideo.mat', 'videodata');
disp('Video saved to file ''testvideo.mat''');
start(handles.video);
set(handles.startAcquisition,'String','Start Acquisition');
end
6. Modify the "Executes when user attempts to close myCameraGUI." section of code so that it reads as follows:
function myCameraGUI_CloseRequestFcn(hObject, eventdata, handles)
delete(hObject);
delete(imaqfind);
7. Save the modifications that you have made to myCameraGUI.m.
You should now be able to run the GUI by typing the following at the MATLAB command prompt:
The relevant files can be found below.