I am making a Graphical User Interface, and I would like to insert live video from my camera into an axis in my GUI using Image Acquisition Toolbox. Essentially, I would like to have the functionality of the PREVIEW function within my GUI.
John Williams answered .
2025-11-20
guide
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
% Create video object
% Putting the object into manual trigger mode and then
% starting the object will make GETSNAPSHOT return faster
% since the connection to the camera will already have
% been established.
handles.video = videoinput('winvideo', 1);
set(handles.video,'TimerPeriod', 0.05, ...
'TimerFcn',['if(~isempty(gco)),'...
'handles=guidata(gcf);'... % Update handles
'image(getsnapshot(handles.video));'... % Get picture using GETSNAPSHOT and put it into axes using IMAGE
'set(handles.cameraAxes,''ytick'',[],''xtick'',[]),'... % Remove tickmarks and labels that are inserted when using IMAGE
'else '...
'delete(imaqfind);'... % Clean up - delete any image acquisition objects
'end']);
triggerconfig(handles.video,'manual');
handles.video.FramesPerTrigger = Inf; % Capture frames until we manually stop it
% UIWAIT makes myCameraGUI wait for user response (see UIRESUME) uiwait(handles.myCameraGUI);
3. Modify the "--- Outputs from this function..." section of code so that it reads as follows:
% --- Outputs from this function are returned to the command line.
function varargout = myCameraGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
handles.output = hObject;
varargout{1} = handles.output;
4. Modify the "--- Executes on button press in captureImage." section of code so that it reads as follows:
function captureImage_Callback(hObject, eventdata, handles)
% hObject handle to captureImage (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% frame = getsnapshot(handles.video);
frame = get(get(handles.cameraAxes,'children'),'cdata'); % The current displayed frame
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)
% hObject handle to startAcquisition (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Start/Stop acquisition
if strcmp(get(handles.startAcquisition,'String'),'Start Acquisition')
% Camera is not acquiring. Change button string and start acquisition.
set(handles.startAcquisition,'String','Stop Acquisition');
trigger(handles.video);
else
% Camera is acquiring. Stop acquisition, save video data,
% and change button string.
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); % Restart the camera
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) % hObject handle to myCameraGUI (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: delete(hObject) closes the figure delete(hObject); delete(imaqfind);
myCameraGUI
The relevant files can be found below.