MATLAB Answers

How can I insert live video into a MATLAB GUI using Image Acquisition Toolbox?

95 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Sep 2012
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:
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
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.
% 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
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 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;
3. Modify the "--- Executes on button press in startStopCamera." section of code so that it reads as follows:
function startStopCamera_Callback(hObject, eventdata, handles)
% hObject handle to startStopCamera (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Start/Stop Camera
if strcmp(get(handles.startStopCamera,'String'),'Start Camera')
% Camera is off. Change button string and start camera.
set(handles.startStopCamera,'String','Stop Camera')
start(handles.video)
set(handles.startAcquisition,'Enable','on');
set(handles.captureImage,'Enable','on');
else
% Camera is on. Stop camera and change button string.
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)
% 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);
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:
myCameraGUI
The relevant files can be found below.
  4 Comments
Michael C
Michael C on 4 Aug 2015
Hi,
I went through this example and found a problem. In fact, simply downloading the files has this problem. When I double-click on the video, it freezes and somehow causes the deletion of the video/image acquisition object. Can anyone explain to me why this is happening? I have been building my own GUI using this example but I am having the same problem.
Is there a way to reset and reinitialize the image acquisition object while working in the GUI? My idea was to put another if statement in the start/stop button to check if the video is running (isrunning function) or is valid (isvalid function), delete the image object completely, and copy/paste the opening function initialization and start the video. But so far I haven't been able to get that to work.
Any ideas would be helpful.
Thanks,
Michael

Sign in to comment.

More Answers (6)




shivam verma
shivam verma on 26 Jun 2019
sir i have a question that "how i print may text data on the live webcam video". is there any solution ???


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by