How can I insert live video into a MATLAB GUI using Image Acquisition Toolbox?
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 14 Sep. 2012
Bearbeitet: MathWorks Support Team
am 27 Apr. 2023
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.
Akzeptierte Antwort
MathWorks Support Team
am 14 Okt. 2022
Bearbeitet: MathWorks Support Team
am 14 Okt. 2022
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.
1 Kommentar
Madhura Suresh
am 8 Okt. 2014
Bearbeitet: MathWorks Support Team
am 27 Apr. 2023
Hi Tanner, Sam,
Can you contact Mathworks Technical support with detailed information and files regarding your GUIs that you created based on this solution?
Weitere Antworten (1)
MathWorks MATLAB Hardware Team
am 4 Mai 2021
Bearbeitet: MathWorks MATLAB Hardware Team
am 4 Mai 2021
You can also try using the Acquire Webcam Image MATLAB Live Task. For more details refer to the following answer https://www.mathworks.com/matlabcentral/answers/821010-how-do-i-view-the-live-video-feed-and-capture-images-from-my-usb-webcam-in-matlab
0 Kommentare
Siehe auch
Kategorien
Mehr zu Image and Video Ground Truth Labeling finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!