teena_thar asked . 2021-12-28

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

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.

imaq , image , acquisition , toolbox , thread , stream ,

Expert Answer

John Williams answered . 2024-03-28 23:58:09

 

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;

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.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.